Sha256: 78fca7a85ae594bdf49dc00885dc123c9384552c74dfabca153cba65b2fafce7

Contents?: true

Size: 898 Bytes

Versions: 6

Compression:

Stored size: 898 Bytes

Contents

# Hash.to_snake_keys
class Hash
  # Recursively converts CamelCase and camelBack JSON-style hash keys to
  # Rubyish snake_case, suitable for use during instantiation of Ruby
  # model attributes.
  #
  def to_snake_keys(value = self)
    case value
    when Array
      value.map { |v| to_snake_keys(v) }
    when Hash
      snake_hash(value)
    else
      value
    end
  end

  private

  def snake_hash(value)
    Hash[value.map { |k, v| [underscore_key(k).to_sym, to_snake_keys(v)] }]
  end

  def underscore_key(k)
    if k.is_a? Symbol
      underscore(k.to_s).to_sym
    elsif k.is_a? String
      underscore(k)
    else
      k # Plissken can't snakify anything except strings and symbols
    end
  end

  def underscore(string)
    string.gsub(/::/, '/')
      .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
      .gsub(/([a-z\d])([A-Z])/, '\1_\2')
      .tr('-', '_')
      .downcase
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
fullcontact-api-ruby-0.1.0 lib/fullcontact/ext/hash/to_snake_keys.rb
fullcontact-0.18.0 lib/fullcontact/ext/hash/to_snake_keys.rb
fullcontact-0.17.0 lib/fullcontact/ext/hash/to_snake_keys.rb
fullcontact-0.16.0 lib/fullcontact/ext/hash/to_snake_keys.rb
fullcontact-0.15.0 lib/fullcontact/ext/hash/to_snake_keys.rb
fullcontact-0.14.0 lib/fullcontact/ext/hash/to_snake_keys.rb