class CouchRest::Validation::RequiredFieldValidator

@author Guy van den Berg @since 0.9

Public Instance Methods

call(target) click to toggle source
# File lib/couchrest/validation/validators/required_field_validator.rb, line 38
def call(target)
  value = target.validation_property_value(field_name)
  property = target.validation_property(field_name.to_s)
  return true if present?(value, property)

  error_message = @options[:message] || default_error(property)
  add_error(target, error_message, field_name)

  false
end

Protected Instance Methods

boolean_type?(property) click to toggle source

Is property a boolean property?

Returns true for Boolean, ParanoidBoolean, TrueClass, etc. properties. Returns false for other property types. Returns false for non-properties.

# File lib/couchrest/validation/validators/required_field_validator.rb, line 68
def boolean_type?(property)
  property ? property.type == 'Boolean' : false
end
default_error(property) click to toggle source
# File lib/couchrest/validation/validators/required_field_validator.rb, line 58
def default_error(property)
  actual = boolean_type?(property) ? :nil : :blank
  ValidationErrors.default_error_message(actual, field_name)
end
present?(value, property) click to toggle source

Boolean property types are considered present if non-nil. Other property types are considered present if non-blank. Non-properties are considered present if non-blank.

# File lib/couchrest/validation/validators/required_field_validator.rb, line 54
def present?(value, property)
  boolean_type?(property) ? !value.nil? : !value.blank?
end

Public Class Methods

new(field_name, options={}) click to toggle source
# File lib/couchrest/validation/validators/required_field_validator.rb, line 33
def initialize(field_name, options={})
  super
  @field_name, @options = field_name, options
end