Sha256: 3582e2c8e736eaa8a52af174613ca6975dafb0ae68f2c5380d2589841eabea2d
Contents?: true
Size: 1.38 KB
Versions: 3
Compression:
Stored size: 1.38 KB
Contents
# frozen_string_literal: true module Trifle module Ruby module Mixins module Packer def self.included(base) base.extend ClassMethods end module ClassMethods def pack(hash:, prefix: nil) hash.inject({}) do |o, (k, v)| key = [prefix, k].compact.join('.') if v.instance_of?(Hash) o.update( pack(hash: v, prefix: key) ) else o.update({ key => v }) end end end def unpack(hash:) hash.inject({}) do |out, (key, v)| deep_merge( out, key.split('.').reverse.inject(v.to_i) { |o, k| { k => o } } ) end end def deep_merge(this_hash, other_hash, &block) deep_merge!(this_hash.dup, other_hash, &block) end def deep_merge!(this_hash, other_hash, &block) this_hash.merge!(other_hash) do |key, this_val, other_val| if this_val.is_a?(Hash) && other_val.is_a?(Hash) deep_merge(this_val, other_val, &block) elsif block_given? block.call(key, this_val, other_val) else other_val end end end end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
trifle-ruby-3.0.4 | lib/trifle/ruby/mixins/packer.rb |
trifle-ruby-3.0.1 | lib/trifle/ruby/mixins/packer.rb |
trifle-ruby-3.0.0 | lib/trifle/ruby/mixins/packer.rb |