module SimpleStates

Constants

VERSION

Public Class Methods

included(const) click to toggle source
# File lib/simple_states.rb, line 7
def included(const)
  states = const.const_set(:States, States.new)
  const.send(:prepend, states) if const.respond_to?(:prepend)
  const.extend(ClassMethods)
  const.initial_state = :created
  const.after_initialize(:init_state) if const.respond_to?(:after_initialize)
end

Public Instance Methods

init_state() click to toggle source
# File lib/simple_states.rb, line 37
def init_state
  singleton_class.send(:include, self.class::States) unless self.class.respond_to?(:prepend)
  self.state = self.class.initial_state if self.state.nil?
end
method_missing(name, *args) click to toggle source
Calls superclass method
# File lib/simple_states.rb, line 65
def method_missing(name, *args)
  state = name.to_s[0..-2].to_sym
  return super unless name.to_s[-1] == '?' && self.class.state?(state)
  state?(state)
end
reset_state() click to toggle source
# File lib/simple_states.rb, line 55
def reset_state
  self.state = self.class.initial_state
  self.class::States.events.map { |_, event| event.reset(self) }
end
respond_to?(name) click to toggle source
Calls superclass method
# File lib/simple_states.rb, line 60
def respond_to?(name)
  state = name.to_s[0..-2].to_sym
  name.to_s[-1] == '?' && self.class.state?(state) || super
end
state() click to toggle source
Calls superclass method
# File lib/simple_states.rb, line 46
def state
  state = super
  state.to_sym if state
end
state=(state) click to toggle source
Calls superclass method
# File lib/simple_states.rb, line 42
def state=(state)
  super(state.to_sym)
end
state?(state) click to toggle source
# File lib/simple_states.rb, line 51
def state?(state)
  self.state.to_sym == state.to_sym
end