Sha256: 7a2731fe6f82abf70b3a2d08a5ac46623946ea735237006927d1f039d0e7de7f

Contents?: true

Size: 1.62 KB

Versions: 25

Compression:

Stored size: 1.62 KB

Contents

module Stove
  class Validator
    include Logify

    include Mixin::Insideable

    #
    # 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}")

          # Convert the class and id to their magical equivalents
          error = Error.const_get("#{Util.camelize(klass.id)}#{Util.camelize(id)}ValidationFailed")
          raise error.new(path: Dir.pwd, result: result)
        end
      end

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

Version data entries

25 entries across 25 versions & 3 rubygems

Version Path
chef-stove-7.1.5 lib/stove/validator.rb
stove-7.1.5 lib/stove/validator.rb
chef-stove-7.1.1 lib/stove/validator.rb
stove-7.1.0 lib/stove/validator.rb
stove-7.0.1 lib/stove/validator.rb
stove-7.0.0 lib/stove/validator.rb
stove-6.1.1 lib/stove/validator.rb
stove-6.1.0 lib/stove/validator.rb
stove-6.0.0 lib/stove/validator.rb
stove-5.2.0 lib/stove/validator.rb
stove-5.1.0 lib/stove/validator.rb
stove-5.0.0 lib/stove/validator.rb
stove-4.1.1 lib/stove/validator.rb
stove-4.1.0 lib/stove/validator.rb
stove-4.0.0 lib/stove/validator.rb
stove-3.2.8 lib/stove/validator.rb
wood-stove-3.2.9000 lib/stove/validator.rb
stove-3.2.7 lib/stove/validator.rb
stove-3.2.6 lib/stove/validator.rb
stove-3.2.5 lib/stove/validator.rb