Sha256: 2516a97d01fd10ca54c020434a5d05174f47b0f9eb3d8892a7045b8fe8079d4a

Contents?: true

Size: 1.52 KB

Versions: 24

Compression:

Stored size: 1.52 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}
    # * raise an exception on the {Ably::Realtime::Channel}
    #
    # @return [void]
    def transition_state(state, *args)
      unless result = transition_to(state, *args)
        exception = exception_for_state_change_to(state)
        object.emit :error, exception
        logger.fatal "#{self.class}: #{exception.message}"
      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, 80020)
    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

24 entries across 24 versions & 2 rubygems

Version Path
ably-rest-0.8.15 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-0.8.15 lib/ably/modules/state_machine.rb
ably-rest-0.9.3 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-rest-0.9.2 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-rest-0.9.1 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-rest-0.9.0 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-rest-0.8.14 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-0.8.14 lib/ably/modules/state_machine.rb
ably-rest-0.8.13 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-0.8.13 lib/ably/modules/state_machine.rb
ably-0.8.12 lib/ably/modules/state_machine.rb
ably-0.8.11 lib/ably/modules/state_machine.rb
ably-0.8.10 lib/ably/modules/state_machine.rb
ably-rest-0.8.9 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-0.8.9 lib/ably/modules/state_machine.rb
ably-0.8.8 lib/ably/modules/state_machine.rb
ably-0.8.7 lib/ably/modules/state_machine.rb
ably-0.8.6 lib/ably/modules/state_machine.rb
ably-rest-0.8.6 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb
ably-rest-0.8.5 lib/submodules/ably-ruby/lib/ably/modules/state_machine.rb