def query_values(options={})
defaults = {:notation => :subscript}
options = defaults.merge(options)
if ![:flat, :dot, :subscript].include?(options[:notation])
raise ArgumentError,
"Invalid notation. Must be one of: [:flat, :dot, :subscript]."
end
dehash = lambda do |hash|
hash.each do |(key, value)|
if value.kind_of?(Hash)
hash[key] = dehash.call(value)
end
end
if hash != {} && hash.keys.all? { |key| key =~ /^\d+$/ }
hash.sort.inject([]) do |accu, (key, value)|
accu << value; accu
end
else
hash
end
end
return nil if self.query == nil
return ((self.query.split("&").map do |pair|
pair.split("=")
end).inject({}) do |accumulator, (key, value)|
value = true if value.nil?
key = self.class.unencode_component(key)
if value != true
value = self.class.unencode_component(value).gsub(/\+/, " ")
end
if options[:notation] == :flat
if accumulator[key]
raise ArgumentError, "Key was repeated: #{key.inspect}"
end
accumulator[key] = value
else
if options[:notation] == :dot
array_value = false
subkeys = key.split(".")
elsif options[:notation] == :subscript
array_value = !!(key =~ /\[\]$/)
subkeys = key.split(/[\[\]]+/)
end
current_hash = accumulator
for i in 0...(subkeys.size - 1)
subkey = subkeys[i]
current_hash[subkey] = {} unless current_hash[subkey]
current_hash = current_hash[subkey]
end
if array_value
current_hash[subkeys.last] = [] unless current_hash[subkeys.last]
current_hash[subkeys.last] << value
else
current_hash[subkeys.last] = value
end
end
accumulator
end).inject({}) do |accumulator, (key, value)|
accumulator[key] = value.kind_of?(Hash) ? dehash.call(value) : value
accumulator
end
end