Class: StateMachina::Transition

Inherits:
Object
  • Object
show all
Defined in:
lib/state_machina/transition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_name, machine_name, event_name, from_state_name, to_state_name, guard: nil, metadata: {}) ⇒ Transition

Returns a new instance of Transition.



7
8
9
10
11
12
13
14
15
# File 'lib/state_machina/transition.rb', line 7

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 = 
end

Instance Attribute Details

#event_nameObject (readonly)

Returns the value of attribute event_name.



3
4
5
# File 'lib/state_machina/transition.rb', line 3

def event_name
  @event_name
end

#from_state_nameObject (readonly)

Returns the value of attribute from_state_name.



3
4
5
# File 'lib/state_machina/transition.rb', line 3

def from_state_name
  @from_state_name
end

#guardObject (readonly)

Returns the value of attribute guard.



3
4
5
# File 'lib/state_machina/transition.rb', line 3

def guard
  @guard
end

#guard_contextObject

Returns the value of attribute guard_context.



4
5
6
# File 'lib/state_machina/transition.rb', line 4

def guard_context
  @guard_context
end

#machineObject Also known as: state_machine

Returns the value of attribute machine.



4
5
6
# File 'lib/state_machina/transition.rb', line 4

def machine
  @machine
end

#machine_nameObject (readonly)

Returns the value of attribute machine_name.



3
4
5
# File 'lib/state_machina/transition.rb', line 3

def machine_name
  @machine_name
end

#metadataObject (readonly)

Returns the value of attribute metadata.



3
4
5
# File 'lib/state_machina/transition.rb', line 3

def 
  @metadata
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



3
4
5
# File 'lib/state_machina/transition.rb', line 3

def model_name
  @model_name
end

#to_state_nameObject (readonly)

Returns the value of attribute to_state_name.



3
4
5
# File 'lib/state_machina/transition.rb', line 3

def to_state_name
  @to_state_name
end

Instance Method Details

#executeObject



42
43
44
45
46
# File 'lib/state_machina/transition.rb', line 42

def execute
  return false if machine.nil?

  machine.update_state(to_state_name)
end

#execute!Object



48
49
50
51
52
# File 'lib/state_machina/transition.rb', line 48

def execute!
  return false if machine.nil?

  machine.update_state!(to_state_name)
end

#permitted?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/state_machina/transition.rb', line 23

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

#possible?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/state_machina/transition.rb', line 17

def possible?
  return false if machine.nil?

  from_state_name == machine.current_state_name
end