Class: Vanguard::Validator

Inherits:
Object
  • Object
show all
Includes:
Adamantium, Enumerable
Defined in:
lib/vanguard/validator.rb,
lib/vanguard/validator/builder.rb

Overview

Validator compsed from many rules

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rulesSet<Rule> (readonly)

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.

Return rules

Returns:



58
59
60
# File 'lib/vanguard/validator.rb', line 58

def rules
  @rules
end

Class Method Details

.build(&block) ⇒ Validator

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.

Build validator

Returns:



24
25
26
# File 'lib/vanguard/validator.rb', line 24

def self.build(&block)
  Builder.new(&block).validator
end

Instance Method Details

#add(rule) ⇒ Validator

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.

Return rules set with rule added

Parameters:

Returns:



36
37
38
# File 'lib/vanguard/validator.rb', line 36

def add(rule)
  self.class.new(rules.dup << rule)
end

#call(resource) ⇒ Result

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.

Run validator on resource

Parameters:

  • resource (Resource)

Returns:



14
15
16
# File 'lib/vanguard/validator.rb', line 14

def call(resource)
  Result.new(self, resource)
end

#eachself, Enumerator

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.

Enumerate rules

Returns:

  • (self)

    if block given

  • (Enumerator)

    otherwise



70
71
72
73
74
75
76
# File 'lib/vanguard/validator.rb', line 70

def each
  return to_enum unless block_given?

  rules.each { |rule| yield rule }

  self
end

#on(attribute_name) ⇒ Enumerable<Rule>

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.

Return rules on attribute name

Parameters:

  • attribute_name (Symbol)

Returns:

  • (Enumerable<Rule>)


48
49
50
# File 'lib/vanguard/validator.rb', line 48

def on(attribute_name)
  rules.select { |rule| rule.attribute_name == attribute_name }
end