Sha256: 843bd0880c758f91013d66957cb6b85f70fb40be0e2c8ee498079131e0c2624e

Contents?: true

Size: 1.15 KB

Versions: 26

Compression:

Stored size: 1.15 KB

Contents

require_relative "exceptions"

module Statesman
  class Callback
    attr_reader :from
    attr_reader :to
    attr_reader :callback

    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

    def call(*args)
      callback.call(*args)
    end

    def applies_to?(options = { from: nil, to: nil })
      matches(options[:from], options[:to])
    end

    private

    def matches(from, to)
      matches_all_transitions ||
        matches_to_state(from, to) ||
        matches_from_state(from, to) ||
        matches_both_states(from, to)
    end

    def matches_all_transitions
      from.nil? && to.empty?
    end

    def matches_from_state(from, to)
      (from == self.from && (to.nil? || self.to.empty?))
    end

    def matches_to_state(from, to)
      ((from.nil? || self.from.nil?) && self.to.include?(to))
    end

    def matches_both_states(from, to)
      from == self.from && self.to.include?(to)
    end
  end
end

Version data entries

26 entries across 25 versions & 2 rubygems

Version Path
statesman-5.2.0 lib/statesman/callback.rb
statesman-5.1.0 lib/statesman/callback.rb
statesman-5.0.0 lib/statesman/callback.rb
statesman-4.1.4 lib/statesman/callback.rb
statesman-4.1.3 lib/statesman/callback.rb
statesman-4.1.2 lib/statesman/callback.rb
statesman-4.1.1 lib/statesman/callback.rb
statesman-4.1.0 lib/statesman/callback.rb
statesman-4.0.0 lib/statesman/callback.rb
statesman-3.5.0 lib/statesman/callback.rb
statesman-3.4.1 lib/statesman/callback.rb
statesman-3.4.0 lib/statesman/callback.rb
statesman-3.3.0 lib/statesman/callback.rb
statesman-3.2.0 lib/statesman/callback.rb
statesman-3.1.0 lib/statesman/callback.rb
statesman-3.0.0 lib/statesman/callback.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/statesman-1.3.1/lib/statesman/callback.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/statesman-2.0.1/lib/statesman/callback.rb
statesman-2.0.1 lib/statesman/callback.rb
statesman-2.0.0 lib/statesman/callback.rb