Module: CouchResource::Validations::ClassMethods
- Defined in:
- lib/couch_resource/validations.rb
Constant Summary collapse
- DEFAULT_VALIDATION_OPTIONS =
{ :on => :save, :allow_nil => false, :allow_blank => false, :message => nil }.freeze
- ALL_RANGE_OPTIONS =
[ :is, :within, :in, :minimum, :maximum ].freeze
- ALL_NUMERICALITY_CHECKS =
{ :greater_than => '>', :greater_than_or_equal_to => '>=', :equal_to => '==', :less_than => '<', :less_than_or_equal_to => '<=', :odd => 'odd?', :even => 'even?' }.freeze
Instance Method Summary collapse
-
#validates_acceptance_of ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_confirmation_of(*attr_names) This method is not implemented because the validator works for only virtual attributes.
-
#validates_children_of(*attr_names) ⇒ Object
This method is original for CouchResource validation to validate child object in this class.
-
#validates_confirmation_of(*attr_names) ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_confirmation_of(*attr_names).
-
#validates_each(*attrs) ⇒ Object
This method is the same as ActiveRecord::Validations.validates_each(*attr).
-
#validates_exclusion_of(*attr_names) ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_exclusion_of(*attr_names).
-
#validates_format_of(*attr_names) ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_format_of(*attr_names).
-
#validates_inclusion_of(*attr_names) ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_inclusion_of(*attr_names).
-
#validates_length_of(*attrs) ⇒ Object
(also: #validates_size_of)
This method is the same as ActiveRecord::Validations.vaildates_length_of(*attrs) class Person string :first_name, :validates => [ [:lenfth_of, => 1, :maximum => 16] ] end.
-
#validates_numericality_of(*attr_names) ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_numericality_of(*attr_names).
-
#validates_presense_of(*attr_names) ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_presense_of(*attr_names).
-
#validates_uniqueness_of ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_uniqueness_of(*attr_names) This method is not implemented because you should define a new design document for validation of uniqueness to validate.
Instance Method Details
#validates_acceptance_of ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_confirmation_of(*attr_names) This method is not implemented because the validator works for only virtual attributes.
253 254 255 |
# File 'lib/couch_resource/validations.rb', line 253 def validates_acceptance_of raise "Not Implemented" end |
#validates_children_of(*attr_names) ⇒ Object
This method is original for CouchResource validation to validate child object in this class.
class Person
object :children, :is_a => Children, :validates => [
[:children_of, {:on => :create, :allow_nil => true }]
]
end
449 450 451 452 453 454 455 456 457 |
# File 'lib/couch_resource/validations.rb', line 449 def validates_children_of(*attr_names) configuration = { :message => CouchResource::Errors.[:children], :on => :save } configuration.update(attr_names.) validates_each(attr_names, configuration) do |record, attr_name, value| if value.respond_to?(:valid?) record.errors.add(attr_name, configuration[:message] % value) unless value.valid? end end end |
#validates_confirmation_of(*attr_names) ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_confirmation_of(*attr_names)
class Person
string :password, :validates => [:confirmation_of]
end
or
class Person
string :password
validates_confirmation_of :password
end
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/couch_resource/validations.rb', line 235 def validates_confirmation_of(*attr_names) configuration = { :message => CouchResource::Errors.[:confirmation], :on => :save } configuration.update(attr_names.) attr_accessor(*(attr_names.map { |n| "#{n}_confirmation" })) validates_each(attr_names, configuration) do |record, attr_name, value| unless record.send("#{attr_name}_confirmation").nil? or value == record.send("#{attr_name}_confirmation") record.errors.add(attr_name, configuration[:message]) end end end |
#validates_each(*attrs) ⇒ Object
This method is the same as ActiveRecord::Validations.validates_each(*attr)
class Person
string :first_name, :validates => {
[:each,{
:proc => Proc.new do |record, attr, value|
record.errors.add attr, "starts with z." if value[0] == ?z
end
}]
end
or
class Person
string :first_name
validates_each :first_name do |record, attr, value|
record.errors.add attr, "starts with z." if value[0] == ?z
end
end
210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/couch_resource/validations.rb', line 210 def validates_each(*attrs) = attrs..symbolize_keys attrs = attrs.flatten send(validation_method([:on] || :save), ) do |record| attrs.each do |attr| value = record.get_attribute(attr) next if (value.nil? && [:allow_nil]) || (value.blank? && [:allow_blank]) yield record, attr, value end end end |
#validates_exclusion_of(*attr_names) ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_exclusion_of(*attr_names)
385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/couch_resource/validations.rb', line 385 def validates_exclusion_of(*attr_names) configuration = { :message => CouchResource::Errors.[:exclusion], :on => :save } configuration.update(attr_names.) enum = configuration[:in] || configuration[:within] raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?") validates_each(attr_names, configuration) do |record, attr_name, value| record.errors.add(attr_name, configuration[:message] % value) if enum.include?(value) end end |
#validates_format_of(*attr_names) ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_format_of(*attr_names)
359 360 361 362 363 364 365 366 367 368 |
# File 'lib/couch_resource/validations.rb', line 359 def validates_format_of(*attr_names) configuration = { :message => CouchResource::Errors.[:invalid], :on => :save, :with => nil } configuration.update(attr_names.) raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp) validates_each(attr_names, configuration) do |record, attr_name, value| record.errors.add(attr_name, configuration[:message] % value) unless value.to_s =~ configuration[:with] end end |
#validates_inclusion_of(*attr_names) ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_inclusion_of(*attr_names)
371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/couch_resource/validations.rb', line 371 def validates_inclusion_of(*attr_names) configuration = { :message => CouchResource::Errors.[:inclusion], :on => :save } configuration.update(attr_names.) enum = configuration[:in] || configuration[:within] raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?") validates_each(attr_names, configuration) do |record, attr_name, value| record.errors.add(attr_name, configuration[:message] % value) unless enum.include?(value) end end |
#validates_length_of(*attrs) ⇒ Object Also known as: validates_size_of
This method is the same as ActiveRecord::Validations.vaildates_length_of(*attrs)
class Person
string :first_name, :validates => [
[:lenfth_of, {:minimum => 1, :maximum => 16}]
]
end
or
class Person
string :first_name
validates_presense_of :first_name, :minumum => 1, :maximum => 16
end
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/couch_resource/validations.rb', line 295 def validates_length_of(*attrs) # Merge given options with defaults. = { :too_long => CouchResource::Errors.[:too_long], :too_short => CouchResource::Errors.[:too_short], :wrong_length => CouchResource::Errors.[:wrong_length] }.merge(DEFAULT_VALIDATION_OPTIONS) .update(attrs..symbolize_keys) # Ensure that one and only one range option is specified. = ALL_RANGE_OPTIONS & .keys case .size when 0 raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.' when 1 # Valid number of options; do nothing. else raise ArgumentError, 'Too many range options specified. Choose only one.' end # Get range option and value. option = .first option_value = [.first] case option when :within, :in raise ArgumentError, ":#{option} must be a Range" unless option_value.is_a?(Range) too_short = [:too_short] % option_value.begin too_long = [:too_long] % option_value.end validates_each(attrs, ) do |record, attr, value| value = value.split(//) if value.kind_of?(String) if value.nil? or value.size < option_value.begin record.errors.add(attr, too_short) elsif value.size > option_value.end record.errors.add(attr, too_long) end end when :is, :minimum, :maximum raise ArgumentError, ":#{option} must be a nonnegative Integer" unless option_value.is_a?(Integer) and option_value >= 0 # Declare different validations per option. validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" } = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long } = ([:message] || [[option]]) % option_value validates_each(attrs, ) do |record, attr, value| value = value.split(//) if value.kind_of?(String) record.errors.add(attr, ) unless !value.nil? and value.size.method(validity_checks[option])[option_value] end end end |
#validates_numericality_of(*attr_names) ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_numericality_of(*attr_names)
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/couch_resource/validations.rb', line 399 def validates_numericality_of(*attr_names) configuration = { :on => :save, :only_integer => false, :allow_nil => false } configuration.update(attr_names.) = ALL_NUMERICALITY_CHECKS.keys & configuration.keys ( - [ :odd, :even ]).each do |option| raise ArgumentError, ":#{option} must be a number" unless configuration[option].is_a?(Numeric) end validates_each(attr_names,configuration) do |record, attr_name, value| raw_value = record.send("#{attr_name}_before_type_cast") || value next if configuration[:allow_nil] and raw_value.nil? if configuration[:only_integer] unless raw_value.to_s =~ /\A[+-]?\d+\Z/ record.errors.add(attr_name, configuration[:message] || ActiveRecord::Errors.[:not_a_number]) next end raw_value = raw_value.to_i else begin raw_value = Kernel.Float(raw_value.to_s) rescue ArgumentError, TypeError record.errors.add(attr_name, configuration[:message] || ActiveRecord::Errors.[:not_a_number]) next end end .each do |option| case option when :odd, :even record.errors.add(attr_name, configuration[:message] || CouchResource::Errors.[option]) unless raw_value.to_i.method(ALL_NUMERICALITY_CHECKS[option])[] else = configuration[:message] || CouchResource::Errors.[option] = % configuration[option] if configuration[option] record.errors.add(attr_name, ) unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]] end end end end |
#validates_presense_of(*attr_names) ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_presense_of(*attr_names)
class Person
string :first_name, :validates => [:presense_of]
end
or
class Person
string :first_name
validates_presense_of :first_name
end
270 271 272 273 274 275 276 277 278 279 |
# File 'lib/couch_resource/validations.rb', line 270 def validates_presense_of(*attr_names) configuration = { :message => CouchResource::Errors.[:blank], :on => :save } configuration.update(attr_names.) # can't use validates_each here, because it cannot cope with nonexistent attributes, # while errors.add_on_empty can send(validation_method(configuration[:on]), configuration) do |record| record.errors.add_on_blank(attr_names, configuration[:message]) end end |
#validates_uniqueness_of ⇒ Object
This method is the same as ActiveRecord::Validations.vaildates_uniqueness_of(*attr_names) This method is not implemented because you should define a new design document for validation of uniqueness to validate.
354 355 356 |
# File 'lib/couch_resource/validations.rb', line 354 def validates_uniqueness_of raise "Not Implemented" end |