Module: CemAcpt::Actions

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Class Method Details

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

Configures the Actions module.

Parameters:

Yields:



125
126
127
128
129
130
# File 'lib/cem_acpt/actions.rb', line 125

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



136
137
138
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
# File 'lib/cem_acpt/actions.rb', line 136

def execute(**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?

    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