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.



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

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.



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

def actions
  @actions
end

#asyncObject (readonly)

Returns the value of attribute async.



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

def async
  @async
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#orderObject (readonly)

Returns the value of attribute order.



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

def order
  @order
end

Instance Method Details

#filter_actionsObject



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

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



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

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

#sort!Object



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

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