Class: Chef::Attribute::Validator::Check::MaxChildren

Inherits:
Chef::Attribute::Validator::Check show all
Defined in:
lib/chef-attribute-validator/checks/max_children.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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chef-attribute-validator/checks/max_children.rb', line 15

def check(attrset)
  violations = []

  # So if you say there must be at most one child, and the attributes don't even exist, is that a violation?
  # I say no; if you care if the attributes exist, use required.
  attrset.each do |path, value|
    if value.kind_of?(Array) || value.kind_of?(Mash)
      if value.size > check_arg
        violations.push Chef::Attribute::Validator::Violation.new(rule_name, path, "Attribute should have at most #{check_arg} child values, but has #{value.size}")
      end
    else
      violations.push Chef::Attribute::Validator::Violation.new(rule_name, path, "Attribute should have at most #{check_arg} child values, but is of the wrong type - should be hash or array.")
    end
  end
  violations

end

#validate_check_argObject



9
10
11
12
13
# File 'lib/chef-attribute-validator/checks/max_children.rb', line 9

def validate_check_arg
  unless check_arg.kind_of?(::Fixnum)
    raise "Bad 'max_children' check argument '#{check_arg}' for rule '#{rule_name}' - expected an integer."
  end
end