def query_values(options={})
defaults = {:notation => :subscript}
options = defaults.merge(options)
if ![:flat, :dot, :subscript, :flat_array].include?(options[:notation])
raise ArgumentError,
"Invalid notation. Must be one of: " +
"[:flat, :dot, :subscript, :flat_array]."
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
empty_accumulator = :flat_array == options[:notation] ? [] : {}
return ((self.query.split("&").map do |pair|
pair.split("=", 2) if pair && pair != ""
end).compact.inject(empty_accumulator.dup) 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
elsif options[:notation] == :flat_array
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(empty_accumulator.dup) do |accumulator, (key, value)|
if options[:notation] == :flat_array
accumulator << [key, value]
else
accumulator[key] = value.kind_of?(Hash) ? dehash.call(value) : value
end
accumulator
end
end