Created
July 15, 2010 22:00
-
-
Save ribrdb/477595 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap | |
import java.util.Map | |
class Params | |
def initialize(request:HttpServletRequest) | |
@request = request | |
@params = HashMap.new | |
end | |
def get(name:String) | |
values = array(name) | |
values[0] if values && values.size > 0 | |
end | |
macro def [](name) | |
quote { get(name) } | |
end | |
macro def []=(name, value) | |
quote { put(name, value) } | |
end | |
def array(name:String) | |
String[].cast(@params.get(name)) || @request.getParameterValues(name) | |
end | |
macro def values(name) | |
quote { array(name) } | |
end | |
def map(name:String) | |
Map(map_params.get(name)) | |
end | |
def put(name:String, values:String[]) | |
@params.put(name, value) | |
end | |
def put(name:String, value:String) | |
values = String[1] | |
values[0] = value | |
put(values) | |
end | |
private | |
def map_params | |
@maps ||= parse_maps | |
end | |
def parse_maps | |
map = HashMap.new | |
path_re = /[\[\]]+/ | |
@request.getParameterMap.entrySet.each do |entry| | |
name = String(entry.getKey) | |
values = String[].cast(entry.getValue) | |
path = path_re.split(name) | |
current_map = map | |
last = path.size - 1 | |
last.times do |i| | |
next_map = current_map.get(path[i]) | |
unless next_map.kind_of?(Map) | |
next_map = HashMap.new | |
current_map.put(path[i], next_map) | |
end | |
current_map = next_map | |
end | |
if values.length == 1 | |
current_map.put(path[last], values[0]) | |
else | |
current_map.put(path[last], values) | |
end | |
end | |
Map(map) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment