Class: ChefAPI::Validator::Type

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

Instance Attribute Summary collapse

Attributes inherited from Base

#attribute, #options

Instance Method Summary collapse

Methods inherited from Base

#inspect, #key, #to_s

Constructor Details

#initialize(attribute, type) ⇒ Type

Overload the super method to capture the type attribute in the options hash.



9
10
11
12
# File 'lib/chef-api/validators/type.rb', line 9

def initialize(attribute, type)
  super
  @types = Array(type)
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



3
4
5
# File 'lib/chef-api/validators/type.rb', line 3

def types
  @types
end

Instance Method Details

#validate(resource) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/chef-api/validators/type.rb', line 14

def validate(resource)
  value = resource._attributes[attribute]

  if value && !types.any? { |type| value.is_a?(type) }
    short_name = type.to_s.split('::').last
    resource.errors.add(attribute, "must be a kind of #{short_name}")
  end
end