Module: Puppet_X::Elastic::SortedHash
- Defined in:
- lib/puppet_x/elastic/hash.rb
Class Method Summary collapse
-
.extended(base) ⇒ Object
Upon extension, modify the hash appropriately to render sorted yaml dependent upon whichever way is supported for this version of Puppet/Ruby’s yaml implementation.
Instance Method Summary collapse
-
#each_pair ⇒ Object
Override each_pair with a method that yields key/values in sorted order.
Class Method Details
.extended(base) ⇒ Object
Upon extension, modify the hash appropriately to render sorted yaml dependent upon whichever way is supported for this version of Puppet/Ruby’s yaml implementation.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/puppet_x/elastic/hash.rb', line 8 def self.extended(base) if RUBY_VERSION >= '1.9' # We can sort the hash in Ruby >= 1.9 by recursively # re-inserting key/values in sorted order. Native to_yaml will # call .each and get sorted pairs back. tmp = base.to_a.sort base.clear tmp.each do |key, val| if val.is_a? base.class val.extend Puppet_X::Elastic::SortedHash elsif val.is_a? Array val.map do |elem| if elem.is_a? base.class elem.extend(Puppet_X::Elastic::SortedHash) else elem end end end base[key] = val end else # Otherwise, recurse into the hash to extend all nested # hashes with the sorted each_pair method. # # Ruby < 1.9 doesn't support any notion of sorted hashes, # so we have to expressly monkey patch each_pair, which is # called by ZAML (the yaml library used in Puppet < 4; Puppet # >= 4 deprecates Ruby 1.8) # # Note that respond_to? is used here as there were weird # problems with .class/.is_a? base.merge! base do |_, ov, nv| if ov.respond_to? :each_pair ov.extend Puppet_X::Elastic::SortedHash elsif ov.is_a? Array ov.map do |elem| if elem.respond_to? :each_pair elem.extend Puppet_X::Elastic::SortedHash else elem end end else ov end end end end |
Instance Method Details
#each_pair ⇒ Object
Override each_pair with a method that yields key/values in sorted order.
61 62 63 64 65 |
# File 'lib/puppet_x/elastic/hash.rb', line 61 def each_pair keys.sort.each do |key| yield key, self[key] end end |