Navigation Menu

Skip to content

Commit

Permalink
Extract collecting elements code
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Apr 8, 2014
1 parent 6b1ccff commit 574d95c
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions lib/droonga/plugins/search.rb
Expand Up @@ -49,32 +49,7 @@ class GatherCollector < Droonga::Collector
def collect(message)
output = message.input || message.name
if output.is_a?(Hash)
elements = output["elements"]
if elements and elements.is_a?(Hash)
# because "count" mapper requires all records,
# I have to apply it at first, before "limit" and "offset" are applied.
body = message.body
value = message.value
count_mapper = elements["count"]
if count_mapper
if count_mapper["no_output"]
value.delete("count")
else
value["count"] = value[count_mapper["target"]].size
end
end

records_mapper = elements["records"]
if records_mapper and value["records"]
if records_mapper["no_output"]
value.delete("records")
else
value["records"] = Reducer.apply_range(value["records"],
records_mapper)
value["records"] = apply_output_attributes_and_format(value["records"], records_mapper)
end
end
end
collect_elements(message, output["elements"])
output_name = output["output"]
else
output_name = output
Expand All @@ -83,6 +58,34 @@ def collect(message)
end

private
def collect_elements(message, elements)
return unless elements.is_a?(Hash)

# because "count" mapper requires all records,
# I have to apply it at first, before "limit" and "offset" are applied.
body = message.body
value = message.value
count_mapper = elements["count"]
if count_mapper
if count_mapper["no_output"]
value.delete("count")
else
value["count"] = value[count_mapper["target"]].size
end
end

records_mapper = elements["records"]
if records_mapper and value["records"]
if records_mapper["no_output"]
value.delete("records")
else
value["records"] = Reducer.apply_range(value["records"],
records_mapper)
value["records"] = apply_output_attributes_and_format(value["records"], records_mapper)
end
end
end

def apply_output_attributes_and_format(items, output)
attributes = output["attributes"] || []
if output["format"] == "complex"
Expand Down

0 comments on commit 574d95c

Please sign in to comment.