Class: Vanguard::Result

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

Overview

Result of a resource validation

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resourceResource (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 resource

Returns:

  • (Resource)


67
68
69
# File 'lib/vanguard/result.rb', line 67

def resource
  @resource
end

#validatorValidator (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 validator

Returns:



75
76
77
# File 'lib/vanguard/result.rb', line 75

def validator
  @validator
end

Instance Method Details

#on(attribute_name) ⇒ Enumerable<Violation>

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 violations on attribute name

Parameters:

  • attribute_name (Symbol)

Returns:



57
58
59
# File 'lib/vanguard/result.rb', line 57

def on(attribute_name)
  violations.select { |violation| violation.attribute_name == attribute_name }
end

#outputObject, Set<Violation>

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.

The validation output

Returns:

  • (Object, Set<Violation>)

    the valid resource, or a set of violations



45
46
47
# File 'lib/vanguard/result.rb', line 45

def output
  valid? ? resource : violations
end

#valid?true, false Also known as: success?

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.

Test if result is valid

Returns:

  • (true)

    if no rules where violated

  • (false)

    otherwise



19
20
21
# File 'lib/vanguard/result.rb', line 19

def valid?
  violations.empty?
end

#violationsObject

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 violations for resource

Parameters:

  • resource (Resource)


31
32
33
34
35
# File 'lib/vanguard/result.rb', line 31

def violations
  validator.rules.each_with_object(Set.new) do |rule, violations|
    violations.merge(rule.violations(resource))
  end
end