Sha256: b51a8f0074351109db0c3e3fcc5c9c2e4660891ce7c97c5d48890d3c71990986

Contents?: true

Size: 1.93 KB

Versions: 86

Compression:

Stored size: 1.93 KB

Contents

require 'cgi'

class Object
  # Alias of <tt>to_s</tt>.
  def to_param
    to_s
  end

  # Converts an object into a string suitable for use as a URL query string,
  # using the given <tt>key</tt> as the param name.
  def to_query(key)
    "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
  end
end

class NilClass
  # Returns +self+.
  def to_param
    self
  end
end

class TrueClass
  # Returns +self+.
  def to_param
    self
  end
end

class FalseClass
  # Returns +self+.
  def to_param
    self
  end
end

class Array
  # Calls <tt>to_param</tt> on all its elements and joins the result with
  # slashes. This is used by <tt>url_for</tt> in Action Pack.
  def to_param
    collect(&:to_param).join '/'
  end

  # Converts an array into a string suitable for use as a URL query string,
  # using the given +key+ as the param name.
  #
  #   ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
  def to_query(key)
    prefix = "#{key}[]"

    if empty?
      nil.to_query(prefix)
    else
      collect { |value| value.to_query(prefix) }.join '&'
    end
  end
end

class Hash
  # Returns a string representation of the receiver suitable for use as a URL
  # query string:
  #
  #   {name: 'David', nationality: 'Danish'}.to_query
  #   # => "name=David&nationality=Danish"
  #
  # An optional namespace can be passed to enclose key names:
  #
  #   {name: 'David', nationality: 'Danish'}.to_query('user')
  #   # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
  #
  # The string pairs "key=value" that conform the query string
  # are sorted lexicographically in ascending order.
  #
  # This method is also aliased as +to_param+.
  def to_query(namespace = nil)
    collect do |key, value|
      unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?
        value.to_query(namespace ? "#{namespace}[#{key}]" : key)
      end
    end.compact.sort! * '&'
  end

  alias_method :to_param, :to_query
end

Version data entries

86 entries across 84 versions & 10 rubygems

Version Path
activesupport-5.0.2.rc1 lib/active_support/core_ext/object/to_query.rb
openstack-3.3.9 lib/openstack/core_ext/to_query.rb
openstack-3.3.8 lib/openstack/core_ext/to_query.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/object/to_query.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/object/to_query.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/object/to_query.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/object/to_query.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/object/to_query.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/object/to_query.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/object/to_query.rb
abaci-0.3.0 vendor/bundle/gems/activesupport-5.0.1/lib/active_support/core_ext/object/to_query.rb
abaci-0.3.0 vendor/bundle/gems/activesupport-5.0.0/lib/active_support/core_ext/object/to_query.rb
activesupport-5.0.1 lib/active_support/core_ext/object/to_query.rb
activesupport-5.0.1.rc2 lib/active_support/core_ext/object/to_query.rb
activesupport-5.0.1.rc1 lib/active_support/core_ext/object/to_query.rb
openstack-3.3.7 lib/openstack/core_ext/to_query.rb
openstack-3.3.6 lib/openstack/core_ext/to_query.rb
openstack-3.3.5 lib/openstack/core_ext/to_query.rb
openstack-3.3.4 lib/openstack/core_ext/to_query.rb
openstack-3.3.3 lib/openstack/core_ext/to_query.rb