Module: TAPI::Validations

Included in:
TAPI::V3::GenericSearch
Defined in:
lib/tapi/v3/validations.rb

Instance Method Summary collapse

Instance Method Details

#add_error(name, message) ⇒ Object



9
10
11
# File 'lib/tapi/v3/validations.rb', line 9

def add_error(name, message)
  (errors[name] ||= []) << message
end

#has_errors?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/tapi/v3/validations.rb', line 5

def has_errors?
  errors.any?
end

#inherited_validationsObject



17
18
19
20
21
22
23
# File 'lib/tapi/v3/validations.rb', line 17

def inherited_validations
  if superclass.respond_to?(:validations)
    superclass.validations + validations
  else
    validations
  end
end

#validate(&block) ⇒ Object



25
26
27
# File 'lib/tapi/v3/validations.rb', line 25

def validate(&block)
  validations << block
end

#validates_date_format_of(name, message) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/tapi/v3/validations.rb', line 37

def validates_date_format_of(name, message)
  validate do
    begin
      Utils.coerce_date(send(name))
    rescue TypeError
      add_error name, message
    end
  end
end

#validates_numericality_of(name, message) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/tapi/v3/validations.rb', line 47

def validates_numericality_of(name, message)
  validate do
    number = send(name)
    unless number.is_a?(Fixnum) || /^[0-9]+$/ =~ number
      add_error name, message
    end
  end
end

#validates_presence_of(name, message) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/tapi/v3/validations.rb', line 29

def validates_presence_of(name, message)
  validate do
    if send(name).blank?
      add_error(name, message)
    end
  end
end

#validationsObject



13
14
15
# File 'lib/tapi/v3/validations.rb', line 13

def validations
  @validations ||= []
end