Module: MoneyAttribute::ColumnTypeValidations Private

Included in:
Macro::CompositeClassMethods
Defined in:
lib/money_attribute/column_type_validations.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.

Shared column-type validation for the money_attribute and money_amount macros.

Included by Macro::CompositeClassMethods and MoneyAmount. Raises when a backing column uses a type that cannot store a money amount or currency code.

Constant Summary collapse

VALID_AMOUNT_TYPES =

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

%i[integer bigint decimal].freeze
VALID_CURRENCY_TYPES =

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

%i[string text].freeze

Instance Method Summary collapse

Instance Method Details

#assert_valid_amount_column!(attr_name, column_name, column) ⇒ 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.

Validates that a column can store a money amount.

Parameters:

  • attr_name (Symbol, String)

    the money attribute accessor name

  • column_name (Symbol, String)

    the amount column name

  • column (ActiveRecord::ConnectionAdapters::Column)

    the column metadata

Raises:

  • (ArgumentError)

    if the column type is not numeric



23
24
25
26
27
28
29
# File 'lib/money_attribute/column_type_validations.rb', line 23

def assert_valid_amount_column!(attr_name, column_name, column)
  return if VALID_AMOUNT_TYPES.include?(column.type)

  raise ArgumentError,
        "`:#{attr_name}` amount column `#{column_name}` must be a numeric type " \
        "(integer, bigint, or decimal), got `#{column.type}`"
end

#assert_valid_currency_column!(attr_name, column_name, column) ⇒ 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.

Validates that a column can store a currency code.

Parameters:

  • attr_name (Symbol, String)

    the money attribute accessor name

  • column_name (Symbol, String)

    the currency column name

  • column (ActiveRecord::ConnectionAdapters::Column)

    the column metadata

Raises:

  • (ArgumentError)

    if the column type is not string-like



39
40
41
42
43
44
45
# File 'lib/money_attribute/column_type_validations.rb', line 39

def assert_valid_currency_column!(attr_name, column_name, column)
  return if VALID_CURRENCY_TYPES.include?(column.type)

  raise ArgumentError,
        "`:#{attr_name}` currency column `#{column_name}` must be a string type " \
        "(string or text), got `#{column.type}`"
end