Class: Violated::Violator

Inherits:
Object
  • Object
show all
Defined in:
lib/violated/violator.rb

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ Violator

Returns a new instance of Violator.



5
6
7
# File 'lib/violated/violator.rb', line 5

def initialize(field)
  @field = field
end

Instance Method Details

#validatorsObject



21
22
23
# File 'lib/violated/violator.rb', line 21

def validators
  @validators ||= []
end

#violations_for(object) ⇒ Object



25
26
27
28
29
# File 'lib/violated/violator.rb', line 25

def violations_for(object)
  validators.reject { |validator| validator.valid?(object.send(@field)) }.map do |violation|
    Violation.new(@field, violation.reason)
  end
end

#when_not(*validators) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/violated/violator.rb', line 9

def when_not(*validators)
  validators.each do |validator|
    if Array === validator
      validator_type, *args = *validator
    else
      validator_type, args = validator, []
    end

    self.validators << Registry.get(validator_type).new(@field, *args)
  end
end