Sha256: 480efc6974213c401d6cc56a8f82f24a6aa3c7738e92382fdbc063024341d131

Contents?: true

Size: 926 Bytes

Versions: 4

Compression:

Stored size: 926 Bytes

Contents

class Hash

  # sorted yaml
  def to_yaml( opts = {} )
    YAML::quick_emit( object_id, opts ) do |out|
      out.map( taguri, to_yaml_style ) do |map|
        sorted_keys = keys
        sorted_keys = begin
          sorted_keys.sort
        rescue
          sorted_keys.sort_by {|k| k.to_s} rescue sorted_keys
        end

        sorted_keys.each do |k|
          map.add( k, fetch(k) )
        end
      end
    end
  end

  # active_support hash key functions
  def symbolize_keys!
    self.replace(self.symbolize_keys)
  end

  def symbolize_keys
    inject({}) do |options, (key, value)|
      options[(key.to_sym rescue key) || key] = value
      options
    end
  end

  def recursively_symbolize_keys!
    self.symbolize_keys!
    self.values.each do |v|
      if v.is_a? Hash
        v.recursively_symbolize_keys!
      elsif v.is_a? Array
        v.recursively_symbolize_keys!
      end
    end
    self
  end

end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
oct-0.3.3 lib/oct/core/hash.rb
revenc-0.2.1 lib/revenc/core/hash.rb
oct-0.3.2 lib/oct/core/hash.rb
oct-0.3.1 lib/oct/core/hash.rb