Sha256: 3f60ab4f881b56af15622a1916ba36e39caf8bab4e77556a071f4351ded75020

Contents?: true

Size: 832 Bytes

Versions: 3

Compression:

Stored size: 832 Bytes

Contents

class Hash
  unless method_defined?(:symbolize_keys)
    def symbolize_keys
      inject({}) do |options, (key, value)|
        options[(key.to_sym rescue key) || key] = value
        options
      end
    end
  end

  unless method_defined?(:dasherize_keys)
    def dasherize_keys
      inject({}) do |options, (key, value)|
        options[key.to_s.gsub(/_/, '-')] = value
        options
      end
    end
  end

  unless method_defined?(:to_query) && method(:to_query).arity == -1
    def to_query(namespace=nil)
      collect do |key, value|
        value.to_query(namespace ? "#{namespace}[#{key}]" : key)
      end.sort * '&'
    end
  end
end

class Array
  unless method_defined?(:to_query) && method(:to_query).arity == -1
    def to_query(key)
      collect { |value| value.to_query("#{key}[]") } * '&'
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
the_viking-1.0.0 lib/core_ext/transformations.rb
viking-1.0.1 lib/core_ext/transformations.rb
viking-1.0.0 lib/core_ext/transformations.rb