Class: Bodhi::MatchesValidator

Inherits:
Validator show all
Defined in:
lib/bodhi-slam/validators/matches.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Validator

constantize, #to_sym, #underscore

Constructor Details

#initialize(value) ⇒ MatchesValidator

Returns a new instance of MatchesValidator.



5
6
7
# File 'lib/bodhi-slam/validators/matches.rb', line 5

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/bodhi-slam/validators/matches.rb', line 3

def value
  @value
end

Instance Method Details

#to_optionsObject



23
24
25
# File 'lib/bodhi-slam/validators/matches.rb', line 23

def to_options
  {self.to_sym => @value}
end

#validate(record, attribute, value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bodhi-slam/validators/matches.rb', line 9

def validate(record, attribute, value)
  unless value.nil?

    if value.is_a?(Array)
      unless value.empty?
        record.errors.add(attribute, "must only contain values matching #{@value}") unless value.delete_if{ |v| v.match(@value) }.empty?
      end
    else
      record.errors.add(attribute, "must match #{@value}") unless value.match(@value)
    end

  end
end