Class: Stealth::Flow::State

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/stealth/flow/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, spec:, fails_to: nil, redirects_to: nil) ⇒ State

Returns a new instance of State.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/stealth/flow/state.rb', line 13

def initialize(name:, spec:, fails_to: nil, redirects_to: nil)
  if fails_to.present? && !fails_to.is_a?(Stealth::Session)
    raise(ArgumentError, 'fails_to state should be a Stealth::Session')
  end

  if redirects_to.present? && !redirects_to.is_a?(Stealth::Session)
    raise(ArgumentError, 'redirects_to state should be a Stealth::Session')
  end

  @name, @spec = name, spec
  @fails_to, @redirects_to = fails_to, redirects_to
end

Instance Attribute Details

#fails_toObject (readonly)

Returns the value of attribute fails_to.



11
12
13
# File 'lib/stealth/flow/state.rb', line 11

def fails_to
  @fails_to
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/stealth/flow/state.rb', line 10

def name
  @name
end

#redirects_toObject (readonly)

Returns the value of attribute redirects_to.



11
12
13
# File 'lib/stealth/flow/state.rb', line 11

def redirects_to
  @redirects_to
end

#specObject (readonly)

Returns the value of attribute spec.



11
12
13
# File 'lib/stealth/flow/state.rb', line 11

def spec
  @spec
end

Instance Method Details

#+(steps) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stealth/flow/state.rb', line 30

def +(steps)
  if steps < 0
    new_position = state_position(self) + steps

    # we don't want to allow the array index to wrap here so we return
    # the first state instead
    if new_position < 0
      new_state = spec.states.keys.first
    else
      new_state = spec.states.keys.at(new_position)
    end
  else
    new_state = spec.states.keys[state_position(self) + steps]

    # we may have been told to access an out-of-bounds state
    # return the last state
    if new_state.blank?
      new_state = spec.states.keys.last
    end
  end

  new_state
end

#-(steps) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/stealth/flow/state.rb', line 54

def -(steps)
  if steps < 0
    return self + steps.abs
  else
    return self + (-steps)
  end
end

#<=>(other_state) ⇒ Object



26
27
28
# File 'lib/stealth/flow/state.rb', line 26

def <=>(other_state)
  state_position(self) <=> state_position(other_state)
end

#to_sObject



62
63
64
# File 'lib/stealth/flow/state.rb', line 62

def to_s
  "#{name}"
end

#to_symObject



66
67
68
# File 'lib/stealth/flow/state.rb', line 66

def to_sym
  name.to_sym
end