Sha256: 049d3c905678bfda765b93dcf6d4779de32fca09a4402d8b86c9db7e6c8443e6

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module TransitionsListener
  class Listener
    attr_accessor :attr
    def initialize(attr, &block)
      @attr = attr
      instance_eval(&block)
    end

    def before_transition(states, callback_method = nil, &block)
      before_transitions(states: states, block: callback_method || block)
    end

    def after_transition(states, callback_method = nil, &block)
      after_transitions(states: states, block: callback_method || block)
    end

    def filter_transitions(kind, from:, to:)
      transitions = kind == :before ? before_transitions : after_transitions
      transitions.select do |transition|
        match_states?(transition[:states], from, to)
      end
    end

    private

    def match_states?(states, from_state, to_state)
      parse_states(states).any? do |t_from, t_to|
        (t_from == [any] || t_from.include?(from_state.to_s)) &&
          (t_to == [any] || t_to.include?(to_state.to_s))
      end
    end

    def any
      'any_transition_key'
    end

    def before_transitions(transition = nil)
      @before_transitions ||= []
      @before_transitions << transition if transition
      @before_transitions
    end

    def after_transitions(transition = nil)
      @after_transitions ||= []
      @after_transitions << transition if transition
      @after_transitions
    end

    def parse_states(states)
      states.map do |from, to|
        from = [from] unless from.is_a? Array
        to = [to] unless to.is_a? Array
        [from.map(&:to_s), to.map(&:to_s)]
      end.to_h
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
transitions_listener-0.2.1 lib/transitions_listener/listener.rb
transitions_listener-0.2.0 lib/transitions_listener/listener.rb