Module: MoneyAttribute::PickAmount

Included in:
QueryMethods
Defined in:
lib/money_attribute/query/pick.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#pick_amount(*attrs) ⇒ Mint::Money, ...

Picks money-aware amounts for one or more attributes.

Parameters:

  • attrs (Array<Symbol>)

    one or more registered money attribute names

Returns:

  • (Mint::Money, Array, nil)

    Money for a single attribute, row array for multiple, nil if empty

Raises:

  • (ArgumentError)

    if any attribute is not a registered money attribute



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/money_attribute/query/pick.rb', line 11

def pick_amount(*attrs)
  raise ArgumentError, 'No attribute specified' if attrs.empty?

  specs = attrs.map { |attr| money_attribute_spec!(attr) }
  return pick_single_amount(specs.first) if specs.length == 1

  raw = pick(*specs.flat_map(&:columns))
  return unless raw

  cursor = 0
  specs.map do |spec|
    value, cursor = extract_pick_value(raw, spec, cursor)
    value
  end
end