Module: Foobara::StateMachine::Sugar::ClassMethods

Defined in:
foobara-0.0.110/projects/state_machine/src/sugar.rb

Instance Method Summary collapse

Instance Method Details

#create_can_methodsObject



107
108
109
110
111
112
113
# File 'foobara-0.0.110/projects/state_machine/src/sugar.rb', line 107

def create_can_methods
  transitions.each do |transition|
    define_method "can_#{transition}?" do
      can?(transition)
    end
  end
end

#create_enumsObject



90
91
92
93
# File 'foobara-0.0.110/projects/state_machine/src/sugar.rb', line 90

def create_enums
  self.state = Enumerated::Values.new(states)
  self.transition = Enumerated::Values.new(transitions)
end

#create_state_predicate_methodsObject



95
96
97
98
99
100
101
102
103
104
105
# File 'foobara-0.0.110/projects/state_machine/src/sugar.rb', line 95

def create_state_predicate_methods
  states.each do |state|
    define_method "currently_#{state}?" do
      current_state == state
    end

    define_method "ever_#{state}?" do
      current_state == state || log.any? { |log_entry| log_entry.from == state }
    end
  end
end

#create_transition_methodsObject



115
116
117
118
119
120
121
# File 'foobara-0.0.110/projects/state_machine/src/sugar.rb', line 115

def create_transition_methods
  transitions.each do |transition|
    define_method "#{transition}!" do |&block|
      perform_transition!(transition, &block)
    end
  end
end

#desugarize_transition_mapObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'foobara-0.0.110/projects/state_machine/src/sugar.rb', line 10

def desugarize_transition_map
  self.transition_map = {}

  raw_transition_map.each_pair do |from_state, transitions|
    Util.array(from_state).each do |state|
      state = state.to_sym

      transitions.each_pair do |transition, next_state|
        transition = transition.to_sym
        next_state = next_state.to_sym

        transitions_for_state = transition_map[state] ||= {}

        if transitions_for_state.key?(transition)
          # :nocov:
          raise TransitionAlreadyDefinedError, "There's already a #{transition} for #{state}"
          # :nocov:
        end

        transitions_for_state[transition] = next_state
      end
    end
  end

  transition_map.freeze
end

#determine_states_and_transitionsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'foobara-0.0.110/projects/state_machine/src/sugar.rb', line 37

def determine_states_and_transitions
  computed_non_terminal_states = transition_map.keys.uniq
  computed_terminal_states = []
  computed_transitions = []

  transition_map.each_value do |transitions|
    transitions.each_pair do |transition, to_state|
      computed_transitions << transition unless computed_transitions.include?(transition)

      if !computed_non_terminal_states.include?(to_state) && !computed_terminal_states.include?(to_state)
        computed_terminal_states << to_state
      end
    end
  end

  if terminal_states
    all_states = computed_non_terminal_states + computed_terminal_states
    validate_terminal_states(computed_terminal_states, all_states)
    # User has marked states as explicitly terminal even though they might have transitions
    # This is allowed. So fix any such computations.
    computed_non_terminal_states -= terminal_states
    computed_terminal_states |= terminal_states
  else
    self.terminal_states = computed_terminal_states.freeze
  end

  self.non_terminal_states = computed_non_terminal_states.freeze

  computed_states = computed_non_terminal_states + computed_terminal_states

  if states
    validate_states(computed_states)
  else
    self.states = computed_states.freeze
  end

  if transitions
    validate_transitions(computed_transitions)
  else
    self.transitions = computed_transitions.freeze
  end

  if initial_state
    unless states.include?(initial_state)
      raise BadInitialState, "Initial state explicitly set to #{
        initial_state
      } but should have been one of #{states}"
    end
  else
    self.initial_state = states.first
  end
end