Class: Chef::Attribute::Validator::Check::Type

Inherits:
Chef::Attribute::Validator::Check show all
Defined in:
lib/chef-attribute-validator/checks/type.rb

Instance Attribute Summary

Attributes inherited from Chef::Attribute::Validator::Check

#check_arg, #path_spec, #rule_name

Instance Method Summary collapse

Methods inherited from Chef::Attribute::Validator::Check

#initialize, list_check_types, make

Constructor Details

This class inherits a constructor from Chef::Attribute::Validator::Check

Instance Method Details

#check(attrset) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chef-attribute-validator/checks/type.rb', line 23

def check(attrset)
  violations = []

  klasses = {
    'string'  => [ String ],
    'number'  => [ Integer, Float ],
    'boolean' => [ TrueClass, FalseClass ],
    'array'   => [ Array ],
    'hash'    => [ Mash ],
    'nil'     => [ NilClass ],
  }

  attrset.each do |path, value|
    unless klasses[check_arg].any? {|k| value.kind_of?(k) }
      violations.push Chef::Attribute::Validator::Violation.new(rule_name, path, "Attribute's value is '#{value}', which does not appear to be the right type - expected '#{check_arg}'")
    end
  end

  violations
end

#validate_check_argObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/chef-attribute-validator/checks/type.rb', line 9

def validate_check_arg            
  expected = [
              'string',
              'number',
              'boolean',
              'hash',
              'array',
             ]
       
  unless expected.include?(check_arg)
    raise "Bad 'type' check argument '#{checkarg}' for rule '#{rule_name}' - expected one of #{expected.join(',')}"
  end
end