Class: Validate::ValidationMethods
- Inherits:
-
Object
- Object
- Validate::ValidationMethods
show all
- Defined in:
- lib/validate/validations.rb,
lib/validate/validations/meta.rb
Overview
Every validation method has four arguments:
obj: the `to_hash` of the object to validate
field: the field to validate
opts: the options for the validation
validator: a Validator object that can be used for children
Defined Under Namespace
Classes: ArgumentFailureBlockScope
Class Method Summary
collapse
Instance Method Summary
collapse
-
#validates(obj, field, opts, validator) ⇒ Object
-
#validates_array(obj, field, opts, validator) ⇒ Object
-
#validates_booleanness_of(obj, field, opts, validator) ⇒ Object
-
#validates_child_hash(obj, field, opts, validator) ⇒ Object
-
#validates_inclusion_of(obj, field, opts, validator) ⇒ Object
-
#validates_numericality_of(obj, field, opts, validator) ⇒ Object
-
#validates_presence_of(obj, field, opts, validator) ⇒ Object
-
#validates_regex(obj, field, opts, validator) ⇒ Object
-
#validates_type_of(obj, field, opts, validator) ⇒ Object
-
#validates_value_of(obj, field, opts, validator) ⇒ Object
Class Method Details
.method_added(name) ⇒ Object
9
10
11
12
13
14
15
|
# File 'lib/validate/validations/meta.rb', line 9
def self.method_added(name)
return unless @next_reason
reason = @next_reason
@next_reason = nil
(@reasons ||= {})[name.to_s] = reason
end
|
.reason(name) ⇒ Object
17
18
19
|
# File 'lib/validate/validations/meta.rb', line 17
def self.reason(name)
reason = (@reasons || {})[name.to_s] || "did not validate."
end
|
Instance Method Details
#validates(obj, field, opts, validator) ⇒ Object
21
22
23
24
|
# File 'lib/validate/validations.rb', line 21
def validates(obj, field, opts, validator)
args = [obj, field].first(opts[:with].arity)
true == obj[field].instance_exec(*args, &opts[:with])
end
|
#validates_array(obj, field, opts, validator) ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/validate/validations.rb', line 107
def validates_array(obj, field, opts, validator)
return false unless obj[field].respond_to?(:to_a)
array = obj[field].to_a
array.map do |e|
validator.validates?({self: e})
end.reduce {|a,b| a && b }
end
|
#validates_booleanness_of(obj, field, opts, validator) ⇒ Object
69
70
71
|
# File 'lib/validate/validations.rb', line 69
def validates_booleanness_of(obj, field, opts, validator)
[true, false].include?(obj[field])
end
|
#validates_child_hash(obj, field, opts, validator) ⇒ Object
90
91
92
93
94
|
# File 'lib/validate/validations.rb', line 90
def validates_child_hash(obj, field, opts, validator)
return false unless obj[field].respond_to?(:to_hash)
hash = obj[field].to_hash
validator.validates?(hash)
end
|
#validates_inclusion_of(obj, field, opts, validator) ⇒ Object
51
52
53
|
# File 'lib/validate/validations.rb', line 51
def validates_inclusion_of(obj, field, opts, validator)
opts[:in].include?(obj[field])
end
|
#validates_numericality_of(obj, field, opts, validator) ⇒ Object
60
61
62
|
# File 'lib/validate/validations.rb', line 60
def validates_numericality_of(obj, field, opts, validator)
obj[field].is_a?(Numeric)
end
|
#validates_presence_of(obj, field, opts, validator) ⇒ Object
33
34
35
|
# File 'lib/validate/validations.rb', line 33
def validates_presence_of(obj, field, opts, validator)
obj.include?(field)
end
|
#validates_regex(obj, field, opts, validator) ⇒ Object
120
121
122
123
|
# File 'lib/validate/validations.rb', line 120
def validates_regex(obj, field, opts, validator)
return false unless obj[field].respond_to?(:=~)
0 == (obj[field] =~ opts[:matches])
end
|
#validates_type_of(obj, field, opts, validator) ⇒ Object
42
43
44
|
# File 'lib/validate/validations.rb', line 42
def validates_type_of(obj, field, opts, validator)
obj.include?(field) && obj[field].is_a?(opts[:is])
end
|
#validates_value_of(obj, field, opts, validator) ⇒ Object
78
79
80
|
# File 'lib/validate/validations.rb', line 78
def validates_value_of(obj, field, opts, validator)
obj.include?(field) && obj[field] == opts[:is]
end
|