Class: MoneyAttribute::AttributeSpec

Inherits:
Struct
  • Object
show all
Defined in:
lib/money_attribute/attribute_spec.rb

Overview

Value object holding metadata for a registered money attribute.

Created by money_attribute or money_amount and stored in the class-level registry. Used by query helpers to resolve column names, build Money values, and generate SQL.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#amount_colObject

Returns the value of attribute amount_col

Returns:

  • (Object)

    the current value of amount_col



24
25
26
# File 'lib/money_attribute/attribute_spec.rb', line 24

def amount_col
  @amount_col
end

#amount_typeObject

Returns the value of attribute amount_type

Returns:

  • (Object)

    the current value of amount_type



24
25
26
# File 'lib/money_attribute/attribute_spec.rb', line 24

def amount_type
  @amount_type
end

#currency_colObject

Returns the value of attribute currency_col

Returns:

  • (Object)

    the current value of currency_col



24
25
26
# File 'lib/money_attribute/attribute_spec.rb', line 24

def currency_col
  @currency_col
end

#kindObject

Returns the value of attribute kind

Returns:

  • (Object)

    the current value of kind



24
25
26
# File 'lib/money_attribute/attribute_spec.rb', line 24

def kind
  @kind
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



24
25
26
# File 'lib/money_attribute/attribute_spec.rb', line 24

def name
  @name
end

Instance Method Details

#amount_extractorSymbol

Returns :subunits for integer columns, :to_d for decimal columns.

Returns:

  • (Symbol)

    :subunits for integer columns, :to_d for decimal columns.



42
# File 'lib/money_attribute/attribute_spec.rb', line 42

def amount_extractor = integer_amount? ? :subunits : :to_d

#build_money(amount, currency) ⇒ Mint::Money?

Converts a raw amount and currency into a Mint::Money value.

Parameters:

  • amount (Integer, BigDecimal, nil)

    the raw column value

  • currency (String, Mint::Currency, nil)

    the currency code or object

Returns:

  • (Mint::Money, nil)

    the resolved money value, or nil when amount is nil



57
58
59
60
61
62
# File 'lib/money_attribute/attribute_spec.rb', line 57

def build_money(amount, currency)
  return unless amount
  return amount if amount.is_a?(Mint::Money)

  constructor.call(amount, currency)
end

#columnsArray<String>

Returns the backing database columns for the attribute.

Returns:

  • (Array<String>)

    two-element array for composite, one-element for single.



34
35
36
# File 'lib/money_attribute/attribute_spec.rb', line 34

def columns
  @columns ||= (composite? ? [amount_col, currency_col] : [amount_col]).freeze
end

#composed_of_mappingHash{String => Symbol}

Returns mapping suitable for composed_of.

Returns:

  • (Hash{String => Symbol})

    mapping suitable for composed_of.



45
# File 'lib/money_attribute/attribute_spec.rb', line 45

def composed_of_mapping = { amount_col => amount_extractor, currency_col => :currency_code }

#composite?Boolean

Returns true when the spec describes a two-column (amount + currency) attribute.

Returns:

  • (Boolean)

    true when the spec describes a two-column (amount + currency) attribute.



26
# File 'lib/money_attribute/attribute_spec.rb', line 26

def composite? = kind == :composite

#constructorProc

Returns the constructor lambda used by composed_of to instantiate Money values.

Returns:

  • (Proc)

    the constructor lambda used by composed_of to instantiate Money values.



48
49
50
# File 'lib/money_attribute/attribute_spec.rb', line 48

def constructor
  integer_amount? ? INTEGER_CONSTRUCTOR : DECIMAL_CONSTRUCTOR
end

#integer_amount?Boolean

Returns true when the amount column stores subunits (bigint).

Returns:

  • (Boolean)

    true when the amount column stores subunits (bigint).



39
# File 'lib/money_attribute/attribute_spec.rb', line 39

def integer_amount? = amount_type == :integer

#normalize_query_value(value) ⇒ Integer, ...

Normalizes a query value to the column's storage format.

Composite attributes: decomposes Mint::Money to subunits via .subunits. Single-column attributes: passes through (the registered Type handles serialization).

Parameters:

  • value (Mint::Money, Numeric)

    the query value

Returns:

  • (Integer, BigDecimal, Mint::Money)

    the normalized value



71
72
73
74
75
76
# File 'lib/money_attribute/attribute_spec.rb', line 71

def normalize_query_value(value)
  return value unless integer_amount?
  return value.subunits if value.is_a?(Mint::Money)

  value
end

#single?Boolean

Returns true when the spec describes a single-column (fixed currency) attribute.

Returns:

  • (Boolean)

    true when the spec describes a single-column (fixed currency) attribute.



29
# File 'lib/money_attribute/attribute_spec.rb', line 29

def single? = kind == :single