Sha256: 29b85eeea0ffa79bbee07d5507a531e3ed3bccd8febd59f091321a20706a03a1

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

require 'rubygems' # DEPRECATION: required for ruby 1.8.7
require 'tsort'
require 'yaml'

class Array
  def to_yaml_style
    :inline
  end
end

class Hash
  include TSort
  alias tsort_each_node each_key
  def tsort_each_child(node, &block)
    fetch(node).each(&block)
  end

  # https://github.com/rails/rails/blob/c48a0cac626b4e32d7abfa9f4f1fae16568157d9/activesupport/lib/active_support/core_ext/hash/keys.rb
  #
  # Destructively convert all keys to symbols, as long as they respond
  # to +to_sym+. This includes the keys from the root hash and from all
  # nested hashes.
  #
  # DEPRECATION
  # required for: ruby 1.8.7
  # can be replaced with activesupport ~4.0
  #
  def deep_symbolize_keys!
    deep_transform_keys!{ |key| key.to_sym rescue key }
  end

  def deep_stringify_keys!
    deep_transform_keys!{ |key| key.to_s rescue key }
  end

  # Destructively convert all keys by using the block operation.
  # This includes the keys from the root hash and from all
  # nested hashes.
  #
  def deep_transform_keys! &block
    keys.each do |key|
      value = delete(key)

      self[yield(key)] = case value
      when Hash
        value.deep_transform_keys!(&block)
      when Array
        value.each{ |e| e.deep_transform_keys!(&block) rescue value }
      else
        value
      end
    end
    self
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yuyi-1.1.7 lib/yuyi/core.rb
yuyi-1.1.6 lib/yuyi/core.rb