class EndState::Transition

Attributes

configuration[R]
mode[R]
object[R]
previous_state[R]
state[R]

Public Class Methods

new(object, previous_state, state, configuration, mode) click to toggle source
# File lib/end_state/transition.rb, line 5
def initialize(object, previous_state, state, configuration, mode)
  @object = object
  @previous_state = previous_state
  @state = state
  @configuration = configuration
  @mode = mode
end

Public Instance Methods

allowed?(params={}) click to toggle source
# File lib/end_state/transition.rb, line 20
def allowed?(params={})
  return params_not_provided(missing_params(params)) unless missing_params(params).empty?
  guards.all? { |guard| guard.new(object, state, params).allowed? }
end
call(params={}) click to toggle source
# File lib/end_state/transition.rb, line 13
def call(params={})
  return guard_failed unless allowed?(params)
  return false unless action.new(object, state).call
  return conclude_failed unless conclude(params)
  true
end
will_allow?(params={}) click to toggle source
# File lib/end_state/transition.rb, line 25
def will_allow?(params={})
  return false unless missing_params(params).empty?
  guards.all? { |guard| guard.new(object, state, params).will_allow? }
end

Private Instance Methods

conclude(params={}) click to toggle source
# File lib/end_state/transition.rb, line 49
def conclude(params={})
  concluders.each_with_object([]) do |concluder, concluded|
    concluded << concluder
    return rollback(concluded, params) unless concluder.new(object, state, params).call
  end
  true
end
conclude_failed() click to toggle source
# File lib/end_state/transition.rb, line 41
def conclude_failed
  failed ConcluderFailed, 'rolled back'
end
failed(error, message) click to toggle source
# File lib/end_state/transition.rb, line 32
def failed(error, message)
  return false unless mode == :hard
  Kernel.fail error, "The transition to #{state} was #{message}: #{object.failure_messages.join(', ')}"
end
guard_failed() click to toggle source
# File lib/end_state/transition.rb, line 37
def guard_failed
  failed GuardFailed, 'blocked'
end
missing_params(params) click to toggle source
# File lib/end_state/transition.rb, line 63
def missing_params(params)
  required_params.select { |key| params[key].nil? }
end
params_not_provided(params_list) click to toggle source
# File lib/end_state/transition.rb, line 45
def params_not_provided(params_list)
  Kernel.fail MissingParams, "Missing params: #{params_list.join(',')}"
end
rollback(concluded, params) click to toggle source
# File lib/end_state/transition.rb, line 57
def rollback(concluded, params)
  action.new(object, previous_state).rollback
  concluded.reverse_each { |concluder| concluder.new(object, state, params).rollback }
  false
end