Module: CemAcpt::Actions

Extended by:
Logging
Defined in:
lib/cem_acpt/actions.rb

Overview

Provides a way to register actions to be executed if the registered actions are included in the list of actions to be executed.

Defined Under Namespace

Classes: Action, ActionConfig, ActionGroup

Constant Summary

Constants included from Logging

Logging::LEVEL_MAP

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Logging

current_log_config, current_log_config, current_log_format, current_log_format, current_log_level, current_log_level, included, logger, logger, new_log_config, new_log_config, new_log_formatter, new_log_formatter, new_log_level, new_log_level, new_logger, new_logger, verbose?, verbose?

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



121
122
123
# File 'lib/cem_acpt/actions.rb', line 121

def config
  @config
end

Class Method Details

.configure(world_config) {|CemAcpt::Actions::ActionConfig| ... } ⇒ Object

Configures the Actions module.

Parameters:

Yields:



128
129
130
131
132
133
# File 'lib/cem_acpt/actions.rb', line 128

def configure(world_config)
  @config = ActionConfig.new
  @config.only = world_config.get('actions.only') || []
  @config.except = world_config.get('actions.except') || []
  yield @config if block_given?
end

.execute(**opts) ⇒ Array

Executes the actions in the current action groups.

Parameters:

  • opts (Hash)

    the options to be passed to the actions

Options Hash (**opts):

  • :context (Hash)

    the context to be passed to the actions

Returns:

  • (Array)

    the results of the actions



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/cem_acpt/actions.rb', line 139

def execute(**opts)
  logger.debug('CemAcpt::Actions') { "Executing registered actions with opts: #{opts}" }
  context = opts[:context] || {}
  ordered_groups = config.groups.values.sort_by(&:order)
  ordered_groups.each_with_object([]) do |group, results|
    actions = group.filter_actions
    next if actions.empty?

    logger.debug('CemAcpt::Actions') { "Executing action group: #{group.name}" }
    context[:group] = group.name
    context[:actions] = actions.map(&:name)
    if group.async
      Async do
        barrier = Async::Barrier.new
        context[:internet] = Async::HTTP::Internet.new
        actions.each do |action|
          context[:action] = action.name
          barrier.async do
            action_opts = opts[action.name.to_sym] || {}
            action.call(context.merge(action_opts))
          end
        end
        barrier.wait
      ensure
        context[:internet]&.close
      end
      results << context[:results]
    else
      actions.each do |action|
        context[:action] = action.name
        action_opts = opts[action.name.to_sym] || {}
        results << action.call(context.merge(action_opts))
      end
    end
  end
end