RE: My earlier comment about there being three places that get called
on plugin load and it's potential to cause probs, the state_machine
plugin is one great example of the potential for error.
Essentially self.apply(klass, opts ={}) assumes that you have methods
that are in the ClassMethods and InstanceMethods modules, when in fact
they have not been loaded yet. For example:
Given the model
class Foo < Sequel::Model(:foos)
set_schema do
primary_key :id
varchar :state, :size => 50
end
is :state_machine, :initial => :opened
state :opened
state :closed
end
I get the following error:
/Library/Ruby/Gems/1.8/gems/sequel_model-0.3.3/lib/sequel_model.rb:
305:in `method_missing': undefined method `states=' for Foo:Class
(NoMethodError)
from /Library/Ruby/Gems/1.8/gems/sequel_state_machine-0.0.1/lib/
sequel_state_machine.rb:11:in `apply'
from /Library/Ruby/Gems/1.8/gems/sequel_model-0.3.3/lib/sequel_model/
plugins.rb:11:in `is'
...
extending klass at the top of apply() fixes this particular error:
Index: lib/sequel_state_machine.rb
===================================================================
--- lib/sequel_state_machine.rb (revision 829)
+++ lib/sequel_state_machine.rb (working copy)
@@ -8,6 +8,8 @@
# * +column+ - specifies the column name to use for keeping the
state (default: state)
# * +initial+ - specifies an initial state for newly created
objects (required)
def self.apply(klass, options = {})
+ klass.extend(Sequel::Plugins::StateMachine::ClassMethods)
+ klass.extend(Sequel::Plugins::StateMachine::InstanceMethods)
klass.states = Array.from_hash({}) #Array keyset by sequel
klass.events = Array.from_hash({})
klass.transitions = {}
There are other problems with the plugin, but frankly this shows the
potential pitfalls with the three calls to load plugin functionality
with is() . Maybe delegate the use of ClassMethods, InstanceMethods
as a "best practice" would be a better design choice.
-angel
On Jan 17, 2:09 am, "Wayne E. Seguin" <[EMAIL PROTECTED]> wrote:
> On Jan 14, 2008 4:14 PM, Angel <[EMAIL PROTECTED]> wrote:
>
> > I noticed that state_machine has some code and looks like it should
> > work (apply(), InstanceMethods and ClassMethods are there to pull in),
> > but does not work. I think this is because the apply() is using
> > methods that are not included in the class yet, since apply is called
> > prior to extending by ClassMethods ???
>
> > Anywho, a wiki page on creating plugins and extensions would be most
> > helpful for me on what is loaded and in what order. A sort of "best
> > practices" would be nice.
>
> I am working on two wiki pages, one for plugin writing and one for plugin
> usage (more what plugins are available).
>
> I am waiting on Inviz to let me know where he is with state_machine.
>
> ~Wayne
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---