Sha256: fa1e5199ba0571c7a7d9be40904f9776b3970f2c3349a759bc6df87229dc1f31
Contents?: true
Size: 1.34 KB
Versions: 3
Compression:
Stored size: 1.34 KB
Contents
module StateMachina class Transition attr_reader :model_name, :machine_name, :event_name, :from_state_name, :to_state_name, :guard, :metadata attr_accessor :machine, :guard_context alias_method :state_machine, :machine def initialize(model_name, machine_name, event_name, from_state_name, to_state_name, guard: nil, metadata: {}) @model_name = model_name @machine_name = machine_name @event_name = event_name @from_state_name = from_state_name.to_s @to_state_name = to_state_name.to_s @guard = guard @metadata = metadata end def possible? return false if machine.nil? from_state_name == machine.current_state_name end def permitted? return true if guard.nil? return false if guard_context.nil? case guard when Symbol guard_context.public_send(guard) # Supports `guard: :meat?` when Proc if guard.arity >= 1 guard.call(guard_context) # Supports `guard: (model) -> { model.meat? }` else guard_context.instance_exec(&guard) # Supports `guard: -> { meat? }` end else guard.call(guard_context) if guard.respond_to?(:call) # Supports `guard: CallableObject` end end def execute return false if machine.nil? machine.update_state(to_state_name) end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
state_machina-0.1.2 | lib/state_machina/transition.rb |
state_machina-0.1.1 | lib/state_machina/transition.rb |
state_machina-0.1.0 | lib/state_machina/transition.rb |