module Trifle::Stats::Mixins::Packer::ClassMethods

Public Instance Methods

deep_merge(this_hash, other_hash, &block) click to toggle source
# File lib/trifle/stats/mixins/packer.rb, line 34
def deep_merge(this_hash, other_hash, &block)
  deep_merge!(this_hash.dup, other_hash, &block)
end
deep_merge!(this_hash, other_hash, &block) click to toggle source
# File lib/trifle/stats/mixins/packer.rb, line 38
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
pack(hash:, prefix: nil) click to toggle source
# File lib/trifle/stats/mixins/packer.rb, line 12
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
unpack(hash:) click to toggle source
# File lib/trifle/stats/mixins/packer.rb, line 25
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