lib/search_party.rb in laserlemon-search_party-0.3.2 vs lib/search_party.rb in laserlemon-search_party-0.3.3
- old
+ new
@@ -12,28 +12,34 @@
base.cattr_accessor :search_parameter_patterns
base.search_parameter_patterns = []
end
def search_parameters
- @search_parameters ||= begin
- query_parameters.symbolize_keys.inject({}) do |new_parameters, (key, value)|
- parsed_value = parse_search_parameter(value)
- if model_name = self.class.search_parameter_models[key]
- model = model_name.to_s.classify.constantize
- finder = self.class.search_parameter_finders[key]
- new_values = [*parsed_value].map{|v| model.send(finder, v) }
- new_value = (parsed_value.is_a?(Array) ? new_values.dup : new_values.first)
- else
- new_value = parsed_value
- end
- new_parameters.update(key => new_value)
- end
- end.with_indifferent_access
+ @search_parameters ||= parse_hash(query_parameters)
end
private
- def parse_search_parameter(value)
+ def parse_hash(hash)
+ hash.symbolize_keys.inject({}) do |new_hash, (key, value)|
+ parsed_value = case value
+ when Hash then parse_hash(value)
+ when Array then value.map{|v| parse_value(v) }
+ else parse_value(v)
+ end
+ new_value = if model_name = self.class.search_parameter_models[key]
+ model = model_name.to_s.classify.constantize
+ finder = self.class.search_parameter_finders[key]
+ new_values = [*parsed_value].map{|v| model.send(finder, v) }
+ parsed_value.is_a?(Array) ? new_values.dup : new_values.first
+ else
+ parsed_value
+ end
+ new_hash.update(key => new_value)
+ end.with_indifferent_access
+ end
+
+ def parse_value(value)
if p = self.class.search_parameter_patterns.detect{|r,v| r.match(value) }
(v = p.last).respond_to?(:call) ? v.call($~) : v
else
value
end