class Stove::Validator

Attributes

block[R]

The block to execute to see if the validation passes.

@return [Proc]

id[R]

The identifier or field this validator runs against.

@return [Symbol]

klass[R]

The class that created this validator.

@return [~Class]

Public Class Methods

new(klass, id, &block) click to toggle source

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
# File lib/stove/validator.rb, line 36
def initialize(klass, id, &block)
  @klass   = klass
  @id      = id
  @block   = block
end

Public Instance Methods

run(cookbook, options = {}) click to toggle source

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
# File lib/stove/validator.rb, line 49
def run(cookbook, options = {})
  Stove::Log.info("Running validations for `#{klass.id}.#{id}'")

  inside(cookbook) do
    instance = klass.new(cookbook, options)
    unless result = instance.instance_eval(&block)
      Stove::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

  Stove::Log.debug("Validation #{id} passed!")
end