class Statesman::Callback

Attributes

callback[R]
from[R]
to[R]

Public Class Methods

new(options = { from: nil, to: nil, callback: nil }) click to toggle source
# File lib/statesman/callback.rb, line 11
def initialize(options = { from: nil, to: nil, callback: nil })
  unless options[:callback].respond_to?(:call)
    raise InvalidCallbackError, "No callback passed"
  end

  @from     = options[:from]
  @to       = Array(options[:to])
  @callback = options[:callback]
end

Public Instance Methods

applies_to?(options = { from: nil, to: nil }) click to toggle source
# File lib/statesman/callback.rb, line 25
def applies_to?(options = { from: nil, to: nil })
  matches(options[:from], options[:to])
end
call(*args) click to toggle source
# File lib/statesman/callback.rb, line 21
def call(*args)
  callback.call(*args)
end

Private Instance Methods

matches(from, to) click to toggle source
# File lib/statesman/callback.rb, line 31
def matches(from, to)
  matches_all_transitions ||
    matches_to_state(from, to) ||
    matches_from_state(from, to) ||
    matches_both_states(from, to)
end
matches_all_transitions() click to toggle source
# File lib/statesman/callback.rb, line 38
def matches_all_transitions
  from.nil? && to.empty?
end
matches_both_states(from, to) click to toggle source
# File lib/statesman/callback.rb, line 50
def matches_both_states(from, to)
  from == self.from && self.to.include?(to)
end
matches_from_state(from, to) click to toggle source
# File lib/statesman/callback.rb, line 42
def matches_from_state(from, to)
  (from == self.from && (to.nil? || self.to.empty?))
end
matches_to_state(from, to) click to toggle source
# File lib/statesman/callback.rb, line 46
def matches_to_state(from, to)
  ((from.nil? || self.from.nil?) && self.to.include?(to))
end