Sha256: 9b88e1b1ff2194fa40f312d725454a8e09f64c8a78a5171d2cb8c5d723c8432f

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

module BB
  # Hash utilities.
  module Hash
    class << self
      # Symbolize all top level keys.
      #
      # @param [Hash] hash Input hash
      # @return [Hash] Output hash (with symbolized keys)
      def symbolize_keys(hash)
        hash.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
      end

      # Recursively flatten a hash to property-style format.
      # This is a lossy conversion and should only be used for display-purposes.
      #
      # @example
      #   input = { :a => { :b => :c } }
      #   BB::Hash.flatten_prop_style(input)
      #   => {"a.b"=>:c}
      #
      # @example
      #   input = { :a => { :b => [:c, :d, :e] } }
      #   BB::Hash.flatten_prop_style(input)
      #   => {"a.b"=>"c,d,e"}
      #
      # @param [Hash] input Input hash
      # @param [Hash] opts Options
      # @option opts [String] :delimiter
      #   Key delimiter (Default: '.')
      # @param [Hash] output (leave this blank)
      # @return [Hash] Output hash (flattened)
      def flatten_prop_style(input={}, opts={}, output={})
        input.each do |key, value|
          key = opts[:prefix].nil? ? "#{key}" : "#{opts[:prefix]}#{opts[:delimiter]||'.'}#{key}"
          case value
          when ::Hash
            flatten_prop_style(value, opts.merge({:prefix => key}), output)
          when Array
            output[key] = value.join(',')
          else
            output[key] = value
          end
        end
        output
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
blackbox-1.1.2 lib/blackbox/hash.rb
blackbox-1.1.1 lib/blackbox/hash.rb
blackbox-1.1.0 lib/blackbox/hash.rb
blackbox-1.0.1 lib/blackbox/hash.rb