mongoid_search 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Mauricio Zaffari
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,71 @@
1
+ Mongoid Searh
2
+ ============
3
+
4
+ Mongoid Search is a simple full text search implementation for Mongoid ORM.
5
+
6
+ Installation
7
+ --------
8
+
9
+ In your Gemfile:
10
+
11
+ gem 'mongoid_search'
12
+
13
+ Then:
14
+
15
+ bundle install
16
+
17
+ Examples
18
+ --------
19
+
20
+ class Product
21
+ include Mongoid::Document
22
+ include Mongoid::Search
23
+ field :brand
24
+ field :name
25
+
26
+ references_many :tags
27
+
28
+ search_in :brand, :name, :tags => :name
29
+ end
30
+
31
+ class Tag
32
+ include Mongoid::Document
33
+ field :name
34
+
35
+ referenced_in :product
36
+ end
37
+
38
+ Now when you save a product, you get a _keywords field automatically:
39
+
40
+ p = Product.new :brand => "Apple", :name => "iPhone"
41
+ p.tags << Tag.new(:name => "Amazing")
42
+ p.tags << Tag.new(:name => "Awesome")
43
+ p.tags << Tag.new(:name => "Superb")
44
+ p.save
45
+ => true
46
+ p._keywords
47
+
48
+ Now you can run search, which will look in the _keywords field and return all matching results:
49
+
50
+ Product.search("apple iphone").size
51
+ => 1
52
+
53
+ Note that the search is case insensitive, and accept partial searching too:
54
+
55
+ Product.search("ipho").size
56
+ => 1
57
+
58
+ Options
59
+ -------
60
+
61
+ :match - :any for match any occurrence, :all to match all ocurrences. Default is :any
62
+
63
+ search_in :brand, :name, { :tags => :name }, { :match => :all }
64
+
65
+ Product.search("apple motorola").size
66
+ => 1
67
+
68
+ search_in :brand, :name, { :tags => :name }, { :match => :all }
69
+
70
+ Product.search("apple motorola").size
71
+ => 0
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "mongoid_search"
8
+ gem.summary = "Search implementation for Mongoid ORM"
9
+ gem.description = "Simple full text search implementation."
10
+ gem.email = "mauricio@papodenerd.net"
11
+ gem.homepage = "http://github.com/mauriciozaffari/mongoid_search"
12
+ gem.authors = ["Mauricio Zaffari"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "mongoid_search #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,2 @@
1
+ require 'mongoid_search/keywords_extractor'
2
+ require 'mongoid_search/mongoid_search'
@@ -0,0 +1,6 @@
1
+ class KeywordsExtractor
2
+ def self.extract(text)
3
+ return if text.blank?
4
+ text.mb_chars.normalize(:kd).to_s.gsub(/[^\x00-\x7F]/,'').downcase.split(/[\s\.\-_]+/)
5
+ end
6
+ end
@@ -0,0 +1,33 @@
1
+ module Mongoid::Search
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ cattr_accessor :search_fields, :match
6
+ end
7
+
8
+ module ClassMethods #:nodoc:
9
+ # Set a field or a number of fields as sources for search
10
+ def search_in(*args)
11
+ options = args.last.is_a?(Hash) && (args.last.keys.first == :match) ? args.pop : {}
12
+ self.match = options[:match] || :any
13
+ self.search_fields = args
14
+
15
+ field :_keywords, :type => Array
16
+ index :_keywords
17
+
18
+ before_save :set_keywords
19
+ end
20
+
21
+ def search(query)
22
+ self.send("#{self.match.to_s}_in", :_keywords => KeywordsExtractor.extract(query).map { |q| /#{q}/i })
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def set_keywords
29
+ self._keywords = self.search_fields.map do |field|
30
+ field.is_a?(Hash) ? self.send(field.keys.first).map(&field.values.first).map { |t| KeywordsExtractor.extract t } : KeywordsExtractor.extract(self.send(field))
31
+ end.flatten.compact.uniq
32
+ end
33
+ end
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mongoid_search}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Mauricio Zaffari"]
12
+ s.date = %q{2010-09-16}
13
+ s.description = %q{Simple full text search implementation.}
14
+ s.email = %q{mauricio@papodenerd.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/mongoid_search.rb",
27
+ "lib/mongoid_search/keywords_extractor.rb",
28
+ "lib/mongoid_search/mongoid_search.rb",
29
+ "mongoid_search.gemspec",
30
+ "spec/mongoid_search_spec.rb",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/mauriciozaffari/mongoid_search}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{Search implementation for Mongoid ORM}
39
+ s.test_files = [
40
+ "spec/mongoid_search_spec.rb",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
50
+ else
51
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
55
+ end
56
+ end
57
+
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "MongoidSearch" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'mongoid_search'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_search
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Mauricio Zaffari
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-16 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 2
31
+ - 9
32
+ version: 1.2.9
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Simple full text search implementation.
36
+ email: mauricio@papodenerd.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.md
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.md
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/mongoid_search.rb
52
+ - lib/mongoid_search/keywords_extractor.rb
53
+ - lib/mongoid_search/mongoid_search.rb
54
+ - mongoid_search.gemspec
55
+ - spec/mongoid_search_spec.rb
56
+ - spec/spec.opts
57
+ - spec/spec_helper.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/mauriciozaffari/mongoid_search
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.7
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Search implementation for Mongoid ORM
90
+ test_files:
91
+ - spec/mongoid_search_spec.rb
92
+ - spec/spec_helper.rb