Class: DataMapper::Validations::GenericValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-validations/validators/generic_validator.rb

Overview

All validators extend this base class. Validators must:

  • Implement the initialize method to capture its parameters, also calling super to have this parent class capture the optional, general :if and :unless parameters.

  • Implement the call method, returning true or false. The call method provides the validation logic.

Author:

  • Guy van den Berg

Since:

  • 0.9

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name, options = {}) ⇒ GenericValidator

Note:

All additional key/value pairs are passed through to the validator that is sub-classing this GenericValidator

Construct a validator. Capture the :if and :unless clauses when present.

Parameters:

  • field (String, Symbol)

    The property specified for validation.

  • [Symbol, (Hash)

    a customizable set of options

Since:

  • 0.9



37
38
39
40
41
42
43
# File 'lib/dm-validations/validators/generic_validator.rb', line 37

def initialize(field_name, options = {})
  @field_name           = field_name
  @options              = DataMapper::Ext::Hash.except(options, :if, :unless)
  @if_clause            = options[:if]
  @unless_clause        = options[:unless]
  @humanized_field_name = DataMapper::Inflector.humanize(@field_name)
end

Instance Attribute Details

#field_nameObject (readonly)

Since:

  • 0.9



17
18
19
# File 'lib/dm-validations/validators/generic_validator.rb', line 17

def field_name
  @field_name
end

#humanized_field_nameObject (readonly)

Since:

  • 0.9



17
18
19
# File 'lib/dm-validations/validators/generic_validator.rb', line 17

def humanized_field_name
  @humanized_field_name
end

#if_clauseObject

Since:

  • 0.9



16
17
18
# File 'lib/dm-validations/validators/generic_validator.rb', line 16

def if_clause
  @if_clause
end

#optionsObject (readonly)

Since:

  • 0.9



17
18
19
# File 'lib/dm-validations/validators/generic_validator.rb', line 17

def options
  @options
end

#unless_clauseObject

Since:

  • 0.9



16
17
18
# File 'lib/dm-validations/validators/generic_validator.rb', line 16

def unless_clause
  @unless_clause
end

Instance Method Details

#==(other) ⇒ Object

Returns true if validators are equal

Note that this intentionally do validate options equality

even though it is hard to imagine a situation when multiple validations will be used on the same field with the same conditions but different options, it happens to be the case every once in a while with inferred validations for strings/text and explicitly given validations with different option (usually as Range vs. max limit for inferred validation)

Since:

  • 0.9



153
154
155
156
157
158
159
# File 'lib/dm-validations/validators/generic_validator.rb', line 153

def ==(other)
  self.class == other.class &&
  self.field_name == other.field_name &&
  self.if_clause == other.if_clause &&
  self.unless_clause == other.unless_clause &&
  self.options == other.options
end

#add_error(target, message, field_name = :general) ⇒ Object

Add an error message to a target resource. If the error corresponds to a specific field of the resource, add it to that field, otherwise add it as a :general message.

Parameters:

  • target (Object)

    The resource that has the error.

  • message (String)

    The message to add.

  • field_name (Symbol) (defaults to: :general)

    The name of the field that caused the error.

Since:

  • 0.9



58
59
60
61
# File 'lib/dm-validations/validators/generic_validator.rb', line 58

def add_error(target, message, field_name = :general)
  # TODO: should the field_name for a general message be :default???
  target.errors.add(field_name, message)
end

#call(target) ⇒ Boolean

Call the validator. “call” is used so the operation is BoundMethod and Block compatible. This must be implemented in all concrete classes.

Parameters:

  • target (Object)

    The resource that the validator must be called against.

Returns:

  • (Boolean)

    true if valid, otherwise false.

Raises:

  • (NotImplementedError)

Since:

  • 0.9



73
74
75
# File 'lib/dm-validations/validators/generic_validator.rb', line 73

def call(target)
  raise NotImplementedError, "#{self.class}#call must be implemented"
end

#evaluate_conditional_clause(target, clause) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.9



99
100
101
102
103
104
105
# File 'lib/dm-validations/validators/generic_validator.rb', line 99

def evaluate_conditional_clause(target, clause)
  if clause.kind_of?(Symbol)
    target.__send__(clause)
  elsif clause.respond_to?(:call)
    clause.call(target)
  end
end

#execute?(target) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines if this validator should be run against the target by evaluating the :if and :unless clauses optionally passed while specifying any validator.

Parameters:

  • target (Object)

    The resource that we check against.

Returns:

  • (Boolean)

    true if should be run, otherwise false.

Since:

  • 0.9



88
89
90
91
92
93
94
95
96
# File 'lib/dm-validations/validators/generic_validator.rb', line 88

def execute?(target)
  if unless_clause = self.unless_clause
    !evaluate_conditional_clause(target, unless_clause)
  elsif if_clause = self.if_clause
    evaluate_conditional_clause(target, if_clause)
  else
    true
  end
end

#inspectObject Also known as: to_s

Since:

  • 0.9



161
162
163
# File 'lib/dm-validations/validators/generic_validator.rb', line 161

def inspect
  "<##{self.class.name} @field_name='#{@field_name}' @if_clause=#{@if_clause.inspect} @unless_clause=#{@unless_clause.inspect} @options=#{@options.inspect}>"
end

#optional?(value) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Test the value to see if it is blank or nil, and if it is allowed. Note that allowing blank without explicitly denying nil allows nil values, since nil.blank? is true.

Parameters:

  • value (Object)

    The value to test.

Returns:

  • (Boolean)

    true if blank/nil is allowed, and the value is blank/nil.

Since:

  • 0.9



129
130
131
132
133
134
135
136
# File 'lib/dm-validations/validators/generic_validator.rb', line 129

def optional?(value)
  if value.nil?
    @options[:allow_nil] ||
      (@options[:allow_blank] && !@options.has_key?(:allow_nil))
  elsif DataMapper::Ext.blank?(value)
    @options[:allow_blank]
  end
end

#set_optional_by_default(default = true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set the default value for allow_nil and allow_blank

Parameters:

  • default (Boolean) (defaults to: true)

    value

Since:

  • 0.9



112
113
114
115
116
# File 'lib/dm-validations/validators/generic_validator.rb', line 112

def set_optional_by_default(default = true)
  [ :allow_nil, :allow_blank ].each do |key|
    @options[key] = true unless options.key?(key)
  end
end