Sha256: 7b969e5d0dc9a933928e986d0cb5d72cb85fb518a0f6032c08c7f647d43ca62b
Contents?: true
Size: 1.55 KB
Versions: 1
Compression:
Stored size: 1.55 KB
Contents
require 'active_support/core_ext/hash' require 'active_support/core_ext/string/inflections' module CamelSnakeKeys class << self def if_underscore(v) if v.is_a? Symbol v.to_s.underscore.to_sym elsif v.is_a? String v.underscore else v end end def if_camelize(v) if v.is_a? Symbol v.to_s.camelize(:lower).to_sym elsif v.is_a? String v.camelize(:lower) else v end end def snake_keys(data, indifference=false) if data.kind_of? Array data.map { |v| snake_keys(v) } elsif data.kind_of? HashWithIndifferentAccess || (data.kind_of? Hash && indifference) HashWithIndifferentAccess[data.map {|k, v| [if_underscore(k), snake_keys(v)] }] elsif data.kind_of? Hash Hash[data.map {|k, v| [if_underscore(k), snake_keys(v)] }] else data end end def camel_keys(data, indifference=false) if data.kind_of? Array data.map { |v| camel_keys(v) } elsif data.kind_of? HashWithIndifferentAccess || (data.kind_of? Hash && indifference) HashWithIndifferentAccess[data.map {|k, v| [if_camelize(k), camel_keys(v)] }] elsif data.kind_of?(Hash) Hash[data.map {|k, v| [if_camelize(k), camel_keys(v)] }] else data end end end end module Enumerable def with_camel_keys(indifference=false) CamelSnakeKeys.camel_keys(self, indifference) end def with_snake_keys(indifference=false) CamelSnakeKeys.snake_keys(self, indifference) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
camel_snake_keys-0.0.1 | lib/camel_snake_keys.rb |