Class: Stateful::States

Inherits:
Object
  • Object
show all
Defined in:
lib/Stateful/States.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ States

Returns a new instance of States.



11
12
13
14
15
16
17
# File 'lib/Stateful/States.rb', line 11

def initialize(klass, options = {})
  @klass = klass
  @options = options
  @all = []
  @initial_state = nil
  @final_states = []
end

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



9
10
11
# File 'lib/Stateful/States.rb', line 9

def all
  @all
end

Instance Method Details

#detect(candidate_state) ⇒ Object Also known as: find



19
20
21
22
23
24
25
26
27
28
# File 'lib/Stateful/States.rb', line 19

def detect(candidate_state)
  candidate_state_name = (
    if candidate_state.is_a?(State)
      candidate_state.name
    else
      candidate_state && candidate_state.to_sym
    end
  )
  self.all.detect{|state| state.name == candidate_state_name}
end

#final_state(*state_names) ⇒ Object Also known as: final_state=



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

def final_state(*state_names)
  final_states(*state_names).first
end

#final_states(*state_names) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/Stateful/States.rb', line 61

def final_states(*state_names)
  state_names = state_names.flatten
  if !state_names.empty?
    state_names.each do |state_name|
      final_state = State.new(state_name)
      @final_states << final_state
      all << final_state
    end
    @final_states
  else
    @final_states
  end
end

#find_or_create(state_name, options = {}) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/Stateful/States.rb', line 40

def find_or_create(state_name, options = {})
  find(state_name) || (
    state = State.new(state_name, options)
    all << state
    state
  )
end

#initial_state(state_name = nil, options = {}, &block) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/Stateful/States.rb', line 48

def initial_state(state_name = nil, options = {}, &block)
  if state_name
    @initial_state = state(state_name, options, &block)
  else
    @initial_state
  end
end

#missing?(state) ⇒ Boolean Also known as: new_state?

Returns:

  • (Boolean)


31
32
33
# File 'lib/Stateful/States.rb', line 31

def missing?(state)
  !detect(state)
end

#present?(state) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/Stateful/States.rb', line 36

def present?(state)
  !!detect(state)
end

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



75
76
77
78
79
80
81
82
83
84
# File 'lib/Stateful/States.rb', line 75

def state(state_name, options = {}, &block)
  options.merge!(non_deterministic_event_ordering: global_non_deterministic_event_ordering?)
  state = find_or_create(state_name, options)
  state.instance_eval(&block) if block
  state.transitions.each do |transition|
    @klass.define_event_method(transition)
  end
  @klass.define_status_predicate_method(state_name)
  state
end

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



86
87
88
89
# File 'lib/Stateful/States.rb', line 86

def stateful(options = {}, &block)
  @options = options
  instance_eval(&block) if block
end