Class: Stove::Validator

Inherits:
Object
  • Object
show all
Includes:
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



36
37
38
39
40
# File 'lib/stove/validator.rb', line 36

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)


24
25
26
# File 'lib/stove/validator.rb', line 24

def block
  @block
end

#idSymbol (readonly)

The identifier or field this validator runs against.

Returns:

  • (Symbol)


17
18
19
# File 'lib/stove/validator.rb', line 17

def id
  @id
end

#klass~Class (readonly)

The class that created this validator.

Returns:

  • (~Class)


10
11
12
# File 'lib/stove/validator.rb', line 10

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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