Class: Axn::Async::BatchEnqueue::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/axn/async/batch_enqueue/config.rb

Overview

Stores the configuration for a single enqueues_each declaration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field:, from:, via:, filter_block:) ⇒ Config

Returns a new instance of Config.



10
11
12
13
14
15
# File 'lib/axn/async/batch_enqueue/config.rb', line 10

def initialize(field:, from:, via:, filter_block:)
  @field = field
  @from = from
  @via = via
  @filter_block = filter_block
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



8
9
10
# File 'lib/axn/async/batch_enqueue/config.rb', line 8

def field
  @field
end

#filter_blockObject (readonly)

Returns the value of attribute filter_block.



8
9
10
# File 'lib/axn/async/batch_enqueue/config.rb', line 8

def filter_block
  @filter_block
end

#fromObject (readonly)

Returns the value of attribute from.



8
9
10
# File 'lib/axn/async/batch_enqueue/config.rb', line 8

def from
  @from
end

#viaObject (readonly)

Returns the value of attribute via.



8
9
10
# File 'lib/axn/async/batch_enqueue/config.rb', line 8

def via
  @via
end

Instance Method Details

#resolve_source(target:) ⇒ Object

Resolves the source collection for iteration Can be a lambda, a symbol (method name on target), or inferred from model: true



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/axn/async/batch_enqueue/config.rb', line 19

def resolve_source(target:)
  return from.call if from.is_a?(Proc)
  return target.send(from) if from.is_a?(Symbol)

  # Infer from field's model config if 'from' is nil
  field_config = target.internal_field_configs.find { |c| c.field == field }
  model_opts = field_config&.validations&.dig(:model)
  model_class = model_opts[:klass] if model_opts.is_a?(Hash)

  unless model_class
    raise ArgumentError,
          "enqueues_each :#{field} requires `from:` option or a `model:` declaration " \
          "on `expects :#{field}` to infer the source collection."
  end
  model_class.all
end