mongoid-fulltextable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mongoid-fulltextable.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ module Mongoid
4
+ module Fulltextable
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,80 @@
1
+ # encoding: utf-8
2
+
3
+ require 'active_support/concern'
4
+
5
+ module Mongoid
6
+
7
+ module Fulltextable
8
+
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+
13
+ field :_keywords, :type => Array, :accessible => false
14
+
15
+ index :_keywords
16
+
17
+ scope :fulltext, lambda { |str| where(:_keywords => /#{str}/i) }
18
+
19
+ end
20
+
21
+ module ClassMethods
22
+
23
+ # Define fields which will be saved into full-text index.
24
+ def fulltextable_fields(*args)
25
+ before_save :update_keywords
26
+
27
+ self.instance_variable_set(:@index_fields, args)
28
+ end
29
+
30
+ # Define proc which populates data for full-text index.
31
+ def fulltextable(&block)
32
+ before_save :update_keywords
33
+
34
+ self.instance_variable_set(:@index_proc, block)
35
+ end
36
+
37
+ end
38
+
39
+ module InstanceMethods
40
+
41
+ protected
42
+
43
+ def update_keywords
44
+ update_keywords_from_proc if index_proc
45
+ update_keywords_from_fields if index_fields && !index_fields.empty?
46
+ end
47
+
48
+ def update_keywords_from_proc
49
+ self._keywords = []
50
+ instance_eval &index_proc
51
+ end
52
+
53
+ def update_keywords_from_fields
54
+ keywords = index_fields.map do |field|
55
+ value = self.send(field).to_s
56
+ simplified = value.without_accents
57
+
58
+ simplified == value ? value : [value, simplified]
59
+ end.reject(&:blank?).compact.flatten.uniq
60
+
61
+ self._keywords = keywords
62
+ end
63
+
64
+ def index_proc
65
+ @index_proc ||= self.class.instance_variable_get(:@index_proc)
66
+ end
67
+
68
+ def index_fields
69
+ @index_fields ||= self.class.instance_variable_get(:@index_fields)
70
+ end
71
+
72
+ def indexes(*args)
73
+ self._keywords += args.compact.uniq
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mongoid/fulltextable-version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mongoid-fulltextable"
7
+ s.version = Mongoid::Fulltextable::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jiří Zajpt"]
10
+ s.email = ["jzajpt@blueberry.cz"]
11
+ s.homepage = "http://rubygems.org/gems/mongoid-fulltextable"
12
+ s.summary = %q{Simple & stupid 'fulltext' searching for Mongoid.}
13
+ s.description = %q{Simple & stupid 'fulltext' searching for Mongoid.}
14
+
15
+ s.rubyforge_project = "mongoid-fulltextable"
16
+
17
+ s.add_dependency 'activesupport', '~> 3.0.1'
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-fulltextable
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
+ - "Ji\xC5\x99\xC3\xAD Zajpt"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-20 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 1
32
+ version: 3.0.1
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Simple & stupid 'fulltext' searching for Mongoid.
36
+ email:
37
+ - jzajpt@blueberry.cz
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - Rakefile
48
+ - lib/mongoid/fulltextable-version.rb
49
+ - lib/mongoid/fulltextable.rb
50
+ - mongoid-fulltextable.gemspec
51
+ has_rdoc: true
52
+ homepage: http://rubygems.org/gems/mongoid-fulltextable
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project: mongoid-fulltextable
79
+ rubygems_version: 1.3.7
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Simple & stupid 'fulltext' searching for Mongoid.
83
+ test_files: []
84
+