Class: MoneyAttribute::AttributeSpec Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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_columnObject

Returns the value of attribute amount_column

Returns:

  • (Object)

    the current value of amount_column



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

def amount_column
  @amount_column
end

#amount_typeObject

Returns the value of attribute amount_type

Returns:

  • (Object)

    the current value of amount_type



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

def amount_type
  @amount_type
end

#currency_columnObject

Returns the value of attribute currency_column

Returns:

  • (Object)

    the current value of currency_column



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

def currency_column
  @currency_column
end

#kindObject

Returns the value of attribute kind

Returns:

  • (Object)

    the current value of kind



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

def kind
  @kind
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



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

def name
  @name
end

Instance Method Details

#amount_extractorSymbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Symbol)

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



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

def amount_extractor = integer_amount? ? :subunits : :to_d

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



62
63
64
65
66
67
# File 'lib/money_attribute/attribute_spec.rb', line 62

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

  constructor.call(amount, currency)
end

#columnsArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the backing database columns for the attribute.

Returns:

  • (Array<String>)

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



36
37
38
# File 'lib/money_attribute/attribute_spec.rb', line 36

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

#composed_of_mappingHash{String => Symbol}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns mapping suitable for composed_of.

Returns:

  • (Hash{String => Symbol})

    mapping suitable for composed_of.



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

def composed_of_mapping
  @composed_of_mapping ||= { amount_column => amount_extractor, currency_column => :currency_code }.freeze
end

#composite?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



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

def composite? = kind == :composite

#constructorProc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



52
53
54
# File 'lib/money_attribute/attribute_spec.rb', line 52

def constructor
  integer_amount? ? INTEGER_CONSTRUCTOR : DECIMAL_CONSTRUCTOR
end

#integer_amount?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Boolean)

    true when the amount column stores subunits (bigint).



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

def integer_amount? = amount_type == :integer

#normalize_query_value(value) ⇒ Integer, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



77
78
79
80
81
82
# File 'lib/money_attribute/attribute_spec.rb', line 77

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

  value
end

#single?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



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

def single? = kind == :single