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