Module: HashConverter
- Defined in:
- lib/locomotive/common/core_ext/hash.rb
Overview
Big thanks to Tim Ruffles (github.com/timruffles) gist.github.com/timruffles/2780508
Class Method Summary collapse
-
.convert(obj, *method) ⇒ Object
FIXME: not sure it will be ever needed def to_camel_case hash convert hash, :camelize, :lower end.
- .to_string(hash) ⇒ Object
- .to_sym(hash) ⇒ Object
- .to_underscore(hash) ⇒ Object
Class Method Details
.convert(obj, *method) ⇒ Object
FIXME: not sure it will be ever needed def to_camel_case hash
convert hash, :camelize, :lower
end
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/locomotive/common/core_ext/hash.rb', line 23 def convert(obj, *method) case obj when Hash obj.inject({}) do |h, (k,v)| v = convert(v, *method) h[k.send(*method)] = v h end when Array obj.map { |m| convert(m, *method) } else obj end end |
.to_string(hash) ⇒ Object
10 11 12 |
# File 'lib/locomotive/common/core_ext/hash.rb', line 10 def to_string(hash) convert(hash, :to_s) end |
.to_sym(hash) ⇒ Object
14 15 16 |
# File 'lib/locomotive/common/core_ext/hash.rb', line 14 def to_sym(hash) convert(hash, :to_sym) end |
.to_underscore(hash) ⇒ Object
6 7 8 |
# File 'lib/locomotive/common/core_ext/hash.rb', line 6 def to_underscore(hash) convert(hash, :underscore) end |