Sha256: 9c7cc46ed70382873a5ad829a858d99fcbc14b3cac61823c544358d901ccc1f0

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

# -*- encoding: utf-8 -*-

module Vanguard

  # Result of a resource validation
  class Result
    include Adamantium::Flat, Enumerable, Equalizer.new(:violations, :validator)

    # Test if result is valid
    #
    # @return [true]
    #   if no rules where violated
    #
    # @return [false]
    #   otherwise
    #
    # @api private
    #
    def valid?
      violations.empty?
    end

    alias_method :success?, :valid?

    # Return violations for resource
    #
    # @param [Resource] resource
    #
    # @api private
    #
    def violations
      validator.rules.each_with_object(Set.new) do |rule, violations|
        violations.merge(rule.violations(resource))
      end
    end
    memoize :violations

    # The validation output
    #
    # @return [Object, Set<Violation>]
    #   the valid resource, or a set of violations
    #
    # @api private
    #
    def output
      valid? ? resource : violations
    end

    # Return violations on attribute name
    #
    # @param [Symbol] attribute_name
    #
    # @return [Enumerable<Violation>]
    #
    # @api private
    #
    def on(attribute_name)
      violations.select { |violation| violation.attribute_name == attribute_name }
    end

    # Return resource
    #
    # @return [Resource]
    #
    # @api private
    #
    attr_reader :resource

    # Return validator
    #
    # @return [Validator]
    #
    # @api private
    #
    attr_reader :validator

  private

    # Initialize object
    #
    # @param [Validator] validator
    # @param [Resource] resource
    #
    # @return [undefined]
    #
    # @api private
    #
    def initialize(validator, resource)
      @validator, @resource = validator, resource
    end

  end # class ViolationSet
end # module Vanguard

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vanguard-0.0.5 lib/vanguard/result.rb
vanguard-0.0.4 lib/vanguard/result.rb
vanguard-0.0.3 lib/vanguard/result.rb