Module: Foobara::StateMachine::Callbacks::ClassMethods

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#register_callback_methodsObject (readonly)

Returns the value of attribute register_callback_methods.



42
43
44
# File 'foobara-0.0.110/projects/state_machine/src/callbacks.rb', line 42

def register_callback_methods
  @register_callback_methods
end

Instance Method Details

#class_callback_registryObject



26
27
28
29
30
31
32
# File 'foobara-0.0.110/projects/state_machine/src/callbacks.rb', line 26

def class_callback_registry
  @class_callback_registry ||= Callback::Registry::Conditioned.new(
    from: states,
    transition: transitions,
    to: states
  )
end

#create_register_callback_methodsObject

0 before_any_transition 1 before_transition_to_<state> 2 before_transition_from_<state> 3 before_transition_from_<state>to<state> 4 before_<transition>_transition 5 before_<transition>transition_to<state> 6 before_<transition>transition_from<state>



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'foobara-0.0.110/projects/state_machine/src/callbacks.rb', line 51

def create_register_callback_methods
  if register_callback_methods
    # :nocov:
    raise "Do not create register callback methods twice"
    # :nocov:
  end

  callback_methods = @register_callback_methods = []

  tos = Set.new
  transitions = Set.new
  froms = Set.new

  froms_to_tos = Set.new
  transitions_to_tos = Set.new
  froms_to_transitions = Set.new
  froms_transitions_tos = Set.new

  transition_map.each_pair do |from, to_map|
    froms << from
    to_map.each_pair do |transition, to|
      transitions << transition
      tos << to
      froms_to_tos << [from, to]
      transitions_to_tos << [transition, to]
      froms_to_transitions << [from, transition]
      froms_transitions_tos << [from, transition, to]
    end
  end

  Foobara::Callback::Block.types.each do |type|
    method_name = "#{type}_any_transition"
    callback_methods << method_name

    define_method method_name do |&block|
      register_transition_callback(type, &block)
    end

    froms.each do |from|
      method_name = "#{type}_transition_from_#{from}"
      callback_methods << method_name

      define_method method_name do |&block|
        register_transition_callback(type, from:, &block)
      end
    end

    transitions.each do |transition|
      method_name = "#{type}_#{transition}"
      callback_methods << method_name

      define_method method_name do |&block|
        register_transition_callback(type, transition:, &block)
      end
    end

    tos.each do |to|
      method_name = "#{type}_transition_to_#{to}"
      callback_methods << method_name

      define_method method_name do |&block|
        register_transition_callback(type, to:, &block)
      end
    end

    froms_to_tos.each do |(from, to)|
      method_name = "#{type}_transition_from_#{from}_to_#{to}"
      callback_methods << method_name

      define_method method_name do |&block|
        register_transition_callback(type, to:, from:, &block)
      end
    end

    transitions_to_tos.each do |(transition, to)|
      method_name = "#{type}_#{transition}_to_#{to}"
      callback_methods << method_name

      define_method method_name do |&block|
        register_transition_callback(type, transition:, to:, &block)
      end
    end

    froms_to_transitions.each do |(from, transition)|
      method_name = "#{type}_#{transition}_from_#{from}"
      callback_methods << method_name

      define_method method_name do |&block|
        register_transition_callback(type, transition:, from:, &block)
      end
    end

    froms_transitions_tos.each do |(from, transition, to)|
      method_name = "#{type}_#{transition}_from_#{from}_to_#{to}"
      callback_methods << method_name

      define_method method_name do |&block|
        register_transition_callback(type, transition:, from:, to:, &block)
      end
    end
  end
end

#register_transition_callback(type) ⇒ Object



38
39
40
# File 'foobara-0.0.110/projects/state_machine/src/callbacks.rb', line 38

def register_transition_callback(type, **, &)
  class_callback_registry.register_callback(type, **, &)
end

#remove_all_callbacksObject



34
35
36
# File 'foobara-0.0.110/projects/state_machine/src/callbacks.rb', line 34

def remove_all_callbacks
  @class_callback_registry = nil
end