class StateManager::State

Attributes

name[RW]
parent_state[RW]
states[RW]

Public Class Methods

create_resource_accessor!(name) click to toggle source
# File lib/state_manager/state.rb, line 142
def self.create_resource_accessor!(name)
  unless method_defined?(name)
    define_method name do
      resource
    end
  end
  specification.states.values.each {|s|s.create_resource_accessor!(name)}
end
inherited(child) click to toggle source
# File lib/state_manager/state.rb, line 35
def self.inherited(child)
  # Give all sublcasses a clone of this states specification. Subclasses can
  # add events and states to their specification without affecting the
  # parent
  child.specification = specification.clone
end
new(name, parent_state) click to toggle source
# File lib/state_manager/state.rb, line 44
def initialize(name, parent_state)
  self.name = name
  self.parent_state = parent_state
  self.states = self.class.specification.states.inject({}) do |states, (name, klazz)|
    states[name] = klazz.new(name, self)
    states
  end
end

Public Instance Methods

enter() click to toggle source
# File lib/state_manager/state.rb, line 61
def enter
end
entered() click to toggle source
Calls superclass method StateManager::DelayedJob::State#entered
# File lib/state_manager/plugins/delayed_job.rb, line 50
def entered
  super
end
exit() click to toggle source
# File lib/state_manager/state.rb, line 64
def exit
end
exited() click to toggle source
Calls superclass method StateManager::DelayedJob::State#exited
# File lib/state_manager/plugins/delayed_job.rb, line 54
def exited
  super
end
find_state(path) click to toggle source

Returns the state at the given path

# File lib/state_manager/state.rb, line 121
def find_state(path)
  states = find_states(path)
  states && states.last
end
find_states(path) click to toggle source

Find all the states along the path

# File lib/state_manager/state.rb, line 108
def find_states(path)
  state = self
  parts = path.split('.')
  ret = []
  parts.each do |name|
    state = state.states[name.to_sym]
    ret << state
    return unless state
  end
  ret
end
has_event?(name) click to toggle source
# File lib/state_manager/state.rb, line 94
def has_event?(name)
  name = name.to_sym
  !!self.class.specification.events[name]
end
initial_state() click to toggle source

If an initial state is not explicitly specified, we choose the first leaf state

# File lib/state_manager/state.rb, line 132
def initial_state
  if state = self.class.specification.initial_state
    find_state(state.to_s)
  elsif leaf?
    self
  else
    states.values.first.initial_state
  end
end
leaf?() click to toggle source
# File lib/state_manager/state.rb, line 126
def leaf?
  states.empty?
end
path() click to toggle source

String representing the path of the current state, e.g.: 'parentState.childState'

# File lib/state_manager/state.rb, line 55
def path
  path = name.to_s
  path = "#{parent_state.path}.#{path}" if parent_state && parent_state.name
  path
end
perform_event(name, *args) click to toggle source
# File lib/state_manager/state.rb, line 99
def perform_event(name, *args)
  name = name.to_sym
  event = self.class.specification.events[name]
  result = send(name, *args) if respond_to?(name)
  transition_to(event[:transitions_to]) if event[:transitions_to]
  result
end
resource() click to toggle source

Get the resource stored on the state manager

# File lib/state_manager/state.rb, line 86
def resource
  state_manager.resource
end
state_manager() click to toggle source
# File lib/state_manager/state.rb, line 81
def state_manager
  parent_state.state_manager
end
to_s() click to toggle source
# File lib/state_manager/state.rb, line 73
def to_s
  "#{path}"
end
to_sym() click to toggle source
# File lib/state_manager/state.rb, line 77
def to_sym
  path.to_sym
end
transition_to(*args) click to toggle source
# File lib/state_manager/state.rb, line 90
def transition_to(*args)
  state_manager.transition_to(*args)
end

Protected Instance Methods

method_missing(name, *args, &block) click to toggle source
# File lib/state_manager/state.rb, line 155
def method_missing(name, *args, &block)
  resource.send(name, *args, &block)
end