module Ably::Modules::UsesStateMachine

Mixing module that assists with {github.com/gocardless/statesman Statemans State Machine} state transitions and maintaining state of this object’s state.

Expects:

- @state_machine is set to the StateMachine
- StateEmitter is included in the object

Private Class Methods

included(base) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/uses_state_machine.rb, line 88
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

previous_state() click to toggle source

@!attribute [r] previous_state @return [STATE,nil] The previous state for this connection @api private

# File lib/submodules/ably-ruby/lib/ably/modules/uses_state_machine.rb, line 40
def previous_state
  if state_machine.previous_state
    STATE(state_machine.previous_state)
  end
end
state_history() click to toggle source

@!attribute [r] state_history @return [Array<Hash>] All previous states including the current state in date ascending order with Hash properties :state, :metadata, :transitioned_at @api private

# File lib/submodules/ably-ruby/lib/ably/modules/uses_state_machine.rb, line 49
def state_history
  state_machine.history.map do |transition|
    {
      state:           STATE(transition.to_state),
      metadata:        transition.metadata,
      transitioned_at: transition.created_at
    }
  end
end
synchronize_state_with_statemachine(*args) click to toggle source

Provides an internal method for this object’s state to match the StateMachine’s current state. The current object’s state will be changed to the StateMachine state and will emit an event @api private

# File lib/submodules/ably-ruby/lib/ably/modules/uses_state_machine.rb, line 32
def synchronize_state_with_statemachine(*args)
  log_state_machine_state_change
  change_state state_machine.current_state, state_machine.last_transition.metadata
end
transition_state_machine(new_state, emit_params = {}) click to toggle source

Call transition_to on the StateMachine

@return [Boolean] true if new_state can be transitioned to by state machine @api private

# File lib/submodules/ably-ruby/lib/ably/modules/uses_state_machine.rb, line 16
def transition_state_machine(new_state, emit_params = {})
  state_machine.transition_state(new_state, emit_object(new_state, emit_params))
end
transition_state_machine!(new_state, emit_params = {}) click to toggle source

Call transition_to! on the StateMachine An exception wil be raised if new_state cannot be transitioned to by state machine

@return [void] @api private

# File lib/submodules/ably-ruby/lib/ably/modules/uses_state_machine.rb, line 25
def transition_state_machine!(new_state, emit_params = {})
  state_machine.transition_to!(new_state, emit_object(new_state, emit_params))
end

Private Instance Methods

emit_object(new_state, emit_params) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/uses_state_machine.rb, line 76
def emit_object(new_state, emit_params)
  if self.class.emits_klass
    self.class.emits_klass.new((emit_params || {}).merge(
      current: STATE(new_state),
      previous: STATE(state_machine.current_state),
      event: EVENT(new_state)
    ))
  else
    emit_params
  end
end
log_state_machine_state_change() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/uses_state_machine.rb, line 68
def log_state_machine_state_change
  if state_machine.previous_state
    logger.debug { "#{self.class.name}: Transitioned from #{state_machine.previous_state} => #{state_machine.current_state}" }
  else
    logger.debug { "#{self.class.name}: Transitioned to #{state_machine.current_state}" }
  end
end
state_machine() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/uses_state_machine.rb, line 62
def state_machine
  @state_machine
end