Class: Stove::Validator
- Inherits:
-
Object
- Object
- Stove::Validator
- Includes:
- Mixin::Insideable
- Defined in:
- lib/stove/validator.rb
Instance Attribute Summary collapse
-
#block ⇒ Proc
readonly
The block to execute to see if the validation passes.
-
#id ⇒ Symbol
readonly
The identifier or field this validator runs against.
-
#klass ⇒ ~Class
readonly
The class that created this validator.
Instance Method Summary collapse
-
#initialize(klass, id, &block) ⇒ Validator
constructor
Create a new validator object.
-
#run(cookbook, options = {}) ⇒ Object
Execute this validation in the context of the creating class, inside the given cookbook’s path.
Methods included from Mixin::Insideable
Constructor Details
#initialize(klass, id, &block) ⇒ Validator
Create a new validator object.
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
#block ⇒ Proc (readonly)
The block to execute to see if the validation passes.
24 25 26 |
# File 'lib/stove/validator.rb', line 24 def block @block end |
#id ⇒ Symbol (readonly)
The identifier or field this validator runs against.
17 18 19 |
# File 'lib/stove/validator.rb', line 17 def id @id end |
#klass ⇒ ~Class (readonly)
The class that created this validator.
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.
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, = {}) Stove::Log.info("Running validations for `#{klass.id}.#{id}'") inside(cookbook) do instance = klass.new(cookbook, ) 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 |