Module: Opensteam::StateLogic::Mod::ClassMethods

Defined in:
lib/opensteam/state_logic.rb

Instance Method Summary collapse

Instance Method Details

#included(receiver) ⇒ Object

included method called when the state-module is included into the receiver-class (like Order, Shipment, Invoice)

for each instance-method of the state-module, it creates an alias (in the receiver-klass) and delegates it to the corresponding state-module (based on receiver.state -> StatePattern) through the fire_event method.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/opensteam/state_logic.rb', line 57

def included(receiver)
  self.instance_methods(false).each do |m|
    
    receiver.class_eval do
      define_method(m) { |*args| fire_event( m, *args ) }
    end
    
  end
  
  # state_name = self.to_s.demodulize.downcase
  state_name = self.to_s.demodulize.underscore
  receiver.class_eval do
    define_method("#{state_name}?") { self.state == state_name }
    named_scope state_name, :conditions => ["#{self.table_name}.state = ?", state_name ]
    
    class << self ;
      def available_states
        self.included_modules.select { |s| 
          s.ancestors.include?( Opensteam::StateLogic::Mod ) }.reject { |s| 
          s == Opensteam::StateLogic::Mod }
      end
    end
  end

end

#nameObject



38
# File 'lib/opensteam/state_logic.rb', line 38

def name ; self.to_s.demodulize.underscore.to_sym ; end

#respond_to?(method) ⇒ Boolean

def name ; self.to_s.demodulize.downcase.to_sym ; end

Returns:

  • (Boolean)


41
42
43
# File 'lib/opensteam/state_logic.rb', line 41

def respond_to?( method )
  self.protected_instance_methods.include?( method )
end

#state_module?Boolean

Returns:

  • (Boolean)


45
# File 'lib/opensteam/state_logic.rb', line 45

def state_module? ; true ; end