Module: SSM::ClassMethods
- Defined in:
- lib/ssm.rb
Overview
Class methods to be mixed in when the module is included.
– Note: self returns the actual class –
Instance Attribute Summary collapse
-
#ssm_instance_property ⇒ Object
Returns the value of attribute ssm_instance_property.
Instance Method Summary collapse
-
#inherited(subclass) ⇒ Object
:nodoc:.
-
#ssm_event(name, options = {}, &block) ⇒ Object
Adds new Events.
-
#ssm_events ⇒ Object
Returns all the available Events.
-
#ssm_initial_state(name = nil) ⇒ Object
This method is used as both a setter - in the context of the class declaration - and a getter when called from an instance.
-
#ssm_inject_state_into(name, options = {}, &block) ⇒ Object
Sets the instance attribute that stores a representation of the State.
-
#ssm_state(name, options = {}) ⇒ Object
Adds new States.
-
#ssm_states ⇒ Object
Returns all the available States.
-
#template_state_machine ⇒ Object
:nodoc:.
Instance Attribute Details
#ssm_instance_property ⇒ Object
Returns the value of attribute ssm_instance_property.
114 115 116 |
# File 'lib/ssm.rb', line 114 def ssm_instance_property @ssm_instance_property end |
Instance Method Details
#inherited(subclass) ⇒ Object
:nodoc:
116 117 118 |
# File 'lib/ssm.rb', line 116 def inherited(subclass) #:nodoc: raise Exception.new("SSM cannot be inherited. Use include instead.") end |
#ssm_event(name, options = {}, &block) ⇒ Object
Adds new Events. These Events can then be called as methods.
class Door
include SSM
ssm_initial_state :closed
ssm_state :opened
ssm_event :open, :from => [:closed], :to => :opened do
puts "Just opened door"
end
ssm_event :close, :from => [:opened], :to => :closed do
puts "Closed door"
end
end
door = Door.new
door.open
door.is?(:opened) #=> true
door.closed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/ssm.rb', line 203 def ssm_event(name, = {}, &block) msg = "Please specificy a final state for this transition. Use a lowly instance method if a transition is not required." raise SSM::InvalidTransition.new(msg) unless [:to].is_a?(Symbol) begin # build Array of States this transition can be called from from = [] if [:from].is_a?(Array) and [:from].size > 0 [:from].each { |state_name| from << SSM::TemplateStateMachines[self].get_state_by_name(state_name) } end to = SSM::TemplateStateMachines[self].get_state_by_name([:to]) rescue raise end # Create StateMachine and create method associated with this StateTransition SSM::TemplateStateMachines[self] << SSM::Event.new(name, SSM::StateTransition.new(from, to), &block) define_method("#{name.to_s}") { |*args| _synchronize_state; _ssm_trigger_event(name, args) } end |
#ssm_events ⇒ Object
Returns all the available Events
230 231 232 |
# File 'lib/ssm.rb', line 230 def ssm_events template_state_machine.events end |
#ssm_initial_state(name = nil) ⇒ Object
This method is used as both a setter - in the context of the class declaration - and a getter when called from an instance.
class Door
include SSM
ssm_initial_state :closed
end
Door.new.ssm_initial_state #=> :closed
131 132 133 134 135 |
# File 'lib/ssm.rb', line 131 def ssm_initial_state(name=nil) name.nil? ? SSM::TemplateStateMachines[self].initial_state : SSM::TemplateStateMachines[self].initial_state = SSM::State.new(name) end |
#ssm_inject_state_into(name, options = {}, &block) ⇒ Object
Sets the instance attribute that stores a representation of the State. In the first form, the property will return a symbol represeting the State. In the second form, an integer is returned, making it more convenient when dealing with persistence.
class Door
include SSM
ssm_inject_state_into :state
ssm_initial_state :closed
end
Door.new.state #=> :closed
class Door
include SSM
ssm_inject_state_into :state, :as_integer => true
ssm_initial_state :closed
end
Door.new.state #=> 0
161 162 163 164 165 |
# File 'lib/ssm.rb', line 161 def ssm_inject_state_into(name, ={}, &block) SSM::TemplateStateMachines[self].property_name = name SSM::TemplateStateMachines[self].use_property_index = [:as_integer].nil? ? false : true SSM::TemplateStateMachines[self].injection_strategy = [:strategy] #SSM::InjectionStrategies::Base.factory(options[:strategy]) end |
#ssm_state(name, options = {}) ⇒ Object
Adds new States. This method takes a string or a symbol.
class Door
include SSM
ssm_state :closed
ssm_state :opened
end
176 177 178 |
# File 'lib/ssm.rb', line 176 def ssm_state(name, ={}) SSM::TemplateStateMachines[self] << SSM::State.new(name) end |
#ssm_states ⇒ Object
Returns all the available States
235 236 237 |
# File 'lib/ssm.rb', line 235 def ssm_states template_state_machine.states end |
#template_state_machine ⇒ Object
:nodoc:
225 226 227 |
# File 'lib/ssm.rb', line 225 def template_state_machine #:nodoc: SSM::TemplateStateMachines[self] end |