Class: CemAcpt::Actions::ActionGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/cem_acpt/actions.rb

Overview

Represents a group of actions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :main, **opts) ⇒ ActionGroup

Returns a new instance of ActionGroup.



31
32
33
34
35
36
# File 'lib/cem_acpt/actions.rb', line 31

def initialize(name = :main, **opts)
  @name = name.to_sym
  @actions = []
  @async = opts[:async] || false
  @order = opts[:order] || 0
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



29
30
31
# File 'lib/cem_acpt/actions.rb', line 29

def actions
  @actions
end

#asyncObject (readonly)

Returns the value of attribute async.



29
30
31
# File 'lib/cem_acpt/actions.rb', line 29

def async
  @async
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/cem_acpt/actions.rb', line 29

def name
  @name
end

#orderObject (readonly)

Returns the value of attribute order.



29
30
31
# File 'lib/cem_acpt/actions.rb', line 29

def order
  @order
end

Instance Method Details

#filter_actionsObject



52
53
54
55
56
57
58
59
60
# File 'lib/cem_acpt/actions.rb', line 52

def filter_actions
  filtered = @actions.dup
  only = CemAcpt::Actions.config.only
  except = CemAcpt::Actions.config.except
  filtered.select! { |action| only.include?(action.name) } unless only.empty?
  filtered.reject! { |action| except.include?(action.name) } unless except.empty?
  filtered.sort_by!(&:order)
  filtered
end

#register_action(name, order: 0, &block) ⇒ Object

Registers a block to be executed if the registered actions are included in the list of actions to be executed.

Parameters:

  • action (String, Symbol)

    the name of the action to be registered

  • order (Integer) (defaults to: 0)

    the order in which the action should be executed. Lower numbers are executed first, but this order is relative to the order of the associated action groups’ execution order. Defaults to 0.

  • block (Proc)

    the block to be executed



45
46
47
48
49
50
# File 'lib/cem_acpt/actions.rb', line 45

def register_action(name, order: 0, &block)
  new_action = Action.new(name, order: order, &block)
  @actions << new_action
  sort!
  self # return self to allow chaining
end

#sort!Object



62
63
64
# File 'lib/cem_acpt/actions.rb', line 62

def sort!
  @actions.sort_by!(&:order)
end