Skip to content

Model.Coerce#19

Open
ebaxt wants to merge 7 commits into
benpickles:masterfrom
ebaxt:master
Open

Model.Coerce#19
ebaxt wants to merge 7 commits into
benpickles:masterfrom
ebaxt:master

Conversation

@ebaxt

@ebaxt ebaxt commented Mar 31, 2011

Copy link
Copy Markdown

Hi, I had a stab at adding the Model.Coerce that came up in the date serialization discussion. I've added integer, boolean, float and isoDate coercion as a start since that is what I needed, but one can easily add more by adding to Model.Coerce.

The api looks like this:

var Post = Model("post", function() {
  this.persistence(Model.REST, "/posts"),
  this.coerce(Model.Coerce,
  {
    category_id: "integer",
    lat: "float",
    published: "boolean",
    pubDate: "isoDate"
  })
})

If this is something you would like to use I'll add docs and stuff.

@benpickles

Copy link
Copy Markdown
Owner

Cool, I'd like to include this in the main repo but probably not as core functionality. Rather, like this potential addition, I'd prefer it to be included manually with some sort of Plugin Architecture™ (probably very simple). What do you think?

@ebaxt

ebaxt commented Apr 1, 2011

Copy link
Copy Markdown
Author

Sure, that sounds like a good idea. Are you currently working on something like that? If not I could probably give it a shot if you helped me define the API.

@benpickles

Copy link
Copy Markdown
Owner

I'm currently preparing the groundwork for it, it'll probably be something like this:

// first argument is the class
function SomeModuleOfFunctionality(klass, a, b) {
  // add some functionality to the class
  // can also add to the klass.prototype
}

// probably applied when declaring the model
var Project = Model("project", function() {
  this.use(SomeModuleOfFunctionality, "plus", "arguments")
}

// but can be called whenever
Project.use(SomethingElse)

update: fix code example

@benpickles

Copy link
Copy Markdown
Owner

Looks like @github's new reply-via-email thing needs a little more work...

@benpickles

Copy link
Copy Markdown
Owner

I've just pushed a few changes which include a simple "plugin architecture" which is used like so:

// first argument is the class
function SomeModuleOfFunctionality(klass, a, b) {
  // add some functionality to the class
  // can also add to the klass.prototype
}

// probably applied when declaring the model
var Project = Model("project", function() {
  this.use(SomeModuleOfFunctionality, "plus", "arguments")
}

// but can be called whenever
Project.use(SomethingElse)

@ebaxt

ebaxt commented Apr 8, 2011

Copy link
Copy Markdown
Author

Cool. I'll update my pull request to use the new "plugin architecture".

@ebaxt

ebaxt commented Apr 12, 2011

Copy link
Copy Markdown
Author

Moved Model.Coerce to Plugin.Coerce to make use of the new "plugin architecture". Not sure how you want to bundle the plugins so I just left it for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants