mongoid_matcher 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +38 -0
- data/lib/mongoid/matcher.rb +36 -0
- data/lib/mongoid_matcher.rb +2 -0
- metadata +21 -5
data/README.rdoc
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
= Purpose
|
2
|
+
|
3
|
+
Enable search over fields.
|
4
|
+
|
5
|
+
|
6
|
+
= Usage
|
7
|
+
|
8
|
+
== Installation
|
9
|
+
|
10
|
+
Add to your Gemfile:
|
11
|
+
gem 'mongoid_matcher'
|
12
|
+
|
13
|
+
And run:
|
14
|
+
bundle install
|
15
|
+
|
16
|
+
|
17
|
+
== Add to model
|
18
|
+
|
19
|
+
class SomeModel
|
20
|
+
include Mongoid::Document
|
21
|
+
include Mongoid::Matcher
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
== Example searches
|
26
|
+
|
27
|
+
Returns model objects where field value is exactly the same:
|
28
|
+
SomeModel.matching('search string') # searches from all fields exact match
|
29
|
+
SomeModel.matching('search string', :fields => [:name, :desc])
|
30
|
+
SomeModel.matching('search string, :types => [Array, String])
|
31
|
+
|
32
|
+
Regexp:
|
33
|
+
SomeModel.matching(/^search string$/) # Regexp search for exact match
|
34
|
+
SomeModel.matching(/search string/) # Regexp search for containing
|
35
|
+
# Read more: http://www.ruby-doc.org/core-1.9.3/Regexp.html
|
36
|
+
|
37
|
+
|
38
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Matcher
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
# Arguments:
|
8
|
+
#
|
9
|
+
# String search_string
|
10
|
+
# Hash opt
|
11
|
+
# types: :all|array_of_types (default: :all, example: [String, Array])
|
12
|
+
# fields: :all|array_of_names (default: :all, example: [:name, :age])
|
13
|
+
def matching(search_string, opt={types: :all, fields: :all})
|
14
|
+
fields = Hash[self.fields]
|
15
|
+
keys = fields.keys
|
16
|
+
|
17
|
+
case opt[:types]
|
18
|
+
when Array
|
19
|
+
keys.keep_if! { |key| opt[:types].include?(fields[key].type) }
|
20
|
+
end
|
21
|
+
|
22
|
+
case opt[:fields]
|
23
|
+
when Array
|
24
|
+
keys.keep_if! { |key| opt[:fields].include?(key) }
|
25
|
+
end
|
26
|
+
|
27
|
+
search = keys.map { |key| Hash[key, search_string] }
|
28
|
+
any_of(search)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Margus P\xC3\xA4rt"
|
@@ -31,7 +31,21 @@ dependencies:
|
|
31
31
|
version: "2.3"
|
32
32
|
type: :runtime
|
33
33
|
version_requirements: *id001
|
34
|
-
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: activesupport
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 3
|
44
|
+
- 1
|
45
|
+
version: "3.1"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
description: "Example search: matching(/search string/, :keys => :all, :types => [String, Array])."
|
35
49
|
email: margus@tione.eu
|
36
50
|
executables: []
|
37
51
|
|
@@ -39,8 +53,10 @@ extensions: []
|
|
39
53
|
|
40
54
|
extra_rdoc_files: []
|
41
55
|
|
42
|
-
files:
|
43
|
-
|
56
|
+
files:
|
57
|
+
- README.rdoc
|
58
|
+
- lib/mongoid/matcher.rb
|
59
|
+
- lib/mongoid_matcher.rb
|
44
60
|
has_rdoc: true
|
45
61
|
homepage: https://github.com/tione/mongoid_matcher
|
46
62
|
licenses: []
|