mongo_scope 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +43 -0
- data/VERSION +1 -0
- data/lib/mongo_scope.rb +69 -0
- data/lib/mongo_scope/util.rb +11 -0
- data/spec/mongo_scope_spec.rb +40 -0
- data/spec/spec_helper.rb +11 -0
- data/test/mongo_scope_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +69 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Mike Harris
|
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.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "mongo_scope"
|
8
|
+
gem.summary = "Adds scope methods to Mongo::Collection. Inspired by Searchlogic."
|
9
|
+
gem.email = "mharris717@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/mharris717/mongo_scope"
|
11
|
+
gem.authors = ["Mike Harris"]
|
12
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
13
|
+
end
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'rake/rdoctask'
|
19
|
+
Rake::RDocTask.new do |rdoc|
|
20
|
+
rdoc.rdoc_dir = 'rdoc'
|
21
|
+
rdoc.title = 'mongo_scope'
|
22
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
23
|
+
rdoc.rdoc_files.include('README*')
|
24
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'spec/rake/spectask'
|
28
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
31
|
+
end
|
32
|
+
|
33
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
34
|
+
spec.libs << 'lib' << 'spec'
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
Jeweler::GemcutterTasks.new
|
43
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/mongo_scope.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'fattr'
|
3
|
+
require File.join(File.dirname(__FILE__),'mongo_scope','util')
|
4
|
+
|
5
|
+
module MongoScope
|
6
|
+
module ScopeMethods
|
7
|
+
def scope(ops)
|
8
|
+
ScopedCollection.new(ops,self)
|
9
|
+
end
|
10
|
+
def scope_eq(ops)
|
11
|
+
scope_ops = {:field => ops.keys.first, :val => ops.values.first}
|
12
|
+
ScopedCollection.new(EqScope.new(scope_ops),self)
|
13
|
+
end
|
14
|
+
%w(in gt nin).each do |op|
|
15
|
+
define_method("scope_#{op}") do |ops|
|
16
|
+
scope(:op => "$#{op}", :field => ops.keys.first, :val => ops.values.first)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module CollMethods
|
22
|
+
def count
|
23
|
+
find.count
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class ScopedCollection
|
28
|
+
include ScopeMethods
|
29
|
+
include CollMethods
|
30
|
+
attr_accessor :scope_obj, :coll
|
31
|
+
def initialize(scope_ops,coll)
|
32
|
+
self.scope_obj = Scope.new(scope_ops)
|
33
|
+
self.coll = coll
|
34
|
+
end
|
35
|
+
def scoped_ops(ops)
|
36
|
+
ops.merge(scope_obj.find_ops)
|
37
|
+
end
|
38
|
+
def find(ops={})
|
39
|
+
coll.find(scoped_ops(ops))
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
class Scope
|
45
|
+
def self.new(ops)
|
46
|
+
ops.respond_to?(:find_ops) ? ops : super(ops)
|
47
|
+
end
|
48
|
+
attr_accessor :field, :op, :val
|
49
|
+
include FromHash
|
50
|
+
def find_ops
|
51
|
+
{field => {op => val}}
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class EqScope
|
56
|
+
attr_accessor :field, :val
|
57
|
+
include FromHash
|
58
|
+
def find_ops
|
59
|
+
{field => val}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.use!
|
64
|
+
[CollMethods,ScopeMethods].each { |mod| ::Mongo::Collection.send(:include,mod) }
|
65
|
+
end
|
66
|
+
|
67
|
+
use!
|
68
|
+
end
|
69
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "MongoScope" do
|
4
|
+
before(:all) do
|
5
|
+
@connection = Mongo::Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Connection::DEFAULT_PORT)
|
6
|
+
@db = @connection.db('ruby-mongo-test')
|
7
|
+
@coll = @db.collection("test")
|
8
|
+
end
|
9
|
+
before(:each) do
|
10
|
+
@coll.remove
|
11
|
+
@coll.save({:first_name => 'Mike', :last_name => 'Harris', :age => 27})
|
12
|
+
@coll.save({:first_name => 'Lowell', :age => 28})
|
13
|
+
@coll.save({:first_name => 'Lou', :age => 27})
|
14
|
+
end
|
15
|
+
it 'scope method' do
|
16
|
+
@coll.scope(:op => '$in', :field => :first_name, :val => ['Mike','Dave']).find.count.should == 1
|
17
|
+
end
|
18
|
+
it 'helper method - in' do
|
19
|
+
@coll.scope_in(:first_name => ['Mike','Dave']).find.count.should == 1
|
20
|
+
end
|
21
|
+
it 'helper method - nin' do
|
22
|
+
@coll.scope_nin(:first_name => ['Mike','Dave']).find.count.should == 2
|
23
|
+
end
|
24
|
+
it 'helper method - eq' do
|
25
|
+
@coll.scope_eq(:first_name => 'Mike').find.count.should == 1
|
26
|
+
end
|
27
|
+
it 'helper method - gt' do
|
28
|
+
@coll.scope_gt(:age => 26).find.count.should == 3
|
29
|
+
@coll.scope_gt(:age => 27).find.count.should == 1
|
30
|
+
end
|
31
|
+
it 'chained scopes' do
|
32
|
+
@coll.scope_eq(:age => 27).count.should == 2
|
33
|
+
@coll.scope_eq(:age => 27).scope_eq(:first_name => 'Mike').count.should == 1
|
34
|
+
@coll.scope_eq(:first_name => /^L/).scope_gt(:age => 27).count.should == 1
|
35
|
+
end
|
36
|
+
it 'scope find ops' do
|
37
|
+
scope = MongoScope::Scope.new(:op => '$in', :field => :first_name, :val => ['Mike','Dave'])
|
38
|
+
scope.find_ops.should == {:first_name => {'$in' => ['Mike','Dave']}}
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongo_scope
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Harris
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-11 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: mharris717@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- LICENSE
|
28
|
+
- README.rdoc
|
29
|
+
- Rakefile
|
30
|
+
- VERSION
|
31
|
+
- lib/mongo_scope.rb
|
32
|
+
- lib/mongo_scope/util.rb
|
33
|
+
- spec/mongo_scope_spec.rb
|
34
|
+
- spec/spec_helper.rb
|
35
|
+
- test/mongo_scope_test.rb
|
36
|
+
- test/test_helper.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://github.com/mharris717/mongo_scope
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --charset=UTF-8
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.3.5
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Adds scope methods to Mongo::Collection. Inspired by Searchlogic.
|
65
|
+
test_files:
|
66
|
+
- spec/mongo_scope_spec.rb
|
67
|
+
- spec/spec_helper.rb
|
68
|
+
- test/mongo_scope_test.rb
|
69
|
+
- test/test_helper.rb
|