On Thu, Jan 19, 2012 at 11:51 PM, Jeremy Evans <[email protected]> wrote:
> On Jan 19, 1:45 pm, Christian MICHON <[email protected]>
> wrote:
>> Hi,
>>
>> I've a portion of code (a checksum) which is commonly used in a
>> specific app I just did. I've dozens of models, if not more, and for
>> each I've been copying the portion of code.
>>
>> This seemed to be a good place for a DRY approach, by either using
>> mixins or plugins. I've questions.
>>
>> - which one would be easier ? I did not manage to make a clean mixin,
>> since I need to invoke some methods like columns().
>
> If by mixin you mean plain ruby module, that's fine.  A plugin is
> useful if you want to share it across multiple applications, or if you
> want to include both class methods and instance methods (or dataset
> methods), but may be overkill if it is for just instance methods in
> one internal app.

plugins look simpler. I could not manage to get columns() work inside
my module. I trashed the code anyway and started looking at plugins.

>
>> - developping a local plugin (without inserting it back into the gem)
>> seems interesting, but then how to load it ?
>
> As long as the plugin is available at sequel/plugins/___.rb and named
> correctly in the Sequel::Plugins namespace, you'll be able to load it
> with Model.plugin just like you load the plugins that ship with
> Sequel.

if not, a single require would do, no? I wish to use the plugin magic
to avoid making mixins directly.

>
>> - I did not manage to find proper guidelines for developping plugins.
>> Is there such guideline ?
>
> Other than the RDoc documentation for Sequel::Plugins, no.
>
>> It seems that this is the bare minimum to have in most plugins I saw:
>>
>> module Sequel
>>   module Plugins
>>     module MyOwnChecksum
>>       def self.apply(model, opts={}, &block)
>>         model.send(:include, ::MyOwnChecksum)
>>       end
>>       def self.configure(model, opts={}, &block)
>>       end
>>       module ClassMethods
>>       end
>>       module InstanceMethods
>>       end
>>       module DatasetMethods
>>       end
>>     end
>>   end
>> end
>>
>> - Is this a decent/correct skeleton ?
>
> The true bare minimum is:
>
> module Sequel
>  module Plugins
>    module MyOwnChecksum
>    end
>  end
> end
>
> as all parts are optional.  Your skeleton has the wrong apply method,
> you should not call include with the plugin module.  The Model.plugin
> method handles loading the ClassMethods, InstanceMethods, and
> DatasetMethods modules correctly.

Well I grabbed the apply part from another piece of code found using google.

>
>> - I need to memoize in a class variable the columns list (I'm making a
>> custom checksum, excluding some columns and therefore I do not wish to
>> calculate at each checksum the list of columns to exclude). How to
>> insert this class variable inside the plugin ?
>
> Class variables are almost certainly the wrong approach.  You probably
> want to use class instance variables (instances variables of the
> class).  This is fairly easy to do by adding an attr_accessor in
> ClassMethods and calling that method on the class itself.
>

I meant a @@ variable/attribute. I will update my code following your
suggestions tomorrow.

Thanks Jeremy!

-- 
Christian

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
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