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.



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.



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

def block
  @block
end

#idSymbol (readonly)

The identifier or field this validator runs against.



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

def id
  @id
end

#klass~Class (readonly)

The class that created this validator.



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.



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