Class: Validation::Rule::Matches

Inherits:
Object
  • Object
show all
Defined in:
lib/validation/rule/matches.rb

Overview

Matches rule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher_field) ⇒ Matches

This class should take the field to match with in the constructor:

rule = Validation::Rule::Matches(:password) rule.obj = OpenStruct.new(:password => ‘foo’) rule.valid_value?(‘foo’)



12
13
14
# File 'lib/validation/rule/matches.rb', line 12

def initialize(matcher_field)
  @matcher_field = matcher_field
end

Instance Attribute Details

#obj=(value) ⇒ Object (writeonly)

Sets the attribute obj

Parameters:

  • value

    the value to set the attribute obj to.



5
6
7
# File 'lib/validation/rule/matches.rb', line 5

def obj=(value)
  @obj = value
end

Instance Method Details

#error_keyObject

The error key for this rule



17
18
19
# File 'lib/validation/rule/matches.rb', line 17

def error_key
  :matches
end

#paramsObject

Params is the matcher_field given in the constructor



22
23
24
# File 'lib/validation/rule/matches.rb', line 22

def params
  @matcher_field
end

#valid_value?(value) ⇒ Boolean

Determines if value matches the field given in the constructor

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/validation/rule/matches.rb', line 27

def valid_value?(value)
  matcher_value = @obj.send(@matcher_field)
  matcher_value == value
end