Module: SimpleStates::ClassMethods

Defined in:
lib/simple_states.rb

Instance Method Summary collapse

Instance Method Details

#add_states(*states) ⇒ Object



42
43
44
# File 'lib/simple_states.rb', line 42

def add_states(*states)
  self.state_names = (self.state_names || []).concat(states.compact.map(&:to_sym)).uniq
end

#allocateObject



28
29
30
# File 'lib/simple_states.rb', line 28

def allocate
  super.tap { |object| States.init(object) }
end

#event(name, options = {}) ⇒ Object



46
47
48
49
# File 'lib/simple_states.rb', line 46

def event(name, options = {})
  add_states(options[:to], *options[:from])
  self.events += [Event.new(name, options)]
end

#newObject



24
25
26
# File 'lib/simple_states.rb', line 24

def new(*)
  super.tap { |object| States.init(object) }
end

#states(*args) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/simple_states.rb', line 32

def states(*args)
  if args.empty?
    self.state_names ||= add_states(self.initial_state)
  else
    options = args.last.is_a?(Hash) ? args.pop : {}
    self.initial_state = options[:initial].to_sym if options.key?(:initial)
    add_states(*[self.initial_state].concat(args))
  end
end