Module: ActiveRecord::Validations::ClassMethods
- Defined in:
- lib/adaptation/validateable.rb
Instance Method Summary collapse
-
#validates_as_email(*attr_names) ⇒ Object
Validates whether the value of the specified xml attribute/element has a valid email format.
-
#validates_presence_of(*attr_names) ⇒ Object
Validates whether the specified xml attribute/element is present (not nil or blank).
-
#validates_value_of(*attr_names) ⇒ Object
Validates whether the value of the specified xml attribute/element is the expected one.
Instance Method Details
#validates_as_email(*attr_names) ⇒ Object
Validates whether the value of the specified xml attribute/element has a valid email format.
Example:
class Contact < Adaptation::Message
validates_as_email :email
end
c = Contact.new('<contact email="[email protected]">...</contact>')
c.valid? # -> true
c.email = "nomail"
c.valid? # -> false
210 211 212 213 214 215 216 217 218 |
# File 'lib/adaptation/validateable.rb', line 210 def validates_as_email(*attr_names) configuration = { :message => 'is an invalid email', :with => RFC822::EmailAddress, :allow_nil => true } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) validates_format_of attr_names, configuration end |
#validates_presence_of(*attr_names) ⇒ Object
Validates whether the specified xml attribute/element is present (not nil or blank).
Example 1:
class Leftwing < Adaptation::Message
validates_presence_of :side
end
lw = Leftwing.new("<leftwing side=\"left\"/>")
lw.valid? # -> true
lw = Leftwing.new("<leftwing noside=\"left\"/>")
lw.valid? # -> false
Example 2:
class Bird < Adaptation::Message
validates_presence_of :side, :in => "birds.wings"
end
b = Bired.new('<birds><bird><wings><wing side="left"/><wing side="right"/></wings></bird></birds>')
b.valid? # -> true
b = Bired.new('<birds><bird><wings><wing side="left"/><wing noside="right"/></wings></bird></birds>')
b.valid? # -> false
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/adaptation/validateable.rb', line 80 def validates_presence_of(*attr_names) configuration = { :message => 'cannot be blank' } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) if configuration[:in].nil? validates_each(attr_names, configuration) do |record, attr_name, value| string = record.send(attr_names[0].to_sym) unless string.nil? string = string.content if string.is_a?(Adaptation::Message) end if string.blank? record.errors.add(attr_name, configuration[:message]) end end else validates_each(attr_names, configuration) do |record, attr_name, value| missing = false configuration_in = configuration[:in].to_s elements = configuration_in.to_s.split('.') subelement = record while elements.length > 1 subelement = record.send(elements[0].to_sym) elements.slice!(0) end if !subelement.is_a?(Array) subelement.send(elements[0].to_sym).each do |s| string = s.send(attr_names[0].to_sym) unless string.nil? string = string.content unless string.is_a?(String) end if string.blank? missing = true break end end else subelement.each do |sub| sub.send(elements[0].to_sym).each do |s| string = s.send(attr_names[0].to_sym) unless string.nil? string = string.content unless string.is_a?(String) end if string.blank? missing = true break end end break if missing end end record.errors.add(attr_name, configuration[:message]) if missing end end end |
#validates_value_of(*attr_names) ⇒ Object
Validates whether the value of the specified xml attribute/element is the expected one.
Example 1:
<leftwing side="left"/>
class Leftwing < Adaptation::Message
validates_value_of :side, "left"
end
Example 2:
<bird><wings><wing side="left"/><wing side="right"/></wings></bird>
class Bird < Adaptation::Message
validates_value_of :side, "left", :in => :wings
end
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/adaptation/validateable.rb', line 156 def validates_value_of(*attr_names) configuration = { :message => 'value doesn\'t exist' } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) if configuration[:in].nil? validates_each(attr_names, configuration) do |record, attr_name, value| string = record.send(attr_names[0].to_sym) unless string.nil? string = string.content unless string.is_a?(String) end if (attr_names[1].to_s != string) record.errors.add(attr_name, configuration[:message]) end end else validates_each(attr_names, configuration) do |record, attr_name, value| found = false configuration_in = configuration[:in].to_s elements = configuration_in.to_s.split('.') subelement = record while elements.length > 1 subelement = record.send(elements[0].to_sym) elements.slice!(0) end subelement.send(elements[0].to_sym).each do |s| string = s.send(attr_names[0].to_sym) unless string.nil? string = string.content unless string.is_a?(String) end if (attr_names[1].to_s == string) found = true break end end record.errors.add(attr_name, configuration[:message]) unless found end end end |