Class: Validator
- Inherits:
-
Object
- Object
- Validator
- Defined in:
- lib/scaffold/lib/model/validations/validator.rb
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #allow_nil(_obj, _val, _errors_array) ⇒ Object
- #class(_obj, val, errors_array) ⇒ Object
- #errors(obj) ⇒ Object
-
#initialize(attribute, options = {}) ⇒ Validator
constructor
A new instance of Validator.
- #length(_obj, val, errors_array) ⇒ Object
- #presence(_obj, val, errors_array) ⇒ Object
- #uniqueness(obj, val, errors_array) ⇒ Object
- #valid?(obj) ⇒ Boolean
Constructor Details
#initialize(attribute, options = {}) ⇒ Validator
Returns a new instance of Validator.
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/scaffold/lib/model/validations/validator.rb', line 4 def initialize(attribute, = {}) default = { allow_nil: false, presence: false, uniqueness: false, length: false, class: false } default.merge!() @attribute = attribute @options = default end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
2 3 4 |
# File 'lib/scaffold/lib/model/validations/validator.rb', line 2 def attribute @attribute end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
2 3 4 |
# File 'lib/scaffold/lib/model/validations/validator.rb', line 2 def @options end |
Instance Method Details
#allow_nil(_obj, _val, _errors_array) ⇒ Object
18 19 |
# File 'lib/scaffold/lib/model/validations/validator.rb', line 18 def allow_nil(_obj, _val, _errors_array) end |
#class(_obj, val, errors_array) ⇒ Object
36 37 38 |
# File 'lib/scaffold/lib/model/validations/validator.rb', line 36 def class(_obj, val, errors_array) errors_array << "#{attribute} must be #{[:class]}" unless val.is_a?([:class]) end |
#errors(obj) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/scaffold/lib/model/validations/validator.rb', line 69 def errors(obj) errors_array = [] val = obj.send(attribute) return errors_array if val.nil? && [:allow_nil] .each do |name, validate| self.send(name, obj, val, errors_array) if validate end errors_array end |
#length(_obj, val, errors_array) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/scaffold/lib/model/validations/validator.rb', line 40 def length(_obj, val, errors_array) if val.nil? errors_array << "#{attribute} can't be nil" unless [:length][:allow_nil] return errors_array elsif [:length].is_a?(Hash) min = [:length][:minimum] if min && val.length < min errors_array << "#{attribute} must be longer than #{min} characters" end max = [:length][:maximum] if max && val.length > max errors_array << "#{attribute} must be shorter than #{max} characters" end else unless val.length = [:length] errors_array << "#{attribute} must be #{max} characters" end end errors_array end |
#presence(_obj, val, errors_array) ⇒ Object
21 22 23 |
# File 'lib/scaffold/lib/model/validations/validator.rb', line 21 def presence(_obj, val, errors_array) errors_array << "#{attribute} must be present" if val.blank? end |
#uniqueness(obj, val, errors_array) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/scaffold/lib/model/validations/validator.rb', line 25 def uniqueness(obj, val, errors_array) if obj.id.nil? where_line = "#{attribute} = '#{val}'" else where_line = "#{attribute} = '#{val}' AND id != #{obj.id}" end arr = obj.class.where(where_line) errors_array << "#{attribute} must be unique" unless arr.empty? end |
#valid?(obj) ⇒ Boolean
65 66 67 |
# File 'lib/scaffold/lib/model/validations/validator.rb', line 65 def valid?(obj) errors(obj).empty? end |