Class: Stove::Validator

Inherits:
Object
  • Object
show all
Includes:
Logify, Mixin::Insideable
Defined in:
lib/stove/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Insideable

#inside

Constructor Details

#initialize(klass, id, &block) ⇒ Validator

Create a new validator object.

Parameters:

  • klass (~Class)

    the class that created this validator

  • id (Symbol)

    the identifier or field this validator runs against

  • block (Proc)

    the block to execute to see if the validation passes



38
39
40
41
42
# File 'lib/stove/validator.rb', line 38

def initialize(klass, id, &block)
  @klass   = klass
  @id      = id
  @block   = block
end

Instance Attribute Details

#blockProc (readonly)

The block to execute to see if the validation passes.

Returns:

  • (Proc)


26
27
28
# File 'lib/stove/validator.rb', line 26

def block
  @block
end

#idSymbol (readonly)

The identifier or field this validator runs against.

Returns:

  • (Symbol)


19
20
21
# File 'lib/stove/validator.rb', line 19

def id
  @id
end

#klass~Class (readonly)

The class that created this validator.

Returns:

  • (~Class)


12
13
14
# File 'lib/stove/validator.rb', line 12

def klass
  @klass
end

Instance Method Details

#run(cookbook, options = {}) ⇒ Object

Execute this validation in the context of the creating class, inside the given cookbook’s path.

Parameters:

  • the (Cookbook)

    cookbook to run this validation against



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/stove/validator.rb', line 51

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