Sha256: d8f2e089966817030ba024ce96c016fd7fa7d67db252c9c4106086e8c22dec1e

Contents?: true

Size: 650 Bytes

Versions: 1

Compression:

Stored size: 650 Bytes

Contents

class Hash
 
  # Converts all of the keys to strings, optionally formatting key name
  def rubyify_keys!
    keys.each{|k|
      v = delete(k)
      new_key = k.to_s.to_underscore!
      self[new_key] = v
      v.rubyify_keys! if v.is_a?(Hash)
      v.each{|p| p.rubyify_keys! if p.is_a?(Hash)} if v.is_a?(Array)
    }
    self
  end
 
end

class String
  
  # converts a camel_cased string to a underscore string, 
  # Same way ActiveSupport does string.underscore
  def to_underscore!
    self.to_s.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

1 entries across 1 versions & 1 rubygems

Version Path
yelp4r-1.1.0 lib/rubyify_keys.rb