class ChefDK::Policyfile::AttributeMergeChecker

Attributes

attribute_hash_infos[R]

@return [Array<AttributeHashInfo>] A list of attributes and who they were provided by

Public Class Methods

new() click to toggle source
# File lib/chef-dk/policyfile/attribute_merge_checker.rb, line 62
def initialize
  @attribute_hash_infos = []
end

Public Instance Methods

check!() click to toggle source

Check all added attributes for conflicts. Different sources can provide the same attribute if they have the same value. Otherwise, it is considered a conflict

@raise ConflictError if there are conflicting attributes

# File lib/chef-dk/policyfile/attribute_merge_checker.rb, line 80
def check!
  check_struct = Mash.new
  attribute_hash_infos.each do |attr_hash_info|
    fill!(check_struct, attr_hash_info.source_name, "", attr_hash_info.hash)
  end
end
with_attributes(source_name, hash) click to toggle source

Add a hash of attributes to the set of attributes that will be compared for conflicts

@param source_name [String] Where the attributes came from @param hash [Hash] attributes from source_name

# File lib/chef-dk/policyfile/attribute_merge_checker.rb, line 71
def with_attributes(source_name, hash)
  attribute_hash_infos << AttributeHashInfo.new(source_name, hash)
end

Private Instance Methods

fill!(acc, source_name, path, hash) click to toggle source
# File lib/chef-dk/policyfile/attribute_merge_checker.rb, line 89
def fill!(acc, source_name, path, hash)
  hash.each do |(key, val)|
    new_path = "#{path}[#{key}]"
    if val.is_a?(Hash)
      acc[key] ||= Mash.new
      fill!(acc[key], source_name, new_path, val)
    else
      if acc[key].nil?
        acc[key] = Leaf.new(source_name, val)
      else
        leaf = acc[key]
        if leaf.val != val
          raise ConflictError.new(new_path, [leaf.provided_by, source_name])
        end
      end
    end
  end
end