Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default search values #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
rvm:
- 1.9.3
- 2.0.0

- 2.6.1
bundler_args: --without docs

notifications:
email:
- brwcodes@gmail.com
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ KeywordSearch.search(terms) do |with|
arguments << "%#{values.join(' ')}%"
end

# A keyword description can be passed as the second argument of the
# `keyword` call, and read back as an attribute on the keyword
with.keyword :title, 'Some description of the title search here' do |values|
...
end

# Default values can be passed as the `keyword` call's third argument.
# Such default values will be assumed to be part of the search string
# if its keyword is not already present in that string. Defaults are
# always positive.
with.keyword :title, nil, "Dr." do |values|
# A search on 'title:Mr.' will yield a `values` of `['Mr.']`
# A search on 'has:something' will yield a `values` of `['Dr.']`
end

end
```

Expand Down
2 changes: 1 addition & 1 deletion keyword_search.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ Gem::Specification.new do |spec|
spec.rdoc_options = ["--charset=UTF-8"]
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "minitest"
end
9 changes: 8 additions & 1 deletion lib/keyword_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class << self
def search(input_string, definition=nil, &block)
definition ||= Definition.new(&block)
results = parse(input_string)
add_default_values(results, definition)
results.each do |key, terms|
definition.handle(key, terms)
end
Expand All @@ -25,6 +26,13 @@ def search(input_string, definition=nil, &block)
private
#######

def add_default_values(results, definition)
definition.keywords.each do |keyword|
next if keyword.default_values.nil?
results[keyword.name.to_s] ||= parse(keyword.default_values)[:default]
end
end

def parse(input) #:nodoc:
data = input + ' '

Expand Down Expand Up @@ -2286,4 +2294,3 @@ class << self

end


10 changes: 5 additions & 5 deletions lib/keyword_search/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class Definition

class Keyword

attr_reader :name, :description, :handler
def initialize(name, description=nil, &handler)
@name, @description = name, description
attr_reader :name, :description, :default_values, :handler
def initialize(name, description=nil, default_values=nil, &handler)
@name, @description, @default_values = name, description, default_values
@handler = handler
end

Expand All @@ -31,8 +31,8 @@ def keywords
@keywords ||= []
end

def keyword(name, description=nil, &block)
keywords << Keyword.new(name, description, &block)
def keyword(name, description=nil, default_values=nil, &block)
keywords << Keyword.new(name, description, default_values, &block)
end

def default_keyword(name)
Expand Down
2 changes: 1 addition & 1 deletion lib/keyword_search/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module KeywordSearch
VERSION = '1.5.0'
VERSION = '1.6.0'
end
24 changes: 24 additions & 0 deletions test/test_keyword_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,28 @@
end
assert_equal [ [ %w(google.com), false ] ], result
end

it 'a search falling back to default values' do
result = []

KeywordSearch.search(%<>) do |with|
with.keyword :some_flag, nil, "false" do |values, positive|
result << [ values, positive ]
end
end

assert_equal [ [ %w(false), true ] ], result
end

it 'a search should not use default values when explicit values present' do
result = []

KeywordSearch.search(%<some_flag:true>) do |with|
with.keyword :some_flag, nil, "false" do |values, positive|
result << [ values, positive ]
end
end

assert_equal [ [ %w(true), true ] ], result
end
end