class DhEasy::Qa::ValidateType

Attributes

data_hash[R]
desired_type[R]
errored_item[R]
field_to_validate[R]
rules[R]

Public Class Methods

new(data_hash, field_to_validate, desired_type, rules, errored_item) click to toggle source
# File lib/dh_easy/qa/validate_type.rb, line 8
def initialize(data_hash, field_to_validate, desired_type, rules, errored_item)
  @data_hash = data_hash
  @field_to_validate = field_to_validate
  @desired_type = desired_type
  @rules = rules
  @errored_item = errored_item
end

Public Instance Methods

run() click to toggle source
# File lib/dh_easy/qa/validate_type.rb, line 16
def run
  handle_types
end

Private Instance Methods

handle_types() click to toggle source
# File lib/dh_easy/qa/validate_type.rb, line 22
def handle_types
  case desired_type
  when 'String'
    add_errored_item(data_hash, field_to_validate, 'type') if data_hash[field_to_validate].class != String
  when 'Integer'
    add_errored_item(data_hash, field_to_validate, 'type') unless data_hash[field_to_validate].class == Fixnum || data_hash[field_to_validate].to_s.strip =~ /\A\d+(\,\d+)?\z/
  when 'Float'
    add_errored_item(data_hash, field_to_validate, 'type') unless data_hash[field_to_validate].class == Float || data_hash[field_to_validate].to_s.strip =~ /\A\.?\d+(\,\d+)?(\.\d+)?\z/
  when 'Date'
    validate_date_type
  when 'Url'
    add_errored_item(data_hash, field_to_validate, 'type') if data_hash[field_to_validate] !~ /^(http|https):\/\//
  else
    unknown_type_error(desired_type)
  end
end
missing_date_format_error() click to toggle source
# File lib/dh_easy/qa/validate_type.rb, line 59
def missing_date_format_error
  raise StandardError.new("Date validation is missing a format.")
end
unknown_type_error(desired_type) click to toggle source
# File lib/dh_easy/qa/validate_type.rb, line 55
def unknown_type_error(desired_type)
  raise StandardError.new("The validation type '#{desired_type}' is unknown.")
end
validate_date_type() click to toggle source
# File lib/dh_easy/qa/validate_type.rb, line 39
def validate_date_type
  format = rules[field_to_validate]['format']
  missing_date_format_error if format.nil?
  date_str = data_hash[field_to_validate]
  begin
    date = Time.strptime(date_str, format.gsub('%-m', '%m').gsub('%-d', '%d'))
    add_errored_item(data_hash, field_to_validate, 'type') if date.strftime(format) != date_str
  rescue ArgumentError => e
    if e.to_s =~ /^invalid strptime format/
      add_errored_item(data_hash, field_to_validate, 'type')
    else
      raise StandardError.new(e.to_s)
    end
  end
end