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

Defined in:
foobara-0.5.8/projects/typesystem/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.



71
72
73
# File 'projects/typesystem/projects/state_machine/src/callbacks.rb', line 71

def register_callback_methods
  @register_callback_methods
end

Instance Method Details

#class_callback_registryObject



55
56
57
58
59
60
61
# File 'projects/typesystem/projects/state_machine/src/callbacks.rb', line 55

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>



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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'projects/typesystem/projects/state_machine/src/callbacks.rb', line 80

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



67
68
69
# File 'projects/typesystem/projects/state_machine/src/callbacks.rb', line 67

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

#remove_all_callbacksObject



63
64
65
# File 'projects/typesystem/projects/state_machine/src/callbacks.rb', line 63

def remove_all_callbacks
  @class_callback_registry = nil
end