peace_love 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/examples/usage.eg.rb +120 -0
- data/lib/peace_love/collection.rb +8 -1
- data/lib/peace_love/document.rb +8 -0
- data/peace_love.gemspec +3 -3
- metadata +4 -4
- data/examples/loading.eg.rb +0 -98
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'eg.helper'
|
2
|
+
require 'angry_hash'
|
3
|
+
require 'peace_love'
|
4
|
+
|
5
|
+
|
6
|
+
module Kind
|
7
|
+
def claws; "dainty" end
|
8
|
+
|
9
|
+
def for_kids?
|
10
|
+
fictional? && cartoon?
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module Bear
|
15
|
+
include PeaceLove::Doc
|
16
|
+
sub_doc :kind, Kind
|
17
|
+
sub_col :lovers, Bear
|
18
|
+
|
19
|
+
def claws; "woah" end
|
20
|
+
def liver
|
21
|
+
super.upcase
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
eg.setup do
|
26
|
+
PeaceLove.db = $db
|
27
|
+
|
28
|
+
@mbears = $db['bears']
|
29
|
+
@mbears.remove()
|
30
|
+
|
31
|
+
Bear.collection = 'bears'
|
32
|
+
@plbears = PeaceLove['bears']
|
33
|
+
end
|
34
|
+
eg.helpers do
|
35
|
+
attr_reader :mbears, :plbears
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
eg 'loading a document with a mixin' do
|
40
|
+
mbears.insert(:name => 'yogi', :liver => 'pure', :kind => {:fictional => true, :cartoon => true})
|
41
|
+
|
42
|
+
yogi = plbears.find_one(:name => 'yogi')
|
43
|
+
|
44
|
+
Assert( yogi.claws == 'woah' )
|
45
|
+
Assert( yogi.kind.claws == 'dainty' )
|
46
|
+
Assert( yogi.name == 'yogi' )
|
47
|
+
|
48
|
+
Assert( yogi.liver == 'PURE' )
|
49
|
+
Assert( yogi.kind.for_kids? )
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
eg 'loading a non-existent doc' do
|
54
|
+
b = plbears.find_one(:name => 'bertrand')
|
55
|
+
|
56
|
+
Assert( b.nil? )
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
eg 'wrapping the mongo cursor' do
|
61
|
+
mbears.insert(:name => 'yogi' , :liver => 'pure', :kind => {:fictional => true, :cartoon => true})
|
62
|
+
mbears.insert(:name => 'humphrey', :liver => 'cihrrotic', :kind => {:fictional => true, :cartoon => false})
|
63
|
+
|
64
|
+
|
65
|
+
i = 0
|
66
|
+
plbears.find.each {|b|
|
67
|
+
|
68
|
+
case i
|
69
|
+
when 0
|
70
|
+
Assert( b.kind.claws == 'dainty' )
|
71
|
+
Assert( b.liver == 'PURE' )
|
72
|
+
when 1
|
73
|
+
Assert( b.kind.claws == 'dainty' )
|
74
|
+
Assert( b.liver == 'CIHRROTIC' )
|
75
|
+
end
|
76
|
+
|
77
|
+
i += 1
|
78
|
+
}
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
eg 'mixing in to each element of a sub collection' do
|
83
|
+
mbears.remove()
|
84
|
+
mbears.insert(:name => 'yogi', :liver => 'pure', :kind => {:fictional => true, :cartoon => true}, :lovers =>
|
85
|
+
[
|
86
|
+
{:name => 'mrs. yogi', :liver => 'donated'}, {:name => 'yogi paw', :liver => 'jaundiced'}
|
87
|
+
])
|
88
|
+
|
89
|
+
yogi = plbears.find_one(:name => 'yogi')
|
90
|
+
|
91
|
+
Assert( yogi.lovers[0].liver == 'DONATED' )
|
92
|
+
Assert( yogi.lovers[1].liver == 'JAUNDICED' )
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
eg 'building a blank doc' do
|
97
|
+
yogi = plbears.build
|
98
|
+
|
99
|
+
Assert( yogi.claws == 'woah' )
|
100
|
+
Assert( yogi.kind.claws == 'dainty' )
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
eg 'saving a hash' do
|
105
|
+
h = AngryHash[ :somesuch => 'second thing' ]
|
106
|
+
id = plbears.insert( h )
|
107
|
+
|
108
|
+
Assert( plbears.find_one(id).somesuch == 'second thing' )
|
109
|
+
end
|
110
|
+
|
111
|
+
|
112
|
+
eg 'the id accessor works in ruby 1.8 & 1.9' do
|
113
|
+
Assert(Bear.build(:id => 'abc').id == 'abc')
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
# TODO
|
118
|
+
#eg 'setting the mixin on the collection' do
|
119
|
+
#PeaceLove['bears'].mixin = Bear
|
120
|
+
#end
|
@@ -1,12 +1,19 @@
|
|
1
1
|
module PeaceLove
|
2
2
|
class Collection
|
3
|
-
attr_accessor :mixin
|
4
3
|
attr_reader :mongo_collection
|
5
4
|
|
6
5
|
def initialize(collection)
|
7
6
|
@collection = @mongo_collection = collection
|
8
7
|
end
|
9
8
|
|
9
|
+
def mixin=(mixin)
|
10
|
+
mixin.collection = @mongo_collection.name
|
11
|
+
end
|
12
|
+
def set_mixin(mixin)
|
13
|
+
@mixin = mixin
|
14
|
+
end
|
15
|
+
attr_reader :mixin
|
16
|
+
|
10
17
|
def build(seed={})
|
11
18
|
__wrap( seed )
|
12
19
|
end
|
data/lib/peace_love/document.rb
CHANGED
@@ -13,6 +13,7 @@ module PeaceLove
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
|
16
17
|
def object_extensions
|
17
18
|
@object_extensions ||= Hash.new {|h,k| h[k] = []}
|
18
19
|
end
|
@@ -90,6 +91,13 @@ module PeaceLove
|
|
90
91
|
end
|
91
92
|
|
92
93
|
module ClassMethods
|
94
|
+
def collection=(collection_name)
|
95
|
+
@collection = PeaceLove[collection_name]
|
96
|
+
@collection.set_mixin(self)
|
97
|
+
end
|
98
|
+
|
99
|
+
def collection; @collection end
|
100
|
+
|
93
101
|
def sub_document(field,mod,options={})
|
94
102
|
Doc.register_mixin(self,field,mod,options)
|
95
103
|
end
|
data/peace_love.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{peace_love}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Lachie Cox"]
|
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
"Rakefile",
|
19
19
|
"VERSION",
|
20
20
|
"examples/eg.helper.rb",
|
21
|
-
"examples/
|
21
|
+
"examples/usage.eg.rb",
|
22
22
|
"lib/peace_love.rb",
|
23
23
|
"lib/peace_love/collection.rb",
|
24
24
|
"lib/peace_love/cursor.rb",
|
@@ -32,7 +32,7 @@ Gem::Specification.new do |s|
|
|
32
32
|
s.summary = %q{Peace, Love and Mongo.}
|
33
33
|
s.test_files = [
|
34
34
|
"examples/eg.helper.rb",
|
35
|
-
"examples/
|
35
|
+
"examples/usage.eg.rb"
|
36
36
|
]
|
37
37
|
|
38
38
|
if s.respond_to? :specification_version then
|
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
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Lachie Cox
|
@@ -45,7 +45,7 @@ files:
|
|
45
45
|
- Rakefile
|
46
46
|
- VERSION
|
47
47
|
- examples/eg.helper.rb
|
48
|
-
- examples/
|
48
|
+
- examples/usage.eg.rb
|
49
49
|
- lib/peace_love.rb
|
50
50
|
- lib/peace_love/collection.rb
|
51
51
|
- lib/peace_love/cursor.rb
|
@@ -83,4 +83,4 @@ specification_version: 3
|
|
83
83
|
summary: Peace, Love and Mongo.
|
84
84
|
test_files:
|
85
85
|
- examples/eg.helper.rb
|
86
|
-
- examples/
|
86
|
+
- examples/usage.eg.rb
|
data/examples/loading.eg.rb
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
require 'eg.helper'
|
2
|
-
require 'angry_hash'
|
3
|
-
require 'peace_love'
|
4
|
-
|
5
|
-
|
6
|
-
module Kind
|
7
|
-
def claws; "dainty" end
|
8
|
-
|
9
|
-
def for_kids?
|
10
|
-
fictional? && cartoon?
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
module Bear
|
15
|
-
include PeaceLove::Doc
|
16
|
-
sub_doc :kind, Kind
|
17
|
-
sub_col :lovers, Bear
|
18
|
-
|
19
|
-
def claws; "woah" end
|
20
|
-
def liver
|
21
|
-
super.upcase
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
eg.setup do
|
26
|
-
PeaceLove.db = $db
|
27
|
-
PeaceLove['bears'].mixin = nil
|
28
|
-
end
|
29
|
-
|
30
|
-
|
31
|
-
eg 'loading doc' do
|
32
|
-
$db['bears'].remove()
|
33
|
-
$db['bears'].insert(:name => 'yogi', :liver => 'pure', :kind => {:fictional => true, :cartoon => true})
|
34
|
-
|
35
|
-
PeaceLove['bears'].mixin = Bear
|
36
|
-
yogi = PeaceLove['bears'].find_one(:name => 'yogi')
|
37
|
-
Show(yogi.claws)
|
38
|
-
Show(yogi.name)
|
39
|
-
|
40
|
-
Assert(yogi.liver == 'PURE')
|
41
|
-
Show(yogi.kind.for_kids?)
|
42
|
-
end
|
43
|
-
|
44
|
-
eg 'loading a non-existant doc' do
|
45
|
-
$db['bears'].remove()
|
46
|
-
PeaceLove['bears'].mixin = Bear
|
47
|
-
b = PeaceLove['bears'].find_one(:name => 'bertrand')
|
48
|
-
Assert( b.nil? )
|
49
|
-
end
|
50
|
-
|
51
|
-
eg 'wrapping the cursor' do
|
52
|
-
$db['bears'].remove()
|
53
|
-
$db['bears'].insert(:name => 'yogi' , :liver => 'pure', :kind => {:fictional => true, :cartoon => true})
|
54
|
-
$db['bears'].insert(:name => 'humphrey', :liver => 'cihrrotic', :kind => {:fictional => true, :cartoon => false})
|
55
|
-
|
56
|
-
PeaceLove['bears'].mixin = Bear
|
57
|
-
|
58
|
-
i = 0
|
59
|
-
PeaceLove['bears'].find.each {|b|
|
60
|
-
Show(b)
|
61
|
-
if i == 0
|
62
|
-
Assert(b.liver == 'PURE')
|
63
|
-
elsif i == 1
|
64
|
-
Assert(b.liver == 'CIHRROTIC')
|
65
|
-
end
|
66
|
-
i += 1
|
67
|
-
}
|
68
|
-
end
|
69
|
-
|
70
|
-
eg 'sub collection' do
|
71
|
-
$db['bears'].remove()
|
72
|
-
$db['bears'].insert(:name => 'yogi', :liver => 'pure', :kind => {:fictional => true, :cartoon => true}, :lovers =>
|
73
|
-
[
|
74
|
-
{:name => 'mrs. yogi', :liver => 'donated'}, {:name => 'yogi paw', :liver => 'jaundiced'}
|
75
|
-
])
|
76
|
-
|
77
|
-
PeaceLove['bears'].mixin = Bear
|
78
|
-
yogi = PeaceLove['bears'].find_one(:name => 'yogi')
|
79
|
-
|
80
|
-
Assert(yogi.lovers[0].liver == 'DONATED')
|
81
|
-
Assert(yogi.lovers[1].liver == 'JAUNDICED')
|
82
|
-
end
|
83
|
-
|
84
|
-
eg 'blank doc' do
|
85
|
-
PeaceLove['bears'].mixin = Bear
|
86
|
-
yogi = PeaceLove['bears'].build
|
87
|
-
yogi.claws
|
88
|
-
end
|
89
|
-
|
90
|
-
eg 'saving object' do
|
91
|
-
h = AngryHash[ :somesuch => 'second thing' ]
|
92
|
-
id = PeaceLove['bears'].insert( h )
|
93
|
-
PeaceLove['bears'].find_one(id)
|
94
|
-
end
|
95
|
-
|
96
|
-
eg 'id accessor' do
|
97
|
-
Assert(Bear.build(:id => 'abc').id == 'abc')
|
98
|
-
end
|