Method: StateFu::Machine.bind!

Defined in:
lib/machine.rb

.bind!(machine, owner, name, field_name) ⇒ Object

make it so that a class which has included StateFu has a binding to this machine



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/machine.rb', line 33

def self.bind!( machine, owner, name, field_name)
  name = name.to_sym
  # define an accessor method with the given name
  if owner.class == Class
    owner.state_fu_machines[name]    = machine
    owner.state_fu_field_names[name] = field_name
    # 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
        define_method name do
          state_fu( name )
        end
      end
    end
    # prepare the persistence field
    StateFu::Persistence.prepare_field owner, field_name 
  else
    _binding = StateFu::Binding.new machine, owner, name, :field_name => field_name, :singleton => true 
    MethodFactory.define_singleton_method(owner, name) { _binding }
  end
end