mongo_scope 0.0.3 → 0.0.4
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/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/mongo_scope.rb +58 -3
- data/spec/mongo_scope_spec.rb +15 -1
- metadata +13 -4
data/Rakefile
CHANGED
@@ -9,6 +9,7 @@ begin
|
|
9
9
|
gem.email = "mharris717@gmail.com"
|
10
10
|
gem.homepage = "http://github.com/mharris717/mongo_scope"
|
11
11
|
gem.authors = ["Mike Harris"]
|
12
|
+
gem.add_dependency 'mongo'
|
12
13
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
13
14
|
end
|
14
15
|
rescue LoadError
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/lib/mongo_scope.rb
CHANGED
@@ -1,6 +1,21 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require File.join(File.dirname(__FILE__),'mongo_scope','util')
|
3
3
|
|
4
|
+
class SumBdy
|
5
|
+
def sum_by_raw(ops)
|
6
|
+
reduce_function = "function (obj, prev) { prev.count += (obj.#{ops[:sum_field]} ? obj.#{ops[:sum_field]} : 0); }"
|
7
|
+
code = Mongo::Code.new(reduce_function)
|
8
|
+
group([ops[:key]].flatten, ops[:filter]||{}, {"count" => 0},code)
|
9
|
+
end
|
10
|
+
def sum_by(ops)
|
11
|
+
sum_by_raw(ops).inject({}) { |h,a| k = ops[:key]; h.merge(a[k] => a['count'])}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Mongo::Collection
|
16
|
+
#include SumBy
|
17
|
+
end
|
18
|
+
|
4
19
|
module MongoScope
|
5
20
|
module ScopeMethods
|
6
21
|
def raw_scope(ops)
|
@@ -21,29 +36,69 @@ module MongoScope
|
|
21
36
|
def count
|
22
37
|
find.count
|
23
38
|
end
|
39
|
+
def to_scoped
|
40
|
+
MongoScope::ScopedCollection.new(nil,self)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class ScopedCursor
|
45
|
+
attr_accessor :cursor
|
46
|
+
include FromHash
|
47
|
+
def each(&b)
|
48
|
+
cursor.each do |x|
|
49
|
+
yield(MongoRow.new(x))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
def method_missing(sym,*args,&b)
|
53
|
+
cursor.send(sym,*args,&b)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class MongoRow
|
58
|
+
attr_accessor :h
|
59
|
+
def initialize(h)
|
60
|
+
@h = h
|
61
|
+
end
|
62
|
+
def method_missing(sym,*args,&b)
|
63
|
+
h.send(sym,*args,&b)
|
64
|
+
end
|
24
65
|
end
|
25
66
|
|
26
67
|
class ScopedCollection
|
27
68
|
include ScopeMethods
|
28
69
|
include CollMethods
|
70
|
+
#include SumBy
|
29
71
|
attr_accessor :scope_obj, :coll
|
72
|
+
include Enumerable
|
30
73
|
def initialize(scope_ops,coll)
|
31
|
-
self.scope_obj = Scope.new(scope_ops)
|
74
|
+
self.scope_obj = (scope_ops ? Scope.new(scope_ops) : nil)
|
32
75
|
self.coll = coll
|
33
76
|
end
|
34
77
|
def scoped_ops(ops)
|
78
|
+
return ops unless scope_obj
|
35
79
|
ops = {'_id' => ops} unless ops.kind_of?(Hash)
|
36
80
|
ops.merge(scope_obj.find_ops)
|
37
81
|
end
|
38
82
|
def find(selector={},options={})
|
39
|
-
coll.find(scoped_ops(selector),options)
|
83
|
+
ScopedCursor.new(:cursor => coll.find(scoped_ops(selector),options))
|
40
84
|
end
|
41
85
|
def find_one(selector={},options={})
|
42
|
-
coll.find_one(scoped_ops(selector),options)
|
86
|
+
res = coll.find_one(scoped_ops(selector),options)
|
87
|
+
res ? MongoRow.new(res) : res
|
88
|
+
end
|
89
|
+
def remove(ops={})
|
90
|
+
coll.remove(scoped_ops(ops))
|
43
91
|
end
|
44
92
|
def method_missing(sym,*args,&b)
|
45
93
|
coll.send(sym,*args,&b)
|
46
94
|
end
|
95
|
+
def each(&b)
|
96
|
+
coll.each(&b) #should this be scoped?
|
97
|
+
end
|
98
|
+
def group(k,filter,*args)
|
99
|
+
filter = scoped_ops(filter || {})
|
100
|
+
coll.group(k,filter,*args)
|
101
|
+
end
|
47
102
|
end
|
48
103
|
|
49
104
|
class Scope
|
data/spec/mongo_scope_spec.rb
CHANGED
@@ -17,7 +17,7 @@ describe "MongoScope" do
|
|
17
17
|
before(:all) do
|
18
18
|
@connection = Mongo::Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Connection::DEFAULT_PORT)
|
19
19
|
@db = @connection.db('ruby-mongo-test')
|
20
|
-
@coll = @db.collection("test")
|
20
|
+
@coll = @db.collection("test").to_scoped
|
21
21
|
end
|
22
22
|
before(:each) do
|
23
23
|
@coll.remove
|
@@ -64,5 +64,19 @@ describe "MongoScope" do
|
|
64
64
|
it 'should save' do
|
65
65
|
@coll.scope_eq(:first_name => 'Mike').save(:abc => 42)
|
66
66
|
end
|
67
|
+
it 'scoped remove' do
|
68
|
+
@coll.scope_eq(:first_name => 'Mike').remove
|
69
|
+
@coll.count.should == 2
|
70
|
+
end
|
71
|
+
it 'scoped group' do
|
72
|
+
@coll.remove
|
73
|
+
# @coll.save('a' => 'a', 'b' => 1)
|
74
|
+
# @coll.save("a" => 'a', 'b' => 3)
|
75
|
+
# @coll.save('a' => 'b', 'b' => 2)
|
76
|
+
#
|
77
|
+
# @coll.sum_by(:key => 'a', :sum_field => 'b').should == {'a' => 4, 'b' => 2}
|
78
|
+
end
|
67
79
|
|
68
80
|
end
|
81
|
+
|
82
|
+
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Harris
|
@@ -9,10 +9,19 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-04-15 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mongo
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
16
25
|
description:
|
17
26
|
email: mharris717@gmail.com
|
18
27
|
executables: []
|