Skip to content

Commit

Permalink
Control output of elements by "output.elements" as the spec
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed May 1, 2013
1 parent 20c9c99 commit 80e26ad
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
32 changes: 22 additions & 10 deletions lib/droonga/plugin/handler_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ def need_output?
end

def format
params = @query["output"]
formatted_result = {}
format_count(params, formatted_result)
format_records(params, formatted_result)
if params["elapsedTime"]
format_count(formatted_result)
format_records(formatted_result)
if need_element_output?("startTime")
formatted_result["startTime"] = @start_time.iso8601
end
if need_element_output?("elapsedTime")
formatted_result["elapsedTime"] = Time.now.to_f - @start_time.to_f
end
formatted_result
Expand Down Expand Up @@ -217,15 +218,26 @@ def search_query(results)
@result
end

def format_count(params, formatted_result)
return unless params["count"]
formatted_result["count"] = @count
def need_element_output?(element)
params = @query["output"]

elements = params["elements"]
return false if elements.nil?

elements.include?(element)
end

def format_records(params, formatted_result)
attributes = params["attributes"]
return if attributes.nil?
def format_count(formatted_result)
return unless need_element_output?("count")
formatted_result["count"] = @result.size
end

def format_records(formatted_result)
return unless need_element_output?("records")

params = @query["output"]

attributes = params["attributes"]
target_attributes = normalize_target_attributes(attributes)
offset = params["offset"] || 0
limit = params["limit"] || 10
Expand Down
27 changes: 25 additions & 2 deletions test/plugin/test_handler_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def base_request
"sections-result" => {
"source" => "Sections",
"output" => {
"elements" => [
"records",
],
"format" => "complex",
"limit" => 1,
"attributes" => ["title"],
Expand Down Expand Up @@ -184,7 +187,9 @@ def test_count
"sections-result" => {
"source" => "Sections",
"output" => {
"count" => true,
"elements" => [
"count",
],
},
},
},
Expand All @@ -203,7 +208,10 @@ def test_elapsed_time
"sections-result" => {
"source" => "Sections",
"output" => {
"elapsedTime" => true,
"elements" => [
"startTime",
"elapsedTime",
],
},
},
},
Expand Down Expand Up @@ -235,6 +243,9 @@ def test_source_only
"sections-result" => {
"source" => "Sections",
"output" => {
"elements" => [
"records",
],
"format" => "complex",
"limit" => 3,
"attributes" => ["_key", "title"],
Expand Down Expand Up @@ -269,6 +280,9 @@ def test_label
"sections-result" => {
"source" => "Sections",
"output" => {
"elements" => [
"records",
],
"format" => "complex",
"limit" => 3,
"attributes" => [
Expand All @@ -294,6 +308,9 @@ def test_complex
"sections-result" => {
"source" => "Sections",
"output" => {
"elements" => [
"records",
],
"format" => "complex",
"limit" => 3,
"attributes" => ["_key", "title"],
Expand All @@ -311,6 +328,9 @@ def test_simple
"sections-result" => {
"source" => "Sections",
"output" => {
"elements" => [
"records",
],
"format" => "simple",
"limit" => 3,
"attributes" => ["_key", "title"],
Expand All @@ -328,6 +348,9 @@ def test_default
"sections-result" => {
"source" => "Sections",
"output" => {
"elements" => [
"records",
],
"limit" => 3,
"attributes" => ["_key", "title"],
},
Expand Down
4 changes: 3 additions & 1 deletion test/test_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def test_minimum
"sections" => {
"source" => "Sections",
"output" => {
"count" => true,
"elements" => [
"count",
],
},
},
},
Expand Down

0 comments on commit 80e26ad

Please sign in to comment.