Module: AdLint::Validation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(class_or_module) ⇒ Object



158
159
160
# File 'lib/adlint/util.rb', line 158

def self.included(class_or_module)
  class_or_module.extend(self)
end

Instance Method Details

#ensure_dir_presence_of(*args) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/adlint/util.rb', line 80

def ensure_dir_presence_of(*args)
  @validators ||= []
  attr_names, opts = _attr_names_in(args), _options_in(args)
  allow_nil = opts[:allow_nil]
  attr_names.each do |attr_name|
    @validators.push(DirPresenceValidator.new(attr_name, allow_nil))
  end
  self
end

#ensure_dirs_presence_of(*args) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/adlint/util.rb', line 90

def ensure_dirs_presence_of(*args)
  @validators ||= []
  attr_names = _attr_names_in(args)
  attr_names.each do |attr_name|
    @validators.push(DirsPresenceValidator.new(attr_name))
  end
  self
end

#ensure_exam_packages_presence_of(*args) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/adlint/util.rb', line 118

def ensure_exam_packages_presence_of(*args)
  @validators ||= []
  attr_names = _attr_names_in(args)
  attr_names.each do |attr_name|
    @validators.push(ExamPackagesPresenceValidator.new(attr_name))
  end
  self
end

#ensure_file_presence_of(*args) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/adlint/util.rb', line 70

def ensure_file_presence_of(*args)
  @validators ||= []
  attr_names, opts = _attr_names_in(args), _options_in(args)
  allow_nil = opts[:allow_nil]
  attr_names.each do |attr_name|
    @validators.push(FilePresenceValidator.new(attr_name, allow_nil))
  end
  self
end

#ensure_inclusion_of(*args) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/adlint/util.rb', line 99

def ensure_inclusion_of(*args)
  @validators ||= []
  attr_names, opts = _attr_names_in(args), _options_in(args)
  vals = opts[:values] || []
  attr_names.each do |attr_name|
    @validators.push(InclusionValidator.new(attr_name, vals))
  end
  self
end

#ensure_numericality_of(*args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/adlint/util.rb', line 57

def ensure_numericality_of(*args)
  @validators ||= []
  attr_names, opts = _attr_names_in(args), _options_in(args)
  only_int = opts[:only_integer] ? true : false
  min = opts[:min]
  max = opts[:max]
  attr_names.each do |attr_name|
    @validators.push(NumericalityValidator.new(attr_name, only_int,
                                               min, max))
  end
  self
end

#ensure_presence_of(*args) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/adlint/util.rb', line 48

def ensure_presence_of(*args)
  @validators ||= []
  attr_names = _attr_names_in(args)
  attr_names.each do |attr_name|
    @validators.push(PresenceValidator.new(attr_name))
  end
  self
end

#ensure_true_or_false_of(*args) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/adlint/util.rb', line 109

def ensure_true_or_false_of(*args)
  @validators ||= []
  attr_names = _attr_names_in(args)
  attr_names.each do |attr_name|
    @validators.push(TrueOrFalseValidator.new(attr_name))
  end
  self
end

#ensure_validity_of(*args) ⇒ Object

NOTE: Host class must respond to #entity_name.



39
40
41
42
43
44
45
46
# File 'lib/adlint/util.rb', line 39

def ensure_validity_of(*args)
  @validators ||= []
  attr_names = _attr_names_in(args)
  attr_names.each do |attr_name|
    @validators.push(ObjectValidator.new(attr_name))
  end
  self
end

#ensure_with(*args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/adlint/util.rb', line 127

def ensure_with(*args)
  @validators ||= []
  attr_names, opts = _attr_names_in(args), _options_in(args)
  msg = opts[:message] || "is not valid."
  validator = opts[:validator] || lambda { |val| val }
  attr_names.each do |attr_name|
    @validators.push(CustomValidator.new(attr_name, msg, validator))
  end
  self
end

#errorsObject



150
151
152
153
154
155
156
# File 'lib/adlint/util.rb', line 150

def errors
  if self.class.validators
    self.class.validators.map { |validator| validator.errors }.flatten
  else
    []
  end
end

#valid?Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
# File 'lib/adlint/util.rb', line 142

def valid?
  if self.class.validators
    self.class.validators.map { |validator| validator.execute(self) }.all?
  else
    true
  end
end

#validatorsObject



138
139
140
# File 'lib/adlint/util.rb', line 138

def validators
  @validators ||= []
end