Navigation Menu

Skip to content

Commit

Permalink
Simply command apply pattern
Browse files Browse the repository at this point in the history
It is changed to a pattern from an array of patterns. Maybe, an array
of patterns is needless.
  • Loading branch information
kou committed Feb 3, 2014
1 parent 3270b12 commit 11c21e8
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 35 deletions.
20 changes: 7 additions & 13 deletions lib/droonga/command.rb
Expand Up @@ -18,13 +18,12 @@ class Command
attr_reader :method_name
#
#
# @option options [Array<Array>] :patterns The patterns to be matched
# against message. If all of the patterns are matched to a message,
# @option options [Array] :pattern The pattern to be matched
# against message. If the pattern is matched to a message,
# the command will be applied.
#
# Here is patterns syntax.
# Here is pattern syntax.
#
# * PATTERNS = [PATTERN*]
# * PATTERN = [TARGET_PATH, OPERATOR, ARGUMENTS*]
# * PATTERN = [PATTERN, LOGICAL_OPERATOR, PATTERN]
# * TARGET_PATH = "COMPONENT(.COMPONENT)*"
Expand All @@ -37,7 +36,7 @@ class Command
# For example:
#
# ```
# [["type", :equal, "search"]]
# ["type", :equal, "search"]
# ```
#
# matches to the following message:
Expand All @@ -49,7 +48,7 @@ class Command
# Another example:
#
# ```
# [["body.output.limit", :equal, 10]]
# ["body.output.limit", :equal, 10]
# ```
#
# matches to the following message:
Expand All @@ -69,17 +68,12 @@ def initialize(method_name, options)
end

def match?(message)
patterns.all? do |pattern|
match_pattern?(pattern, message)
end
match_pattern?(@options[:pattern], message)
end

private
def patterns
@options[:patterns] || []
end

def match_pattern?(pattern, message)
return false if pattern.nil?
path, operator, *arguments = pattern
target = resolve_path(path, message)
apply_operator(operator, target, arguments)
Expand Down
2 changes: 1 addition & 1 deletion lib/droonga/plugin/output_adapter/crud.rb
Expand Up @@ -20,7 +20,7 @@ class CRUDOutputAdapter < Droonga::OutputAdapterPlugin
repository.register("crud", self)

command :convert_success,
:patterns => [["replyTo.type", :equal, "add.result"]]
:pattern => ["replyTo.type", :equal, "add.result"]
def convert_success(output_message)
if output_message.body.include?("success")
success = output_message.body["success"]
Expand Down
4 changes: 2 additions & 2 deletions lib/droonga/plugin/output_adapter/groonga.rb
Expand Up @@ -20,7 +20,7 @@ class GroongaOutputAdapter < Droonga::OutputAdapterPlugin
repository.register("groonga", self)

command :convert_select,
:patterns => [["originalTypes", :include?, "select"]]
:pattern => ["originalTypes", :include?, "select"]
def convert_select(output_message)
command = Select.new
output_message.body = command.convert(output_message.body)
Expand All @@ -32,7 +32,7 @@ def convert_select(output_message)
"column_create.result",
]
command :convert_generic_result,
:patterns => [["replyTo.type", :in, *groonga_results]]
:pattern => ["replyTo.type", :in, *groonga_results]
def convert_generic_result(output_message)
if output_message.body.include?("result")
output_message.body = output_message.body["result"]
Expand Down
7 changes: 1 addition & 6 deletions lib/droonga/plugin_registerable.rb
Expand Up @@ -41,15 +41,10 @@ def inherited(sub_class)
def command(method_name_or_map, options={})
if method_name_or_map.is_a?(Hash)
type, method_name = method_name_or_map.to_a.first
options[:patterns] ||= []
options[:patterns] << ["type", :equal, type.to_s]
else
method_name = method_name_or_map
if options.empty?
options[:patterns] ||= []
options[:patterns] << ["type", :equal, method_name.to_s]
end
end
options[:pattern] ||= ["type", :equal, method_name.to_s]
command = Command.new(method_name, options)
@command_repository.register(command)
end
Expand Down
24 changes: 12 additions & 12 deletions test/unit/test_command.rb
Expand Up @@ -56,24 +56,24 @@ def test_nested
end

class MatchTest < self
def command(patterns)
super(:method_name, :patterns => patterns)
def command(pattern)
super(:method_name, :pattern => pattern)
end

def match?(patterns, message)
command(patterns).match?(message)
def match?(pattern, message)
command(pattern).match?(message)
end

class EqualTest < self
def test_same_value
assert_true(match?([["type", :equal, "select"]],
assert_true(match?(["type", :equal, "select"],
{
"type" => "select"
}))
end

def test_different_value
assert_false(match?([["type", :equal, "select"]],
assert_false(match?(["type", :equal, "select"],
{
"type" => "search",
}))
Expand All @@ -82,14 +82,14 @@ def test_different_value

class InTest < self
def test_exist
assert_true(match?([["type", :in, "table_create", "table_remove"]],
assert_true(match?(["type", :in, "table_create", "table_remove"],
{
"type" => "table_remove"
}))
end

def test_not_exist
assert_false(match?([["type", :in, "table_create", "table_remove"]],
assert_false(match?(["type", :in, "table_create", "table_remove"],
{
"type" => "column_create",
}))
Expand All @@ -98,26 +98,26 @@ def test_not_exist

class IncludeTest < self
def test_exist
assert_true(match?([["originalTypes", :include?, "select"]],
assert_true(match?(["originalTypes", :include?, "select"],
{
"originalTypes" => ["search", "select"],
}))
end

def test_not_exist
assert_false(match?([["originalTypes", :include?, "select"]],
assert_false(match?(["originalTypes", :include?, "select"],
{
"originalTypes" => ["load"],
}))
end

def test_no_key
assert_false(match?([["originalTypes", :include?, "select"]],
assert_false(match?(["originalTypes", :include?, "select"],
{}))
end

def test_not_enumerable
assert_false(match?([["originalTypes", :include?, "select"]],
assert_false(match?(["originalTypes", :include?, "select"],
{
"originalTypes" => 29,
}))
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_command_repository.rb
Expand Up @@ -24,7 +24,7 @@ class FindTest < self
def setup
super
@command = Droonga::Command.new(:select,
:patterns => [["type", :equal, "select"]])
:pattern => ["type", :equal, "select"])
@repository.register(@command)
end

Expand Down

0 comments on commit 11c21e8

Please sign in to comment.