Class: MiniDefender::Rules::Different

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/different.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiniDefender::Rule

#active?, available?, #bails?, #coerce, #default_value, #defaults?, #error_message, #excluded?, #force_coerce?, #implicit?, #priority, #stops?, #with_message

Constructor Details

#initialize(other_field) ⇒ Different

Returns a new instance of Different.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
# File 'lib/mini_defender/rules/different.rb', line 4

def initialize(other_field)
  raise ArgumentError, 'Other field must be a string' unless other_field.is_a?(String)

  @found = false
  @other_field = other_field
end

Class Method Details

.make(args) ⇒ Object



15
16
17
# File 'lib/mini_defender/rules/different.rb', line 15

def self.make(args)
  self.new(args[0])
end

.signatureObject



11
12
13
# File 'lib/mini_defender/rules/different.rb', line 11

def self.signature
  'different'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/mini_defender/rules/different.rb', line 24

def message(attribute, value, validator)
  if @found
    "The field does not match \"#{@other}\"."
  else
    "The field must match #{@other_field}."
  end
end

#passes?(attribute, value, validator) ⇒ Boolean

Returns:



19
20
21
22
# File 'lib/mini_defender/rules/different.rb', line 19

def passes?(attribute, value, validator)
  @found, @other = validator.data.key?(@other_field), validator.data[@other_field]
  @found && value == @other
end