module ACH::Validations

This module hosts the most basic validations for both components and records. The only validation being performed is presence validation.

Public Instance Methods

errors() click to toggle source

Return errors hash.

@return [ActiveSupport::OrderedHash]

# File lib/ach/validations.rb, line 40
def errors
  @errors || reset_errors!
end
valid?() click to toggle source

Clear errors, validate object and return true if records are empty.

@return [Boolean]

# File lib/ach/validations.rb, line 8
def valid?
  reset_errors!
  is_a?(Component) ? validate_as_component : validate_as_record
  errors.empty?
end

Private Instance Methods

reset_errors!() click to toggle source

Set +@errors+ as brand new instance of ordered hash.

@return [ActiveSupport::OrderedHash]

# File lib/ach/validations.rb, line 47
def reset_errors!
  @errors = ActiveSupport::OrderedHash.new
end
validate_as_component() click to toggle source

If self is an instance of Component sublclass, validation process will accumulate errors of nested records under corresponding keys to provide better readability.

# File lib/ach/validations.rb, line 17
def validate_as_component
  counts = Hash.new(0)
  to_ach.each do |record|
    unless record.valid?
      klass = record.class
      errors["#{klass}##{counts[klass] += 1}"] = record.errors
    end
  end
end
validate_as_record() click to toggle source

If self is instance of Record, the only records validated are missing values of required fields.

# File lib/ach/validations.rb, line 30
def validate_as_record
  self.class.fields.each do |field|
    errors[field] = "is required" unless fields[field]
  end
end