Sha256: 69ed2e36fbff975a1379f706624c9699646c6b06c00f6ecfa971057db281a497

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

module Stove
  class Validator
    include Mixin::Insideable
    include Mixin::Loggable

    #
    # The class that created this validator.
    #
    # @return [~Class]
    #
    attr_reader :klass

    #
    # The identifier or field this validator runs against.
    #
    # @return [Symbol]
    #
    attr_reader :id

    #
    # The block to execute to see if the validation passes.
    #
    # @return [Proc]
    #
    attr_reader :block

    #
    # Create a new validator object.
    #
    # @param [~Class] klass
    #   the class that created this validator
    # @param [Symbol] id
    #   the identifier or field this validator runs against
    # @param [Proc] block
    #   the block to execute to see if the validation passes
    #
    def initialize(klass, id, &block)
      @klass   = klass
      @id      = id
      @block   = block
    end

    #
    # Execute this validation in the context of the creating class, inside the
    # given cookbook's path.
    #
    # @param [Cookbook]
    #   the cookbook to run this validation against
    #
    def run(cookbook, options = {})
      log.info("Running validations for #{klass.id}.#{id}")

      inside(cookbook) do
        instance = klass.new(cookbook, options)
        unless result = instance.instance_eval(&block)
          log.debug("Validation failed, result: #{result.inspect}")
          raise Error::ValidationFailed.new(klass.id, id,
            path:   Dir.pwd,
            result: result,
          )
        end
      end

      log.debug("Validation #{id} passed!")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stove-2.0.0.beta.2 lib/stove/validator.rb
stove-2.0.0.beta.1 lib/stove/validator.rb