Class: ActionInteractor::State

Inherits:
Object
  • Object
show all
Defined in:
lib/action_interactor/state.rb

Overview

Action Interactor State

Simple state machine for general purpose. You can declare STATES / TRANSITIONS in subclasses for customizing the behavior. See also ‘ActionInteractor::ExecutionState`

Direct Known Subclasses

ExecutionState

Defined Under Namespace

Classes: TransitionError

Constant Summary collapse

STATES =

Define default states

[:initial, :finished]
TRANSITIONS =

Define default transitions key: target state, value: original states

{
  finished: [:initial]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_state = nil) ⇒ State

Returns a new instance of State.



22
23
24
# File 'lib/action_interactor/state.rb', line 22

def initialize(initial_state=nil)
  @state = initial_state || default_state
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/action_interactor/state.rb', line 57

def method_missing(method_name, *args)
  name = method_name.to_s
  # Returns true if state_name is the same as current state
  if status_method_with_suffix?(name, "?")
    return state == name.chop.to_sym
  end

  # Set current state to the state_name, otherwise raises error
  if status_method_with_suffix?(name, "!")
    state_name = name.chop.to_sym
    unless valid_transition?(state_name)
      raise TransitionError.new("Could not change state :#{state_name} from :#{state}")
    end
    @state = state_name
    return
  end

  super
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



20
21
22
# File 'lib/action_interactor/state.rb', line 20

def state
  @state
end

Class Method Details

.statesObject

Available states for the class



48
49
50
# File 'lib/action_interactor/state.rb', line 48

def self.states
  self::STATES
end

.transitionsObject

Available transitions for the class



53
54
55
# File 'lib/action_interactor/state.rb', line 53

def self.transitions
  self::TRANSITIONS
end

Instance Method Details

#default_stateObject

Default initial state (You can override in subclasses.)



28
29
30
# File 'lib/action_interactor/state.rb', line 28

def default_state
  :initial
end

#statesObject

Available states for the instance



38
39
40
# File 'lib/action_interactor/state.rb', line 38

def states
  self.class.states
end

#transitionsObject

Avaiolable transitions for the instance’s states



43
44
45
# File 'lib/action_interactor/state.rb', line 43

def transitions
  self.class.transitions
end

#valid_transition?(target_state) ⇒ Boolean

Returns true if transition to target_state from current state

Returns:

  • (Boolean)


33
34
35
# File 'lib/action_interactor/state.rb', line 33

def valid_transition?(target_state)
  transitions[target_state].include?(state)
end