Sha256: 6a99b1125eab4ee16052d0b30f4779faef8e65a9ce19f9715dfb8a7fd1bbe023

Contents?: true

Size: 1.95 KB

Versions: 10

Compression:

Stored size: 1.95 KB

Contents

# coding: utf-8
# frozen_string_literal: true

module Stealth
  module Flow
    class State

      include Comparable

      attr_accessor :name
      attr_reader :spec, :fails_to, :redirects_to

      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

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

      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

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

      def to_s
        "#{name}"
      end

      def to_sym
        name.to_sym
      end

      private

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
stealth-1.1.6 lib/stealth/flow/state.rb
stealth-1.1.5 lib/stealth/flow/state.rb
stealth-1.1.4 lib/stealth/flow/state.rb
stealth-1.1.3 lib/stealth/flow/state.rb
stealth-1.1.2 lib/stealth/flow/state.rb
stealth-1.1.1 lib/stealth/flow/state.rb
stealth-1.1.0 lib/stealth/flow/state.rb
stealth-1.1.0.rc3 lib/stealth/flow/state.rb
stealth-1.1.0.rc2 lib/stealth/flow/state.rb
stealth-1.1.0.rc1 lib/stealth/flow/state.rb