Class: ActsAsTable::RecordModelClassMethods::GetValue

Inherits:
FindOrInitializeBy show all
Defined in:
app/models/concerns/acts_as_table/record_model_class_methods.rb

Overview

Get the value for the given record using the given options.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FindOrInitializeBy

#arity, #inject

Constructor Details

#initialize(**options) ⇒ ActsAsTable::RecordModelClassMethods::GetValue

Returns a new ActsAsTable "destructive" traversal.

Parameters:

  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :default (Boolean)


384
385
386
387
388
389
390
# File 'app/models/concerns/acts_as_table/record_model_class_methods.rb', line 384

def initialize(**options)
  super()

  options.assert_valid_keys(:default)

  @options = options.dup
end

Instance Attribute Details

#optionsHash<Symbol, Object> (readonly)

The options for this ActsAsTable "destructive" traversal.

Returns:

  • (Hash<Symbol, Object>)


376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'app/models/concerns/acts_as_table/record_model_class_methods.rb', line 376

class GetValue < FindOrInitializeBy
  attr_reader :options

  # Returns a new ActsAsTable "destructive" traversal.
  #
  # @param [Hash<Symbol, Object>] options
  # @option options [Boolean] :default
  # @return [ActsAsTable::RecordModelClassMethods::GetValue]
  def initialize(**options)
    super()

    options.assert_valid_keys(:default)

    @options = options.dup
  end

  # Invokes the traversal.
  #
  # @param [ActsAsTable::RecordModel] record_model
  # @param [ActiveRecord::Base, nil] base
  # @return [ActsAsTable::ValueProvider::WrappedValue]
  # @raise [ArgumentError] If the name of a class for a given record does not match the class name for the corresponding ActsAsTable record model.
  def call(record_model, base = nil)
    ActsAsTable.adapter.wrap_value_for(record_model, base, nil, self.inject({}, record_model, base))
  end

  protected

  # @param [Hash<ActsAsTable::RecordModel, ActsAsTable::ValueProvider::WrappedValue>] acc
  # @param [ActsAsTable::Path] path
  # @yieldparam [Hash<ActsAsTable::RecordModel, ActsAsTable::ValueProvider::WrappedValue>] acc
  # @yieldparam [ActsAsTable::Path] path
  # @yieldreturn [Hash<ActsAsTable::RecordModel, ActsAsTable::ValueProvider::WrappedValue>]
  # @return [Hash<ActsAsTable::RecordModel, ActsAsTable::ValueProvider::WrappedValue>]
  # @raise [ArgumentError]
  def _around_inject(acc, path, &block)
    acc.key?(path.options.dig(:data, :target_record_model)) ? acc : block.call(acc)
  end

  # @param [Hash<ActsAsTable::RecordModel, ActsAsTable::ValueProvider::WrappedValue>] acc
  # @param [ActiveRecord::Base, nil] base
  # @param [Hash<Symbol, Object>] options
  # @option options [#call] :find_or_initialize_by
  # @option options [#call] :new
  # @return [Array<Object>]
  # @raise [ArgumentError]
  def _find_or_initialize(acc, record_model, base = nil, **options)
    acc[record_model] ||= ActsAsTable.adapter.get_value_for(record_model, base, **@options)

    [acc, base]
  end
end

Instance Method Details

#call(record_model, base = nil) ⇒ ActsAsTable::ValueProvider::WrappedValue

Invokes the traversal.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    If the name of a class for a given record does not match the class name for the corresponding ActsAsTable record model.



398
399
400
# File 'app/models/concerns/acts_as_table/record_model_class_methods.rb', line 398

def call(record_model, base = nil)
  ActsAsTable.adapter.wrap_value_for(record_model, base, nil, self.inject({}, record_model, base))
end