Sha256: a6e7ca5c1fac467676d6758d7d1973991ae37a897464c10497e1b6e75b286f94
Contents?: true
Size: 1.48 KB
Versions: 3
Compression:
Stored size: 1.48 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 false unless possible? 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 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.6 | lib/state_machina/transition.rb |
state_machina-0.1.5 | lib/state_machina/transition.rb |
state_machina-0.1.4 | lib/state_machina/transition.rb |