Class: SimpleStates::States

Inherits:
Module
  • Object
show all
Defined in:
lib/simple_states/states.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(events) ⇒ States

Returns a new instance of States.



17
18
19
20
# File 'lib/simple_states/states.rb', line 17

def initialize(events)
  @events = merge_events(events)
  setup
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



15
16
17
# File 'lib/simple_states/states.rb', line 15

def events
  @events
end

Class Method Details

.init(object) ⇒ Object



4
5
6
7
# File 'lib/simple_states/states.rb', line 4

def init(object)
  object.singleton_class.send(:include, proxy_for(object.class))
  object.init_state unless object.singleton_class.respond_to?(:after_initialize)
end

.proxy_for(klass) ⇒ Object



9
10
11
12
# File 'lib/simple_states/states.rb', line 9

def proxy_for(klass)
  args = [:StatesProxy].concat(klass.method(:const_defined?).arity != 1 ? [false] : [])
  klass.const_defined?(*args) ? klass::StatesProxy : klass.const_set(:StatesProxy, new(klass.events))
end