class IeValidator

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# File lib/br_documents/ie_validator.rb, line 2
def validate_each(record, attribute, value)
  ie_present?(value) &&
    attribute_uf_was_configured_at_validator?(record) &&
    can_read_uf_at_record?(options, record) &&
    ie_valid?(record, attribute, value)
end

Private Instance Methods

attribute_uf_was_configured_at_validator?(record) click to toggle source
# File lib/br_documents/ie_validator.rb, line 15
def attribute_uf_was_configured_at_validator?(record)
  record.errors.add(:base, I18n.t('validator.ie.uf.no_configured')) if options[:uf].blank?

  options[:uf].present?
end
can_read_uf_at_record?(options, record) click to toggle source
# File lib/br_documents/ie_validator.rb, line 21
def can_read_uf_at_record?(options, record)
  begin
    uf = read_uf(record)
  rescue NoMethodError
    record.errors.add(
      :base, I18n.t('validator.ie.uf.no_present', uf: options[:uf])
    )
  end

  uf.present?
end
ie_present?(value) click to toggle source
# File lib/br_documents/ie_validator.rb, line 11
def ie_present?(value)
  value.present?
end
ie_valid?(record, attribute, value) click to toggle source
# File lib/br_documents/ie_validator.rb, line 33
def ie_valid?(record, attribute, value)
  begin
    record.errors.add(attribute, :invalid) unless number_valid?(record, value)
  rescue ArgumentError => e
    record.errors.add(attribute, e.message)
  end
  record.errors.messages.empty?
end
number_valid?(record, value) click to toggle source
# File lib/br_documents/ie_validator.rb, line 42
def number_valid?(record, value)
  uf = read_uf(record)
  ie_number = BrDocuments::IE::Factory.create(uf, value)
  ie_number.valid?
end
read_uf(record) click to toggle source
# File lib/br_documents/ie_validator.rb, line 48
def read_uf(record)
  attribute = record
  options[:uf].split('#').each do |field|
    attribute = attribute.send(field)
  end
  attribute
end