On Wednesday, July 18, 2012 10:54:32 PM UTC-7, mjackson wrote:
>
> I released a gem a little while ago for doing factories with Sequel. It's 
> called, ever so creatively, sequel-factory.
>
> http://mjijackson.github.com/sequel-factory
>
> From the README:
>
> Sequel::Factory supports the following features:
>
> * Multiple factories per model, with different attributes
> * Inclusion of the attributes of one factory into another
> * Ability to configure the Sequel::Model method used by a factory to 
> generate instances (defaults to create)
> * Ability to override factory-generated values on instance creation
> * Sequential attributes (e.g. auto-incrementing ids)
>
> Also, the actual code is only ~100 lines of Ruby, so it should be fairly 
> straightforward for you to understand.
>
>
Looks good.  Note that the recommended way to do this in Sequel  would be 
to make it a model plugin, and load it via:

  Sequel::Model.plugin :factory

The change should be pretty simple, changing your "class Model" to "module 
Plugins; module Factory; module ClassMethods", and defining regular methods 
instead of singleton methods.  You may want to take a look at the plugins 
that ship with Sequel to get an idea of how things are structured.  Another 
change that should be made if you turn this into the plugin is to 
initialize the @factories and @factory_method variable in the plugin's 
apply method, and copy it into the subclass inside inherited (see below for 
reasoning).

One of the advantages of the plugins route is that the methods are added to 
a module that extends the Model class, instead of the model's singleton 
class, so it is easy to override them and call super.  With Ruby 2.0's 
Module#prepend, this won't be as important, but it's still nice.  Another 
benefit is that your code will be more similar to other Sequel code, so 
it'll be easier for Sequel users to understand and extend.

Other notes:

* make_with should probably use ensure, so that if make raises an 
exception, the @factory_method is still reset to the previous value.
* When using model inheritance (Employee < Person < Sequel::Model), 
factories defined in the parent class (Person) aren't copied into the 
subclass (Employee) when subclassing.  Plugins usually handle these cases 
by duping the variables inside inherited.
* Calling superclass.method if superclass.defined(:method) is generally not 
done in Sequel.  Sequel uses a copy-on-subclassing approach (by overriding 
inherited) so that the data already exists in the subclass.  This decouples 
the subclass data from the superclass data after the subclass has been 
created.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sequel-talk/-/4ouwWaSfQfUJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to