dm-searcher 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +74 -0
- data/Rakefile +2 -0
- data/dm-searcher.gemspec +19 -0
- data/lib/dm-searcher/searcher.rb +107 -0
- data/lib/dm-searcher/version.rb +5 -0
- data/lib/dm-searcher.rb +6 -0
- data/rvmrc.example +1 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Tower He
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# DataMapper::Searcher
|
2
|
+
|
3
|
+
DataMapper plugin providing for searching models with nested conditions.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'dm-searcher'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install dm-searcher
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# Models
|
23
|
+
class User
|
24
|
+
include DataMapper::Resource
|
25
|
+
include DataMapper::Searcher
|
26
|
+
|
27
|
+
property :id, Serial
|
28
|
+
property :name, String
|
29
|
+
|
30
|
+
has n, :roles
|
31
|
+
end
|
32
|
+
|
33
|
+
class Role
|
34
|
+
include DataMapper::Resource
|
35
|
+
include DataMapper::Searcher
|
36
|
+
|
37
|
+
property :id, Serial
|
38
|
+
property :name, String
|
39
|
+
|
40
|
+
belongs_to :user
|
41
|
+
end
|
42
|
+
|
43
|
+
# Search for records
|
44
|
+
User.search('name.like' => '%Tower%')
|
45
|
+
# => Returns the users whose name contains 'Tower'.
|
46
|
+
|
47
|
+
User.search('id.gt' => 10)
|
48
|
+
# => Returns the users whose id is greater than 10.
|
49
|
+
|
50
|
+
User.search('roles.name' => 'Administrators')
|
51
|
+
# => Returns the users whose role is 'Administrator'.
|
52
|
+
|
53
|
+
User.search('name.like' => '%Tower%',
|
54
|
+
'id.gt' => 10,
|
55
|
+
'roles.name' => 'Administrators')
|
56
|
+
# => Returns the users whose name contains 'Tower', id is greater than 10 and role is 'Administrators'.
|
57
|
+
|
58
|
+
# Combining the result
|
59
|
+
User.search('name.like' => '%Tower%') + User.search('id.gt' => 10)
|
60
|
+
User.search('name.like' => '%Tower%') | User.search('id.gt' => 10)
|
61
|
+
# => Returns the users whose name contains 'Tower' or id is greater than 10.
|
62
|
+
|
63
|
+
User.search('name.like' => '%Tower%') & User.search('id.gt' => 10)
|
64
|
+
User.search('name.like' => '%Tower%') - User.search('id.gt' => 10)
|
65
|
+
# => Returns the users whose name contains 'Tower' and id is greater than 10.
|
66
|
+
```
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
1. Fork it
|
71
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
72
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
73
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
74
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/dm-searcher.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/dm-searcher/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Tower He"]
|
6
|
+
gem.email = ["towerhe@gmail.com"]
|
7
|
+
gem.description = %q{DataMapper plugin providing for searching models with nested conditions.}
|
8
|
+
gem.summary = %q{DataMapper plugin providing for searching models with nested conditions.}
|
9
|
+
gem.homepage = "https://github.com/dm-searcher"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "dm-searcher"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Dm::Searcher::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency 'data_mapper', '~> 1.2.0'
|
19
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Searcher
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
# Valid symbol operators for the conditions are:
|
7
|
+
#
|
8
|
+
# gt # greater than
|
9
|
+
# lt # less than
|
10
|
+
# gte # greater than or equal
|
11
|
+
# lte # less than or equal
|
12
|
+
# not # not equal
|
13
|
+
# eql # equal
|
14
|
+
# like # like
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
#
|
18
|
+
# # Models
|
19
|
+
# class User
|
20
|
+
# include DataMapper::Resource
|
21
|
+
# include DataMapper::Searcher
|
22
|
+
#
|
23
|
+
# property :id, Serial
|
24
|
+
# property :name, String
|
25
|
+
#
|
26
|
+
# has n, :roles
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# class Role
|
30
|
+
# include DataMapper::Resource
|
31
|
+
# include DataMapper::Searcher
|
32
|
+
#
|
33
|
+
# property :id, Serial
|
34
|
+
# property :name, String
|
35
|
+
#
|
36
|
+
# belongs_to :user
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# # Search for records
|
40
|
+
# User.search('name.like' => '%Tower%')
|
41
|
+
# # => Returns the users whose name contains 'Tower'.
|
42
|
+
#
|
43
|
+
# User.search('id.gt' => 10)
|
44
|
+
# # => Returns the users whose id is greater than 10.
|
45
|
+
#
|
46
|
+
# User.search('roles.name' => 'Administrators')
|
47
|
+
# # => Returns the users whose role is 'Administrator'.
|
48
|
+
#
|
49
|
+
# User.search('name.like' => '%Tower%',
|
50
|
+
# 'id.gt' => 10,
|
51
|
+
# 'roles.name' => 'Administrators')
|
52
|
+
# # => Returns the users whose name contains 'Tower', id is greater than 10 and role is 'Administrators'.
|
53
|
+
#
|
54
|
+
# # Combining the result
|
55
|
+
# User.search('name.like' => '%Tower%') + User.search('id.gt' => 10)
|
56
|
+
# User.search('name.like' => '%Tower%') | User.search('id.gt' => 10)
|
57
|
+
# # => Returns the users whose name contains 'Tower' or id is greater than 10.
|
58
|
+
#
|
59
|
+
# User.search('name.like' => '%Tower%') & User.search('id.gt' => 10)
|
60
|
+
# User.search('name.like' => '%Tower%') - User.search('id.gt' => 10)
|
61
|
+
# # => Returns the users whose name contains 'Tower' and id is greater than 10.
|
62
|
+
#
|
63
|
+
# @return [DataMapper::Collection] Matched records
|
64
|
+
def search(opts = {})
|
65
|
+
options = parse_opts(self, opts)
|
66
|
+
|
67
|
+
self.all(options)
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def parse_opts(model, opts)
|
73
|
+
options = {}
|
74
|
+
|
75
|
+
opts.keys.each do |k|
|
76
|
+
key = parse_key(model, k)
|
77
|
+
|
78
|
+
options[key] = opts[k] if key && !opts[k].blank?
|
79
|
+
end
|
80
|
+
|
81
|
+
options
|
82
|
+
end
|
83
|
+
|
84
|
+
def parse_key(model, key)
|
85
|
+
path, property, operator = model, nil, nil
|
86
|
+
|
87
|
+
steps = key.to_s.split(/\./)
|
88
|
+
steps.each do |s|
|
89
|
+
s = s.to_sym
|
90
|
+
|
91
|
+
if model.relationships.named?(s)
|
92
|
+
path = path.send(s)
|
93
|
+
model = path.model
|
94
|
+
elsif model.properties.named?(s)
|
95
|
+
property = path == model ? s : path.send(s)
|
96
|
+
elsif (DataMapper::Query::Conditions::Comparison.slugs | [:not, :asc, :desc]).include?(s)
|
97
|
+
operator = property.send(s)
|
98
|
+
else
|
99
|
+
raise "#{key} is not a valid property or operator."
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
operator || property
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
data/lib/dm-searcher.rb
ADDED
data/rvmrc.example
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use 1.9.3@dm-searcher
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm-searcher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tower He
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: data_mapper
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.2.0
|
30
|
+
description: DataMapper plugin providing for searching models with nested conditions.
|
31
|
+
email:
|
32
|
+
- towerhe@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- dm-searcher.gemspec
|
43
|
+
- lib/dm-searcher.rb
|
44
|
+
- lib/dm-searcher/searcher.rb
|
45
|
+
- lib/dm-searcher/version.rb
|
46
|
+
- rvmrc.example
|
47
|
+
homepage: https://github.com/dm-searcher
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.24
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: DataMapper plugin providing for searching models with nested conditions.
|
71
|
+
test_files: []
|
72
|
+
has_rdoc:
|