mongo_scope 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 +40 -1
- data/VERSION +1 -1
- data/examples/the_lonely_example.rb +27 -0
- data/lib/mongo_scope.rb +10 -5
- data/spec/mongo_scope_spec.rb +28 -0
- metadata +3 -1
data/README.rdoc
CHANGED
@@ -1,7 +1,46 @@
|
|
1
|
-
=
|
1
|
+
= MongoScope
|
2
2
|
|
3
3
|
Hacking on some scope methods for MongoDB in Ruby. Inspired by Searchlogic.
|
4
4
|
|
5
|
+
Adds a scope method, as well as helper methods for MongoDB operators.
|
6
|
+
|
7
|
+
= Installation
|
8
|
+
|
9
|
+
Hosted on Gemcutter
|
10
|
+
|
11
|
+
$ gem install mongo_scope
|
12
|
+
|
13
|
+
= Example
|
14
|
+
|
15
|
+
From the examples dir
|
16
|
+
|
17
|
+
# Mongod should be running locally
|
18
|
+
|
19
|
+
require 'rubygems'
|
20
|
+
require 'mongo'
|
21
|
+
require 'mongo_scope'
|
22
|
+
|
23
|
+
# get a collection
|
24
|
+
connection = Mongo::Connection.new
|
25
|
+
db = connection.db('ruby-mongo-test')
|
26
|
+
coll = db.collection("test")
|
27
|
+
|
28
|
+
# Add some data
|
29
|
+
coll.remove
|
30
|
+
coll.save({:first_name => 'Mike', :last_name => 'Harris', :age => 27})
|
31
|
+
coll.save({:first_name => 'Lowell', :age => 28})
|
32
|
+
coll.save({:first_name => 'Lou', :age => 27})
|
33
|
+
|
34
|
+
# using a scope helper method (find works just like the normal Mongo::Collection find)
|
35
|
+
puts coll.scope_in(:first_name => ['Mike','Lowell']).find.count # 2
|
36
|
+
puts coll.scope_gt(:age => 27).find.count # 1
|
37
|
+
|
38
|
+
# chained scopes
|
39
|
+
puts coll.scope_eq(:first_name => /^L/).scope_eq(:age => 27).find.count # 1
|
40
|
+
|
41
|
+
# the raw scope method (this is wrapped by the helper methods)
|
42
|
+
puts coll.scope(:op => '$in', :field => :first_name, :val => ['Mike','Lowell']).find.count # 2
|
43
|
+
|
5
44
|
== Copyright
|
6
45
|
|
7
46
|
Copyright (c) 2009 Mike Harris. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'mongo'
|
3
|
+
require 'mongo_scope'
|
4
|
+
|
5
|
+
# taken from the spec
|
6
|
+
# Mongod should be running locally
|
7
|
+
|
8
|
+
# get a collection
|
9
|
+
connection = Mongo::Connection.new
|
10
|
+
db = connection.db('ruby-mongo-test')
|
11
|
+
coll = db.collection("test")
|
12
|
+
|
13
|
+
# Add some data
|
14
|
+
coll.remove
|
15
|
+
coll.save({:first_name => 'Mike', :last_name => 'Harris', :age => 27})
|
16
|
+
coll.save({:first_name => 'Lowell', :age => 28})
|
17
|
+
coll.save({:first_name => 'Lou', :age => 27})
|
18
|
+
|
19
|
+
# using a scope helper method (find works just like the normal Mongo::Collection find)
|
20
|
+
puts coll.scope_in(:first_name => ['Mike','Lowell']).find.count # 2
|
21
|
+
puts coll.scope_gt(:age => 27).find.count # 1
|
22
|
+
|
23
|
+
# chained scopes
|
24
|
+
puts coll.scope_eq(:first_name => /^L/).scope_eq(:age => 27).find.count # 1
|
25
|
+
|
26
|
+
# the raw scope method (this is wrapped by the helper methods)
|
27
|
+
puts coll.scope(:op => '$in', :field => :first_name, :val => ['Mike','Lowell']).find.count # 2
|
data/lib/mongo_scope.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require 'fattr'
|
3
2
|
require File.join(File.dirname(__FILE__),'mongo_scope','util')
|
4
3
|
|
5
4
|
module MongoScope
|
@@ -11,7 +10,7 @@ module MongoScope
|
|
11
10
|
scope_ops = {:field => ops.keys.first, :val => ops.values.first}
|
12
11
|
ScopedCollection.new(EqScope.new(scope_ops),self)
|
13
12
|
end
|
14
|
-
%w(in gt
|
13
|
+
%w(in nin gt lt gte lte ne mod all size exists).each do |op|
|
15
14
|
define_method("scope_#{op}") do |ops|
|
16
15
|
scope(:op => "$#{op}", :field => ops.keys.first, :val => ops.values.first)
|
17
16
|
end
|
@@ -33,12 +32,18 @@ module MongoScope
|
|
33
32
|
self.coll = coll
|
34
33
|
end
|
35
34
|
def scoped_ops(ops)
|
35
|
+
ops = {'_id' => ops} unless ops.kind_of?(Hash)
|
36
36
|
ops.merge(scope_obj.find_ops)
|
37
37
|
end
|
38
|
-
def find(
|
39
|
-
coll.find(scoped_ops(
|
38
|
+
def find(selector={},options={})
|
39
|
+
coll.find(scoped_ops(selector),options)
|
40
|
+
end
|
41
|
+
def find_one(selector={},options={})
|
42
|
+
coll.find_one(scoped_ops(selector),options)
|
43
|
+
end
|
44
|
+
def method_missing(sym,*args,&b)
|
45
|
+
coll.send(sym,*args,&b)
|
40
46
|
end
|
41
|
-
|
42
47
|
end
|
43
48
|
|
44
49
|
class Scope
|
data/spec/mongo_scope_spec.rb
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
class Object
|
4
|
+
def to_sym_keys
|
5
|
+
h = {}
|
6
|
+
each { |k,v| h[k.to_sym] = v }
|
7
|
+
h
|
8
|
+
end
|
9
|
+
def to_subset(h)
|
10
|
+
h = h.to_sym_keys
|
11
|
+
keys.each { |k| delete(k) unless h.keys.include?(k) }
|
12
|
+
self
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
3
16
|
describe "MongoScope" do
|
4
17
|
before(:all) do
|
5
18
|
@connection = Mongo::Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Connection::DEFAULT_PORT)
|
@@ -37,4 +50,19 @@ describe "MongoScope" do
|
|
37
50
|
scope = MongoScope::Scope.new(:op => '$in', :field => :first_name, :val => ['Mike','Dave'])
|
38
51
|
scope.find_ops.should == {:first_name => {'$in' => ['Mike','Dave']}}
|
39
52
|
end
|
53
|
+
it 'scoped find_one' do
|
54
|
+
@coll.find_one(:age => 27)['first_name'].should == 'Mike'
|
55
|
+
@coll.scope_eq(:first_name => /^L/).find_one(:age => 27)['first_name'].should == 'Lou'
|
56
|
+
end
|
57
|
+
it 'scoped find_one with id' do
|
58
|
+
mike_id = @coll.find_one(:first_name => 'Mike')['_id']
|
59
|
+
lou_id = @coll.find_one(:first_name => 'Lou')['_id']
|
60
|
+
@coll.find_one(mike_id)['first_name'].should == 'Mike'
|
61
|
+
@coll.scope_eq(:first_name => /^L/).find_one(lou_id)['first_name'].should == 'Lou'
|
62
|
+
@coll.scope_eq(:first_name => 'Mike').find_one(lou_id).should_not be
|
63
|
+
end
|
64
|
+
it 'should save' do
|
65
|
+
@coll.scope_eq(:first_name => 'Mike').save(:abc => 42)
|
66
|
+
end
|
67
|
+
|
40
68
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_scope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Harris
|
@@ -28,6 +28,7 @@ files:
|
|
28
28
|
- README.rdoc
|
29
29
|
- Rakefile
|
30
30
|
- VERSION
|
31
|
+
- examples/the_lonely_example.rb
|
31
32
|
- lib/mongo_scope.rb
|
32
33
|
- lib/mongo_scope/util.rb
|
33
34
|
- spec/mongo_scope_spec.rb
|
@@ -67,3 +68,4 @@ test_files:
|
|
67
68
|
- spec/spec_helper.rb
|
68
69
|
- test/mongo_scope_test.rb
|
69
70
|
- test/test_helper.rb
|
71
|
+
- examples/the_lonely_example.rb
|