Class: TransitionsListener::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/transitions_listener/listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr, &block) ⇒ Listener

Returns a new instance of Listener.



6
7
8
9
# File 'lib/transitions_listener/listener.rb', line 6

def initialize(attr, &block)
  @attr = attr
  instance_eval(&block)
end

Instance Attribute Details

#attrObject

Returns the value of attribute attr.



5
6
7
# File 'lib/transitions_listener/listener.rb', line 5

def attr
  @attr
end

Instance Method Details

#after_transition(states, args = {}, &block) ⇒ Object

Parameters:

  • states (Array)

    : Same as before transitions

  • args (Hash) (defaults to: {})

    : Same as before args



26
27
28
29
# File 'lib/transitions_listener/listener.rb', line 26

def after_transition(states, args = {}, &block)
  args[:callback] ||= block if block
  after_transitions(states: parse_states(states), args: args)
end

#before_transition(states, args = {}, &block) ⇒ Object

Parameters:

  • states: (Array)

    Array of transitions [{ from:, to: }, {}, …] Multiple transitions: [{ from: [..], to: [..] }, …] Single transitions: [{ from: :state, to: :new_state }, …] Single transition: { from: :state, to: :new_state }

  • args (Hash) (defaults to: {})

    : { callback: nil, if: nil, unless: nil } callback: (Sym) Method name to be used as the callback if: (Sym) conditional methods else: (Sym) conditional methods



19
20
21
22
# File 'lib/transitions_listener/listener.rb', line 19

def before_transition(states, args = {}, &block)
  args[:callback] ||= block if block
  before_transitions(states: parse_states(states), args: args)
end

#filter_transitions(kind, model, from:, to:) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/transitions_listener/listener.rb', line 31

def filter_transitions(kind, model, from:, to:)
  transitions = kind == :before ? before_transitions : after_transitions
  transitions.select do |transition|
    match_conditions?(model, transition) &&
      match_states?(transition, from, to)
  end
end