class Stealth::Flow::State

Attributes

fails_to[R]
name[RW]
redirects_to[R]
spec[R]

Public Class Methods

new(name:, spec:, fails_to: nil, redirects_to: nil) click to toggle source
# 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

Public Instance Methods

+(steps) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/stealth/flow/state.rb, line 26
def <=>(other_state)
  state_position(self) <=> state_position(other_state)
end
to_s() click to toggle source
# File lib/stealth/flow/state.rb, line 62
def to_s
  "#{name}"
end
to_sym() click to toggle source
# File lib/stealth/flow/state.rb, line 66
def to_sym
  name.to_sym
end

Private Instance Methods

state_position(state) click to toggle source
# File lib/stealth/flow/state.rb, line 72
def state_position(state)
  states = spec.states.keys

  unless states.include?(state.to_sym)
    raise(ArgumentError, "state `#{state}' does not exist")
  end

  states.index(state.to_sym)
end