Class: FiniteMachine::StateMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/roseflow/finite_machine.rb

Overview

Observer

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ StateMachine

Returns a new instance of StateMachine.



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/roseflow/finite_machine.rb', line 257

def initialize(*args, &block)
  options = args.last.is_a?(::Hash) ? args.pop : {}
  @initial_state = DEFAULT_STATE
  @auto_methods = options.fetch(:auto_methods, true)
  @subscribers = Subscribers.new
  @observer = Observer.new(self)
  @events_map = EventsMap.new
  @env = Env.new(self, [])
  @dsl = DSL.new(self, options)
  @name = options.fetch(:name) { ULID.generate }

  env.target = args.pop unless args.empty?
  env.aliases << options[:alias_target] if options[:alias_target]
  dsl.call(&block) if block
  trigger_init
end

Instance Method Details

#transition!(event_name, *data, &block) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/roseflow/finite_machine.rb', line 274

def transition!(event_name, *data, &block)
  from_state = current
  to_state = events_map.move_to(event_name, from_state, *data)

  block.call(from_state, to_state) if block

  if log_transitions
    Logger.report_transition(@name, event_name, from_state, to_state, *data)
  end

  try_trigger(event_name) { transition_to!(to_state) }
end