Sha256: 7c79fe7b52f83a82d559d15bcb96634b349c1ddb9c8457717c051a7a747fff1c

Contents?: true

Size: 759 Bytes

Versions: 9

Compression:

Stored size: 759 Bytes

Contents

# Big thanks to Tim Ruffles (https://github.com/timruffles)
# https://gist.github.com/timruffles/2780508
module HashConverter
  class << self

    def to_underscore(hash)
      convert(hash, :underscore)
    end

    def to_string(hash)
      convert(hash, :to_s)
    end

    def to_sym(hash)
      convert(hash, :to_sym)
    end

    # FIXME: not sure it will be ever needed
    # def to_camel_case hash
    #   convert hash, :camelize, :lower
    # end

    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

  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
locomotivecms_common-0.4.0 lib/locomotive/common/core_ext/hash.rb
locomotivecms_common-0.3.1 lib/locomotive/common/core_ext/hash.rb
locomotivecms_common-0.3.0 lib/locomotive/common/core_ext/hash.rb
locomotivecms_common-0.2.0 lib/locomotive/common/core_ext/hash.rb
locomotivecms_common-0.1.0 lib/locomotive/common/core_ext/hash.rb
locomotivecms_common-0.0.5 lib/locomotive/common/core_ext/hash.rb
locomotivecms_common-0.0.4 lib/locomotive/common/core_ext/hash.rb
locomotivecms_common-0.0.3 lib/locomotive/common/core_ext/hash.rb
locomotivecms_steam-1.0.0.pre.alpha lib/locomotive/steam/core_ext/hash.rb