class Puppet::Pops::Lookup::LookupKey
@api private
Constants
- LOOKUP_OPTIONS
Attributes
Public Class Methods
# File lib/puppet/pops/lookup/lookup_key.rb 11 def initialize(key) 12 segments = split_key(key) { |problem| Puppet::DataBinding::LookupError.new(_("%{problem} in key: '%{key}'") % { problem: problem, key: key }) } 13 root_key = segments.shift.freeze 14 qual_index = root_key.index(DOUBLE_COLON) 15 16 @key = key 17 @module_name = qual_index.nil? ? nil : root_key[0..qual_index-1].freeze 18 @root_key = root_key 19 @segments = segments.empty? ? nil : segments.freeze 20 end
Public Instance Methods
# File lib/puppet/pops/lookup/lookup_key.rb 22 def dig(lookup_invocation, value) 23 @segments.nil? ? value : sub_lookup(@key, lookup_invocation, @segments, value) 24 end
# File lib/puppet/pops/lookup/lookup_key.rb 83 def eql?(v) 84 v.is_a?(LookupKey) && @key == v.to_s 85 end
# File lib/puppet/pops/lookup/lookup_key.rb 88 def hash 89 @key.hash 90 end
Prunes a found root value with respect to subkeys in this key. The given value is returned untouched if this key has no subkeys. Otherwise an attempt is made to create a Hash or Array that contains only the path to the appointed value and that value.
If subkeys exists and no value is found, then this method will return `nil`, an empty `Array` or an empty `Hash` to enable further merges to be applied. The returned type depends on the given value.
@param value [Object] the value to prune @return the possibly pruned value
# File lib/puppet/pops/lookup/lookup_key.rb 35 def prune(value) 36 if @segments.nil? 37 value 38 else 39 pruned = @segments.reduce(value) do |memo, segment| 40 memo.is_a?(Hash) || memo.is_a?(Array) && segment.is_a?(Integer) ? memo[segment] : nil 41 end 42 if pruned.nil? 43 case value 44 when Hash 45 EMPTY_HASH 46 when Array 47 EMPTY_ARRAY 48 else 49 nil 50 end 51 else 52 undig(pruned) 53 end 54 end 55 end
# File lib/puppet/pops/lookup/lookup_key.rb 74 def to_a 75 unless instance_variable_defined?(:@all_segments) 76 a = [@root_key] 77 a += @segments unless @segments.nil? 78 @all_segments = a.freeze 79 end 80 @all_segments 81 end
# File lib/puppet/pops/lookup/lookup_key.rb 92 def to_s 93 @key 94 end
Create a structure that can be dug into using the subkeys of this key in order to find the given value. If this key has no subkeys, the value is returned.
@param value [Object] the value to wrap in a structure in case this value has subkeys @return [Object] the possibly wrapped value
# File lib/puppet/pops/lookup/lookup_key.rb 62 def undig(value) 63 @segments.nil? ? value : segments.reverse.reduce(value) do |memo, segment| 64 if segment.is_a?(Integer) 65 x = [] 66 x[segment] = memo 67 else 68 x = { segment => memo } 69 end 70 x 71 end 72 end