Module: MoneyAttribute::Macro::CompositeClassMethods Private
- Includes:
- ColumnTypeValidations
- Defined in:
- lib/money_attribute/macro.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Class methods backing the money_attribute macro.
Constant Summary
Constants included from ColumnTypeValidations
ColumnTypeValidations::VALID_AMOUNT_TYPES, ColumnTypeValidations::VALID_CURRENCY_TYPES
Instance Method Summary collapse
-
#assert_columns_exist!(name, mapping) ⇒ void
private
Raises when the resolved columns are not present on the model.
-
#default_mapping(name) ⇒ Hash{Symbol => String}
private
Returns the default amount/currency mapping for the attribute name.
-
#register_composite_spec(name, mapping) ⇒ AttributeSpec
private
Registers the composite money attribute spec for the model.
-
#resolve_mapping(name, mapping_override) ⇒ Hash{Symbol => String}
private
Normalizes the requested mapping by applying conventions and overrides.
Methods included from ColumnTypeValidations
#assert_valid_amount_column!, #assert_valid_currency_column!
Instance Method Details
#assert_columns_exist!(name, mapping) ⇒ void
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.
This method returns an undefined value.
Raises when the resolved columns are not present on the model.
92 93 94 95 96 97 98 99 100 |
# File 'lib/money_attribute/macro.rb', line 92 def assert_columns_exist!(name, mapping) missing = mapping.values - column_names return if missing.empty? raise ArgumentError, "Could not find columns for :#{name} money attribute. " \ "Expected: #{mapping.values.join(', ')}, " \ "Found: #{attribute_names.join(', ')}" end |
#default_mapping(name) ⇒ Hash{Symbol => 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 default amount/currency mapping for the attribute name.
Resolution order: name_currency + name columns, amount + currency
for :amount, then the name_amount + name_currency convention.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/money_attribute/macro.rb', line 49 def default_mapping(name) name = name.to_s names = column_names if names.include?("#{name}_currency") && names.include?(name) { amount: name, currency: "#{name}_currency" } elsif name == 'amount' && names.include?('currency') { amount: name, currency: 'currency' } else { amount: "#{name}_amount", currency: "#{name}_currency" } end end |
#register_composite_spec(name, mapping) ⇒ AttributeSpec
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.
Registers the composite money attribute spec for the model.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/money_attribute/macro.rb', line 69 def register_composite_spec(name, mapping) amount_column = column_for_attribute(mapping[:amount]) currency_column = column_for_attribute(mapping[:currency]) assert_valid_amount_column!(name, mapping[:amount], amount_column) assert_valid_currency_column!(name, mapping[:currency], currency_column) register_money_attribute_spec( name, kind: :composite, amount_column: mapping[:amount], currency_column: mapping[:currency], amount_type: %i[integer bigint].include?(amount_column.type) ? :integer : :decimal ) end |
#resolve_mapping(name, mapping_override) ⇒ Hash{Symbol => 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.
Normalizes the requested mapping by applying conventions and overrides.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/money_attribute/macro.rb', line 30 def resolve_mapping(name, mapping_override) override = mapping_override.compact override.slice!(:amount, :currency) override.transform_values!(&:to_s) mapping = default_mapping(name).merge(override) assert_columns_exist!(name, mapping) mapping end |