lib/machine.rb in state-fu-0.12.3 vs lib/machine.rb in state-fu-0.13.0
- old
+ new
@@ -2,14 +2,14 @@
class Machine
def self.BINDINGS
@@_bindings ||= {}
end
-
+
include Applicable
include HasOptions
-
+
attr_reader :hooks
#
# Class methods
#
@@ -17,48 +17,55 @@
def self.for_class(klass, name, options={}, &block)
options.symbolize_keys!
name = name.to_sym
unless machine = klass.state_fu_machines[ name ]
machine = new(options)
- machine.bind! klass, name, options
end
if block_given?
- machine.apply! &block
+ machine.apply! &block
end
+ machine.bind! klass, name, options
machine
end
# make it so that a class which has included StateFu has a binding to
# this machine
- def self.bind!(machine, owner, name, options={})
+ def self.bind!(machine, owner, name, options={})
name = name.to_sym
options[:define_methods] = (name == DEFAULT) unless options.symbolize_keys!.has_key?(:define_methods)
options[:field_name] ||= Persistence.default_field_name(name)
options[:singleton] = true unless owner.is_a?(Class)
# define an accessor method with the given name
if options[:singleton]
_binding = StateFu::Binding.new machine, owner, name, options
MethodFactory.define_singleton_method(owner, name) { _binding }
if alias_name = options[:alias] || options[:as]
MethodFactory.define_singleton_method(owner, alias_name) { _binding }
- end
+ end
else
- owner.state_fu_machines[name] = machine
- owner.state_fu_options[name] = options
- # method_missing to catch NoMethodError for event methods, etc
- StateFu::MethodFactory.define_once_only_method_missing owner
- unless owner.respond_to? name
- owner.class_eval do
+ klass = owner
+ klass.state_fu_machines[name] = machine
+ klass.state_fu_options[name] = options
+
+ # prepare the state machine accessor method
+ if owner.respond_to? name
+ raise "FIXME " + name
+ else
+ klass.class_eval do
define_method name do
state_fu name
end
# allow aliases to be set up, e.g. machine(:as => :status)
if alias_name = options[:alias] || options[:as]
alias_method alias_name, name
end
end
end
+
+ # prepare event / state class methods
+ StateFu::MethodFactory.prepare_class_machine klass, machine, name, options
+
# prepare the persistence field
StateFu::Persistence.prepare_field owner, options[:field_name]
end
end
@@ -95,13 +102,9 @@
helpers.inject_into( obj )
end
def inject_tools_into( obj )
tools.inject_into( obj )
- end
-
- def inject_methods_into( obj )
- #puts 'inject_methods_into'
end
# the modules listed here will be mixed into Binding and
# Transition objects for this machine. use this to define methods,
# references or data useful to you during transitions, event