Class: Amount::ActiveRecord::AmountValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/amount/active_record/amount_validator.rb

Overview

Validates model-level business rules for ‘has_amount` attributes.

Structural integrity is still handled by ‘has_amount` itself. This validator adds declarative Rails validations such as symbol checks and comparison constraints.

Examples:

Requiring a specific symbol

class Holding < ApplicationRecord
  has_amount :amount
  validates :amount, amount: { symbol: :USDC }
end

Applying numeric thresholds to a fixed-symbol amount

class FeeSchedule < ApplicationRecord
  has_amount :fee, symbol: :SOL
  validates :fee, amount: { greater_than_or_equal_to: 0 }
end

Constant Summary collapse

COMPARATORS =
{
  greater_than: :>,
  greater_than_or_equal_to: :>=,
  less_than: :<,
  less_than_or_equal_to: :<=
}.freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/amount/active_record/amount_validator.rb', line 30

def validate_each(record, attribute, value)
  return if pending_assignment_error?(record, attribute)
  return if value.nil?

  validate_amount_instance(record, attribute, value)
  validate_symbol(record, attribute, value)
  validate_comparators(record, attribute, value)
end