Class: ChefAPI::Validator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-api/validators/base.rb

Direct Known Subclasses

Required, Type

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, options = {}) ⇒ Base

Create anew validator.

Parameters:

  • attribute (Symbol)

    the attribute to apply this validation on

  • options (Hash) (defaults to: {})

    the list of options passed in



23
24
25
26
# File 'lib/chef-api/validators/base.rb', line 23

def initialize(attribute, options = {})
  @attribute = attribute
  @options   = options.is_a?(Hash) ? options : {}
end

Instance Attribute Details

#attributeSymbol (readonly)

Returns the attribute to apply this validation on.

Returns:

  • (Symbol)

    the attribute to apply this validation on



7
8
9
# File 'lib/chef-api/validators/base.rb', line 7

def attribute
  @attribute
end

#optionsHash (readonly)

Returns the hash of additional arguments passed in.

Returns:

  • (Hash)

    the hash of additional arguments passed in



13
14
15
# File 'lib/chef-api/validators/base.rb', line 13

def options
  @options
end

Instance Method Details

#inspectString

The string representation of this validation.

Returns:

  • (String)


67
68
69
# File 'lib/chef-api/validators/base.rb', line 67

def inspect
  "#<#{classname} :#{attribute}>"
end

#keySymbol

Just in case someone forgets to define a key, this will return the class’s underscored name without “validator” as a symbol.

Examples:

FooValidator.new.key #=> :foo

Returns:

  • (Symbol)


37
38
39
40
# File 'lib/chef-api/validators/base.rb', line 37

def key
  name = self.class.name.split("::").last
  Util.underscore(name).to_sym
end

#to_sString

The string representation of this validation.

Returns:

  • (String)


58
59
60
# File 'lib/chef-api/validators/base.rb', line 58

def to_s
  "#<#{classname}>"
end

#validate(resource) ⇒ Object

Execute the validations. This is an abstract class and must be overridden in custom validators.

Parameters:

  • resource (Resource::Base::Base)

    the parent resource to validate against

Raises:



49
50
51
# File 'lib/chef-api/validators/base.rb', line 49

def validate(resource)
  raise Error::AbstractMethod.new(method: "Validators::Base#validate")
end