Class: Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/scaffold/lib/model/validations/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  default = {
    allow_nil: false,
    presence: false,
    uniqueness: false,
    length: false,
    class: false
  }

  default.merge!(options)
  @attribute = attribute
  @options = default
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



2
3
4
# File 'lib/scaffold/lib/model/validations/validator.rb', line 2

def attribute
  @attribute
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/scaffold/lib/model/validations/validator.rb', line 2

def options
  @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 #{options[:class]}" unless val.is_a?(options[: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? && options[:allow_nil]

  options.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 options[:length][:allow_nil]

    return errors_array
  elsif options[:length].is_a?(Hash)

    min = options[:length][:minimum]
    if min && val.length < min
      errors_array << "#{attribute} must be longer than #{min} characters"
    end
    
    max = options[:length][:maximum]
    if max && val.length > max
      errors_array << "#{attribute} must be shorter than #{max} characters"
    end
  else
    unless val.length = options[: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

Returns:

  • (Boolean)


65
66
67
# File 'lib/scaffold/lib/model/validations/validator.rb', line 65

def valid?(obj)
  errors(obj).empty?
end