Skip to content

Commit

Permalink
Resolve rspec version discrepancy, simplify gemspec, apply code-clean…
Browse files Browse the repository at this point in the history
…er for good measure.
  • Loading branch information
jcoene committed Apr 5, 2011
1 parent 9fd1d98 commit efc72be
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 200 deletions.
8 changes: 1 addition & 7 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
source 'http://rubygems.org'

gem "jeweler"
gem "database_cleaner"
gem "mongoid", "~> 2.0.0.beta.19"
gem "rake", "0.8.7"
gem "rspec", "2.3.0"
gem "fast-stemmer", "1.0.0"
gemspec
56 changes: 29 additions & 27 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
PATH
remote: .
specs:
mongoid_search (0.2.0)
bson_ext (~> 1.2)
fast-stemmer (~> 1.0.0)
mongoid (~> 2.0.0)

GEM
remote: http://rubygems.org/
specs:
activemodel (3.0.5)
activesupport (= 3.0.5)
activemodel (3.0.6)
activesupport (= 3.0.6)
builder (~> 2.1.2)
i18n (~> 0.4)
activesupport (3.0.5)
bson (1.2.4)
i18n (~> 0.5.0)
activesupport (3.0.6)
bson (1.3.0)
bson_ext (1.3.0)
builder (2.1.2)
database_cleaner (0.6.4)
database_cleaner (0.6.6)
diff-lcs (1.1.2)
fast-stemmer (1.0.0)
git (1.2.5)
i18n (0.5.0)
jeweler (1.5.2)
bundler (~> 1.0.0)
git (>= 1.2.5)
rake
mongo (1.2.4)
bson (>= 1.2.4)
mongoid (2.0.0.rc.7)
mongo (1.3.0)
bson (>= 1.3.0)
mongoid (2.0.0)
activemodel (~> 3.0)
mongo (~> 1.2)
tzinfo (~> 0.3.22)
will_paginate (~> 3.0.pre)
rake (0.8.7)
rspec (2.3.0)
rspec-core (~> 2.3.0)
rspec-expectations (~> 2.3.0)
rspec-mocks (~> 2.3.0)
rspec-core (2.3.1)
rspec-expectations (2.3.0)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.5.0)
rspec-core (2.5.1)
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.3.0)
tzinfo (0.3.24)
rspec-mocks (2.5.0)
tzinfo (0.3.26)
will_paginate (3.0.pre2)

PLATFORMS
ruby

DEPENDENCIES
database_cleaner
fast-stemmer (= 1.0.0)
jeweler
mongoid (~> 2.0.0.beta.19)
database_cleaner (~> 0.6.4)
mongoid_search!
rake (= 0.8.7)
rspec (= 2.3.0)
rspec (~> 2.4)
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Installation
In your Gemfile:

gem 'mongoid_search'

Then:

bundle install

Examples
Expand All @@ -25,7 +25,7 @@ Examples

references_many :tags
refereced_in :category

search_in :brand, :name, :tags => :name, :category => :name
end

Expand All @@ -35,7 +35,7 @@ Examples

referenced_in :product
end

class Category
include Mongoid::Document
field :name
Expand All @@ -44,53 +44,53 @@ Examples
end

Now when you save a product, you get a _keywords field automatically:

p = Product.new :brand => "Apple", :name => "iPhone"
p.tags << Tag.new(:name => "Amazing")
p.tags << Tag.new(:name => "Awesome")
p.tags << Tag.new(:name => "Superb")
p.save
=> true
p._keywords

Now you can run search, which will look in the _keywords field and return all matching results:

Product.search("apple iphone").size
=> 1

Note that the search is case insensitive, and accept partial searching too:

Product.search("ipho").size
=> 1

Options
-------

match:
_:any_ - match any occurrence
_:all_ - match all ocurrences
match:
_:any_ - match any occurrence
_:all_ - match all ocurrences
Default is _:any_.

search_in :brand, :name, { :tags => :name }, { :match => :any }

Product.search("apple motorola").size
=> 1

search_in :brand, :name, { :tags => :name }, { :match => :all }

Product.search("apple motorola").size
=> 0
allow_empty_search:
_true_ - match any occurrence
_false_ - match all ocurrences

allow_empty_search:
_true_ - match any occurrence
_false_ - match all ocurrences
Default is _false_.

search_in :brand, :name, { :tags => :name }, { :allow_empty_search => true }

Product.search("").size
=> 1

TODO
----

Expand Down
18 changes: 0 additions & 18 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
require 'rubygems'
require 'rake'

begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "mongoid_search"
gem.summary = "Search implementation for Mongoid ORM"
gem.description = "Simple full text search implementation."
gem.email = "mauricio@papodenerd.net"
gem.homepage = "http://www.papodenerd.net/mongoid-search-full-text-search-for-your-mongoid-models/"
gem.authors = ["Mauricio Zaffari"]
gem.add_dependency("mongoid", ["~> 2.0.0"])
gem.add_development_dependency "rspec", ">= 2.0.1"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.rspec_opts = ["-c", "-f progress"]
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.2.0
2 changes: 1 addition & 1 deletion lib/mongoid_search.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require 'mongoid_search/util'
require 'mongoid_search/mongoid_search'
require 'mongoid_search/mongoid_search'
28 changes: 14 additions & 14 deletions lib/mongoid_search/mongoid_search.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Mongoid::Search
extend ActiveSupport::Concern

included do
cattr_accessor :search_fields, :match, :allow_empty_search, :relevant_search, :stem_keywords, :ignore_list
end

module ClassMethods #:nodoc:
# Set a field or a number of fields as sources for search
def search_in(*args)
Expand All @@ -18,28 +18,28 @@ def search_in(*args)

field :_keywords, :type => Array
index :_keywords

before_save :set_keywords
end

def search(query, options={})
if relevant_search
search_relevant(query, options)
else
search_without_relevance(query, options)
end
end

def search_without_relevance(query, options={})
return self.all if query.blank? && allow_empty_search
self.send("#{(options[:match]||self.match).to_s}_in", :_keywords => Util.keywords(query, stem_keywords, ignore_list).map { |q| /#{q}/ })
end

def search_relevant(query, options={})
return self.all if query.blank? && allow_empty_search

keywords = Util.keywords(query, stem_keywords, ignore_list)

map = <<-EOS
function() {
var entries = 0
Expand All @@ -57,27 +57,27 @@ def search_relevant(query, options={})
return(values[0])
}
EOS

#raise [self.class, self.inspect].inspect

kw_conditions = keywords.map do |kw|
{:_keywords => kw}
end

criteria = self.any_of(*kw_conditions)

query = criteria.selector

options.delete(:limit)
options.delete(:skip)
options.merge! :scope => {:keywords => keywords}, :query => query

res = collection.map_reduce(map, reduce, options)

res.find.sort(['value', -1]) # Cursor
end
end

private

# TODO: This need some refatoring..
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid_search/util.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Util

def self.keywords(text, stem_keywords, ignore_list)
return [] if text.blank?
text = text.
Expand All @@ -16,4 +16,4 @@ def self.keywords(text, stem_keywords, ignore_list)
text
end

end
end
Loading

0 comments on commit efc72be

Please sign in to comment.