lib/state_fu/sprocket.rb in davidlee-state-fu-0.3.1 vs lib/state_fu/sprocket.rb in davidlee-state-fu-0.10.0
- old
+ new
@@ -1,11 +1,14 @@
module StateFu
- class Sprocket # Abstract Superclass of State & Event
- include StateFu::Helper # define apply!
+ # the abstract superclass of State & Event
+ # defines behaviours shared by both classes
+ class Sprocket
+ include Applicable # define apply!
+ include HasOptions
+
+ attr_reader :machine, :name, :hooks
- attr_reader :machine, :name, :options, :hooks
-
def initialize(machine, name, options={})
@machine = machine
@name = name.to_sym
@options = options.symbolize_keys!
@hooks = StateFu::Hooks.for( self )
@@ -16,10 +19,11 @@
def add_hook slot, name, value
@hooks[slot.to_sym] << [name.to_sym, value]
end
+ # yields a lathe for self; useful for updating machine definitions on the fly
def lathe(options={}, &block)
StateFu::Lathe.new( machine, self, options, &block )
end
def deep_copy
@@ -28,16 +32,27 @@
def to_s
"#<#{self.class}::#{self.object_id} @name=#{name.inspect}>"
end
- def []v
- options[v]
- end
+ # allows state == <name> || event == <name> to return true
+ def == other
+ if other.is_a?(Symbol)
+ self.name == other
+ else
+ super other
+ end
+ end
- def []=v,k
- options[v]=k
+ # allows case equality tests against the state/event's name
+ # eg
+ # case state
+ # when :new
+ # ...
+ # end
+ def === other
+ self.to_sym === other.to_sym || super(other)
end
-
+
end
end