Sha256: 72a2e4e8d165e1965edb835e1208be6fe05597a55e9449736d1a86cd8c66cf9a

Contents?: true

Size: 1.55 KB

Versions: 8

Compression:

Stored size: 1.55 KB

Contents

require 'statesman'
require 'ably/modules/statesman_monkey_patch'

module Ably::Modules
  # Module providing Statesman StateMachine functionality
  #
  # Expects method #logger to be defined
  #
  # @api private
  module StateMachine
    def self.included(klass)
      klass.class_eval do
        include Statesman::Machine
      end
      klass.extend Ably::Modules::StatesmanMonkeyPatch
      klass.extend ClassMethods
    end

    # Alternative to Statesman's #transition_to that:
    # * log state change failures to {Logger}
    #
    # @return [void]
    #
    def transition_state(state, *args)
      unless result = transition_to(state.to_sym, *args)
        exception = exception_for_state_change_to(state)
        logger.fatal { "#{self.class}: #{exception.message}\n#{caller[0..20].join("\n")}" }
      end
      result
    end

    # @return [Statesman History Object]
    #
    def previous_transition
      history[-2]
    end

    # @return [Symbol]
    #
    def previous_state
      previous_transition.to_state if previous_transition
    end

    # @return [Ably::Exceptions::InvalidStateChange]
    #
    def exception_for_state_change_to(state)
      error_message = "#{self.class}: Unable to transition from #{current_state} => #{state}"
      Ably::Exceptions::InvalidStateChange.new(error_message, nil, Ably::Exceptions::Codes::CHANNEL_OPERATION_FAILED_INVALID_CHANNEL_STATE)
    end

    module ClassMethods
      private

      def is_error_type?(error)
        error.kind_of?(Ably::Models::ErrorInfo) || error.kind_of?(StandardError)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
ably-rest-1.2.7 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-1.2.7 lib/ably/modules/state_machine.rb
ably-rest-1.2.6 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-1.2.6 lib/ably/modules/state_machine.rb
ably-rest-1.2.4 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-1.2.4 lib/ably/modules/state_machine.rb
ably-rest-1.2.3 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-1.2.3 lib/ably/modules/state_machine.rb