Class: StateFu::Sprocket

Inherits:
Object show all
Includes:
HasOptions, Applicable
Defined in:
lib/sprocket.rb

Overview

the abstract superclass of State & Event defines behaviours shared by both classes

Direct Known Subclasses

Event, State

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasOptions

#[], #[]=, included

Methods included from Applicable

included

Constructor Details

#initialize(machine, name, options = {}) ⇒ Sprocket

Returns a new instance of Sprocket.


10
11
12
13
14
15
# File 'lib/sprocket.rb', line 10

def initialize(machine, name, options={})
  @machine = machine
  @name    = name.to_sym
  @options = options.symbolize_keys!
  @hooks   = StateFu::Hooks.for( self )
end

Instance Attribute Details

#hooksObject (readonly)

Returns the value of attribute hooks


8
9
10
# File 'lib/sprocket.rb', line 8

def hooks
  @hooks
end

#machineObject (readonly)

Returns the value of attribute machine


8
9
10
# File 'lib/sprocket.rb', line 8

def machine
  @machine
end

#nameObject (readonly) Also known as: to_sym

Returns the value of attribute name


8
9
10
# File 'lib/sprocket.rb', line 8

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object

allows state == <name> || event == <name> to return true


38
39
40
41
42
43
44
# File 'lib/sprocket.rb', line 38

def == other
  if other.is_a?(Symbol) 
    self.name == other
  else
    super other
  end
end

#===(other) ⇒ Object

allows case equality tests against the state/event's name eg case state when :new

...

end


52
53
54
# File 'lib/sprocket.rb', line 52

def === other
  self.to_sym === other.to_sym || super(other)
end

#add_hook(slot, name, value) ⇒ Object


20
21
22
# File 'lib/sprocket.rb', line 20

def add_hook slot, name, value
  @hooks[slot.to_sym] << [name.to_sym, value]
end

#deep_copyObject

Raises:

  • (NotImeplementedError)

29
30
31
# File 'lib/sprocket.rb', line 29

def deep_copy
  raise NotImeplementedError # abstract
end

#lathe(options = {}, &block) ⇒ Object

yields a lathe for self; useful for updating machine definitions on the fly


25
26
27
# File 'lib/sprocket.rb', line 25

def lathe(options={}, &block)
  StateFu::Lathe.new( machine, self, options, &block )
end

#serializable?Boolean

Returns:

  • (Boolean)

56
57
58
# File 'lib/sprocket.rb', line 56

def serializable?
  !hooks.values.flatten.map(&:class).include?(Proc) && !!(options.to_yaml rescue false)
end

#to_sObject


33
34
35
# File 'lib/sprocket.rb', line 33

def to_s
  "#<#{self.class}::#{self.object_id} @name=#{name.inspect}>"
end