Class: MoneyAttribute::AttributeSpec Private
- Inherits:
-
Struct
- Object
- Struct
- MoneyAttribute::AttributeSpec
- 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
-
#amount_column ⇒ Object
Returns the value of attribute amount_column.
-
#amount_type ⇒ Object
Returns the value of attribute amount_type.
-
#currency_column ⇒ Object
Returns the value of attribute currency_column.
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#amount_extractor ⇒ Symbol
private
:subunitsfor integer columns,:to_dfor decimal columns. -
#build_money(amount, currency) ⇒ Mint::Money?
private
Converts a raw amount and currency into a
Mint::Moneyvalue. -
#columns ⇒ Array<String>
private
Returns the backing database columns for the attribute.
-
#composed_of_mapping ⇒ Hash{String => Symbol}
private
Mapping suitable for
composed_of. -
#composite? ⇒ Boolean
private
truewhen the spec describes a two-column (amount + currency) attribute. -
#constructor ⇒ Proc
private
The constructor lambda used by
composed_ofto instantiate Money values. -
#integer_amount? ⇒ Boolean
private
truewhen the amount column stores subunits (bigint). -
#normalize_query_value(value) ⇒ Integer, ...
private
Normalizes a query value to the column's storage format.
-
#single? ⇒ Boolean
private
truewhen the spec describes a single-column (fixed currency) attribute.
Instance Attribute Details
#amount_column ⇒ Object
Returns the value of attribute amount_column
26 27 28 |
# File 'lib/money_attribute/attribute_spec.rb', line 26 def amount_column @amount_column end |
#amount_type ⇒ Object
Returns the value of attribute amount_type
26 27 28 |
# File 'lib/money_attribute/attribute_spec.rb', line 26 def amount_type @amount_type end |
#currency_column ⇒ Object
Returns the value of attribute currency_column
26 27 28 |
# File 'lib/money_attribute/attribute_spec.rb', line 26 def currency_column @currency_column end |
#kind ⇒ Object
Returns the value of attribute kind
26 27 28 |
# File 'lib/money_attribute/attribute_spec.rb', line 26 def kind @kind end |
#name ⇒ Object
Returns the value of attribute name
26 27 28 |
# File 'lib/money_attribute/attribute_spec.rb', line 26 def name @name end |
Instance Method Details
#amount_extractor ⇒ 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 :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.
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 |
#columns ⇒ Array<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.
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_mapping ⇒ Hash{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.
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.
28 |
# File 'lib/money_attribute/attribute_spec.rb', line 28 def composite? = kind == :composite |
#constructor ⇒ Proc
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.
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).
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).
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.
31 |
# File 'lib/money_attribute/attribute_spec.rb', line 31 def single? = kind == :single |