mordor 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -11
- data/lib/mordor/collection.rb +38 -6
- data/lib/mordor/resource.rb +34 -6
- data/mordor.gemspec +2 -3
- data/spec/mordor/collection_spec.rb +50 -0
- data/spec/mordor/resource_spec.rb +81 -3
- data/spec/spec_helper.rb +6 -3
- metadata +19 -33
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mordor (0.1.
|
4
|
+
mordor (0.1.2)
|
5
5
|
bson_ext
|
6
6
|
extlib
|
7
7
|
json
|
@@ -11,16 +11,12 @@ GEM
|
|
11
11
|
specs:
|
12
12
|
bson (1.4.0)
|
13
13
|
bson_ext (1.4.0)
|
14
|
-
columnize (0.3.4)
|
15
14
|
diff-lcs (1.1.3)
|
16
15
|
extlib (0.9.15)
|
17
16
|
json (1.6.1)
|
18
|
-
linecache (0.46)
|
19
|
-
rbx-require-relative (> 0.0.4)
|
20
17
|
mongo (1.4.0)
|
21
18
|
bson (= 1.4.0)
|
22
19
|
rake (0.9.2.2)
|
23
|
-
rbx-require-relative (0.0.5)
|
24
20
|
rspec (2.7.0)
|
25
21
|
rspec-core (~> 2.7.0)
|
26
22
|
rspec-expectations (~> 2.7.0)
|
@@ -29,11 +25,6 @@ GEM
|
|
29
25
|
rspec-expectations (2.7.0)
|
30
26
|
diff-lcs (~> 1.1.2)
|
31
27
|
rspec-mocks (2.7.0)
|
32
|
-
ruby-debug (0.10.4)
|
33
|
-
columnize (>= 0.1)
|
34
|
-
ruby-debug-base (~> 0.10.4.0)
|
35
|
-
ruby-debug-base (0.10.4)
|
36
|
-
linecache (>= 0.3)
|
37
28
|
|
38
29
|
PLATFORMS
|
39
30
|
ruby
|
@@ -46,4 +37,3 @@ DEPENDENCIES
|
|
46
37
|
mordor!
|
47
38
|
rake
|
48
39
|
rspec (~> 2.0)
|
49
|
-
ruby-debug
|
data/lib/mordor/collection.rb
CHANGED
@@ -7,20 +7,40 @@ module Mordor
|
|
7
7
|
@cursor = cursor
|
8
8
|
end
|
9
9
|
|
10
|
+
def to_a
|
11
|
+
array = []
|
12
|
+
unless @cursor.is_a? Array
|
13
|
+
@cursor.each do |element|
|
14
|
+
if element.is_a? @klass
|
15
|
+
array << element
|
16
|
+
else
|
17
|
+
array << @klass.new(element)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
else
|
21
|
+
array = @cursor.dup
|
22
|
+
end
|
23
|
+
array
|
24
|
+
end
|
25
|
+
|
10
26
|
def each
|
11
27
|
@cursor.each do |element|
|
12
|
-
if element
|
13
|
-
yield
|
28
|
+
if element.is_a? @klass
|
29
|
+
yield element
|
14
30
|
else
|
15
|
-
|
31
|
+
yield @klass.new(element)
|
16
32
|
end
|
17
33
|
end
|
18
34
|
end
|
19
35
|
|
20
36
|
def first
|
21
|
-
|
22
|
-
|
23
|
-
|
37
|
+
if @cursor.is_a? Array
|
38
|
+
@cursor.first ? @klass.new(@cursor.first) : nil
|
39
|
+
else
|
40
|
+
result = @cursor.first
|
41
|
+
@cursor.rewind!
|
42
|
+
result ? @klass.new(result) : nil
|
43
|
+
end
|
24
44
|
end
|
25
45
|
|
26
46
|
def size
|
@@ -45,5 +65,17 @@ module Mordor
|
|
45
65
|
end
|
46
66
|
res.to_json
|
47
67
|
end
|
68
|
+
|
69
|
+
def merge(other_collection)
|
70
|
+
Collection.new(@klass, (self.to_a + other_collection.to_a))
|
71
|
+
end
|
72
|
+
alias_method :+, :merge
|
73
|
+
|
74
|
+
def merge!(other_collection)
|
75
|
+
unless @cursor.is_a? Array
|
76
|
+
@cursor = @cursor.to_a
|
77
|
+
end
|
78
|
+
@cursor += other_collection.to_a
|
79
|
+
end
|
48
80
|
end
|
49
81
|
end
|
data/lib/mordor/resource.rb
CHANGED
@@ -42,6 +42,14 @@ module Mordor
|
|
42
42
|
value
|
43
43
|
end
|
44
44
|
|
45
|
+
def new?
|
46
|
+
return self._id == nil
|
47
|
+
end
|
48
|
+
|
49
|
+
def saved?
|
50
|
+
return !new?
|
51
|
+
end
|
52
|
+
|
45
53
|
def save
|
46
54
|
unless self._id
|
47
55
|
insert_id = self.class.collection.insert(self.to_hash)
|
@@ -49,9 +57,11 @@ module Mordor
|
|
49
57
|
else
|
50
58
|
insert_id = self.update
|
51
59
|
end
|
52
|
-
|
60
|
+
saved?
|
53
61
|
end
|
54
62
|
|
63
|
+
alias_method :save!, :save
|
64
|
+
|
55
65
|
def update
|
56
66
|
insert_id = self.class.collection.update({:_id => self._id}, self.to_hash)
|
57
67
|
insert_id
|
@@ -72,6 +82,12 @@ module Mordor
|
|
72
82
|
end
|
73
83
|
|
74
84
|
module ClassMethods
|
85
|
+
def create(attributes = {})
|
86
|
+
resource = self.new(attributes)
|
87
|
+
resource.save
|
88
|
+
resource
|
89
|
+
end
|
90
|
+
|
75
91
|
def all(options = {})
|
76
92
|
Collection.new(self, collection.find({}, options).to_a)
|
77
93
|
end
|
@@ -104,7 +120,11 @@ module Mordor
|
|
104
120
|
get(id)
|
105
121
|
end
|
106
122
|
|
107
|
-
def
|
123
|
+
def find(query, options = {})
|
124
|
+
Collection.new(self, collection.find(query, options).to_a)
|
125
|
+
end
|
126
|
+
|
127
|
+
def find_by_day(day, options = {})
|
108
128
|
case day
|
109
129
|
when DateTime
|
110
130
|
start = day.to_date.to_time
|
@@ -117,8 +137,11 @@ module Mordor
|
|
117
137
|
end_of_day = (day.to_date + 1).to_datetime.to_date.to_time
|
118
138
|
end
|
119
139
|
hash = {:at => {'$gte' => start, '$lt' => end_of_day}}
|
120
|
-
|
121
|
-
|
140
|
+
if options.keys.include?(:limit)
|
141
|
+
cursor = collection.find({:at => {'$gte' => start, '$lt' => end_of_day}}, options).to_a
|
142
|
+
else
|
143
|
+
cursor = collection.find({:at => {'$gte' => start, '$lt' => end_of_day}})
|
144
|
+
end
|
122
145
|
Collection.new(self, cursor)
|
123
146
|
end
|
124
147
|
|
@@ -132,8 +155,13 @@ module Mordor
|
|
132
155
|
class_eval <<-EOS, __FILE__, __LINE__
|
133
156
|
attr_accessor name
|
134
157
|
|
135
|
-
def self.#{method_name}(value)
|
136
|
-
|
158
|
+
def self.#{method_name}(value, options = {})
|
159
|
+
if options.keys.include?(:limit)
|
160
|
+
col = collection.find({:#{name} => value}, options).to_a
|
161
|
+
else
|
162
|
+
col = collection.find(:#{name} => value)
|
163
|
+
end
|
164
|
+
Collection.new(self, col)
|
137
165
|
end
|
138
166
|
EOS
|
139
167
|
end
|
data/mordor.gemspec
CHANGED
@@ -2,8 +2,8 @@ Gem::Specification.new do |s|
|
|
2
2
|
s.name = "mordor"
|
3
3
|
|
4
4
|
# Do not set the version and date field manually, this is done by the release script
|
5
|
-
s.version = "0.1.
|
6
|
-
s.date = "2011-11-
|
5
|
+
s.version = "0.1.3"
|
6
|
+
s.date = "2011-11-15"
|
7
7
|
|
8
8
|
s.summary = "mordor"
|
9
9
|
s.description = <<-eos
|
@@ -11,7 +11,6 @@ Gem::Specification.new do |s|
|
|
11
11
|
eos
|
12
12
|
|
13
13
|
s.add_development_dependency('rake')
|
14
|
-
s.add_development_dependency('ruby-debug')
|
15
14
|
s.add_development_dependency('rspec', '~> 2.0')
|
16
15
|
s.add_development_dependency('json')
|
17
16
|
|
@@ -35,4 +35,54 @@ describe "with respect to collections" do
|
|
35
35
|
json_collection[collection_name].size.should == 5
|
36
36
|
end
|
37
37
|
end
|
38
|
+
|
39
|
+
describe "merging array based collection" do
|
40
|
+
before :each do
|
41
|
+
@first_collection = Mordor::Collection.new(TestResource, [TestResource.new(:first => "first", :second => "second", :third => "third")])
|
42
|
+
@second_collection = Mordor::Collection.new(TestResource, [TestResource.new(:first => "1st", :second => "2nd", :third => "3rd")])
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not change original collections when no bang is used" do
|
46
|
+
first_size = @first_collection.size
|
47
|
+
second_size = @second_collection.size
|
48
|
+
|
49
|
+
new_collection = @first_collection.merge(@second_collection)
|
50
|
+
@first_collection.size.should == first_size
|
51
|
+
@second_collection.size.should == second_size
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should create collection with all elements from original collections" do
|
55
|
+
new_collection = @first_collection.merge(@second_collection)
|
56
|
+
new_collection.size.should == (@first_collection.size + @second_collection.size)
|
57
|
+
|
58
|
+
[@first_collection, @second_collection].each do |collection|
|
59
|
+
collection.each do |element|
|
60
|
+
new_collection.should include element
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should be possible to use the + as an alias" do
|
66
|
+
new_collection = @first_collection + @second_collection
|
67
|
+
new_collection.size.should == (@first_collection.size + @second_collection.size)
|
68
|
+
|
69
|
+
[@first_collection, @second_collection].each do |collection|
|
70
|
+
collection.each do |element|
|
71
|
+
new_collection.should include element
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should change the receiver of the merge! to have all elements" do
|
77
|
+
first_size = @first_collection.size
|
78
|
+
second_size = @second_collection.size
|
79
|
+
|
80
|
+
@first_collection.merge!(@second_collection)
|
81
|
+
@first_collection.size.should == (first_size + second_size)
|
82
|
+
|
83
|
+
@second_collection.each do |element|
|
84
|
+
@first_collection.should include element
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
38
88
|
end
|
@@ -63,7 +63,27 @@ describe "with respect to resources" do
|
|
63
63
|
hash.size.should == 3
|
64
64
|
hash[:first].should == "first"
|
65
65
|
hash[:second].should == "second"
|
66
|
-
hash[:third].should == "third"
|
66
|
+
hash[:third].should == "third"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "with respect to creating" do
|
71
|
+
before :each do
|
72
|
+
clean_sheet
|
73
|
+
@resource = TestResource.create({:first => "first", :second => "second", :third => "third"})
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should be possible to create a resource" do
|
77
|
+
@resource.should be_saved
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should be possible to retrieve created resources" do
|
81
|
+
res = TestResource.get(@resource._id)
|
82
|
+
res.should_not be_nil
|
83
|
+
res.first.should eql @resource.first
|
84
|
+
res.second.should eql @resource.second
|
85
|
+
res.third.should eql @resource.third
|
86
|
+
res._id.should eql @resource._id
|
67
87
|
end
|
68
88
|
end
|
69
89
|
|
@@ -114,13 +134,41 @@ describe "with respect to resources" do
|
|
114
134
|
res.second.should == resource.second
|
115
135
|
end
|
116
136
|
|
137
|
+
it "should be possible to find resources using queries" do
|
138
|
+
resource = TestResource.new({:first => "first", :second => "second"})
|
139
|
+
resource.save.should be_true
|
140
|
+
|
141
|
+
resource2 = TestResource.new({:first => "first", :second => "2nd"})
|
142
|
+
resource2.save.should be_true
|
143
|
+
|
144
|
+
collection = TestResource.find({:first => "first"})
|
145
|
+
collection.should_not be_nil
|
146
|
+
collection.size.should == 2
|
147
|
+
|
148
|
+
collection = TestResource.find({:second => "2nd"})
|
149
|
+
collection.should_not be_nil
|
150
|
+
collection.size.should == 1
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should be possible to query with a limit" do
|
154
|
+
resource = TestResource.new({:first => "first", :second => "second"})
|
155
|
+
resource.save.should be_true
|
156
|
+
|
157
|
+
resource2 = TestResource.new({:first => "first", :second => "2nd"})
|
158
|
+
resource2.save.should be_true
|
159
|
+
|
160
|
+
collection = TestResource.find({:first => "first"}, :limit => 1)
|
161
|
+
collection.should_not be_nil
|
162
|
+
collection.size.should == 1
|
163
|
+
end
|
164
|
+
|
117
165
|
it "should be possible to retrieve all resources" do
|
118
166
|
TestResource.all.should_not be_nil
|
119
167
|
TestResource.all.size.should == 0
|
120
168
|
|
121
169
|
resource = TestResource.new({:first => "first", :second => "second"})
|
122
170
|
resource.save.should be_true
|
123
|
-
|
171
|
+
|
124
172
|
resource2 = TestResource.new({:first => "first", :second => "second"})
|
125
173
|
resource2.save.should be_true
|
126
174
|
|
@@ -135,7 +183,7 @@ describe "with respect to resources" do
|
|
135
183
|
|
136
184
|
resource = TestResource.new({:first => "first", :second => "second"})
|
137
185
|
resource.save.should be_true
|
138
|
-
|
186
|
+
|
139
187
|
resource2 = TestResource.new({:first => "first", :second => "second"})
|
140
188
|
resource2.save.should be_true
|
141
189
|
|
@@ -145,6 +193,34 @@ describe "with respect to resources" do
|
|
145
193
|
end
|
146
194
|
end
|
147
195
|
|
196
|
+
context "with respect to retrieving by day" do
|
197
|
+
class TestTimedResource
|
198
|
+
include Mordor::Resource
|
199
|
+
|
200
|
+
attribute :first
|
201
|
+
attribute :at
|
202
|
+
end
|
203
|
+
|
204
|
+
before :each do
|
205
|
+
clean_sheet
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should be possible to retrieve a Resource by day" do
|
209
|
+
TestTimedResource.create({:first => "hallo", :at => DateTime.civil(2011, 11, 11, 11, 11)})
|
210
|
+
|
211
|
+
col = TestTimedResource.find_by_day(DateTime.civil(2011,11,11))
|
212
|
+
col.size.should == 1
|
213
|
+
col.first.first.should eql "hallo"
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should not retrieve resources from other days" do
|
217
|
+
TestTimedResource.create({:first => "hallo", :at => DateTime.civil(2011, 11, 11, 11, 11)})
|
218
|
+
|
219
|
+
col = TestTimedResource.find_by_day(DateTime.civil(2011,11,10))
|
220
|
+
col.size.should == 0
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
148
224
|
context "with respect to collections" do
|
149
225
|
it "should correctly return a collection name" do
|
150
226
|
TestResource.collection_name.should == "testresources"
|
@@ -167,4 +243,6 @@ describe "with respect to resources" do
|
|
167
243
|
resource.should be_nil
|
168
244
|
end
|
169
245
|
end
|
246
|
+
|
247
|
+
|
170
248
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,7 +4,7 @@ require 'mongo'
|
|
4
4
|
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
5
5
|
require 'mordor'
|
6
6
|
|
7
|
-
module
|
7
|
+
module Mordor
|
8
8
|
CONFIG = {
|
9
9
|
:hostname => 'localhost',
|
10
10
|
:port => 27017,
|
@@ -15,8 +15,11 @@ end
|
|
15
15
|
def clean_sheet
|
16
16
|
@connection ||= Mongo::Connection.new(Mordor::CONFIG[:hostname], Mordor::CONFIG[:port])
|
17
17
|
@db ||= @connection[Mordor::CONFIG[:database]]
|
18
|
-
|
19
|
-
|
18
|
+
|
19
|
+
['TestResource', 'TestTimedResource'].each do |resource|
|
20
|
+
if Object.const_defined?(resource)
|
21
|
+
@db[Object.const_get(resource).collection_name].drop
|
22
|
+
end
|
20
23
|
end
|
21
24
|
end
|
22
25
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mordor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jan-Willem Koelewijn
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-11-
|
19
|
+
date: 2011-11-15 00:00:00 +01:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -34,36 +34,36 @@ dependencies:
|
|
34
34
|
type: :development
|
35
35
|
version_requirements: *id001
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
37
|
+
name: rspec
|
38
38
|
prerelease: false
|
39
39
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
|
-
- -
|
42
|
+
- - ~>
|
43
43
|
- !ruby/object:Gem::Version
|
44
44
|
hash: 3
|
45
45
|
segments:
|
46
|
+
- 2
|
46
47
|
- 0
|
47
|
-
version: "0"
|
48
|
+
version: "2.0"
|
48
49
|
type: :development
|
49
50
|
version_requirements: *id002
|
50
51
|
- !ruby/object:Gem::Dependency
|
51
|
-
name:
|
52
|
+
name: json
|
52
53
|
prerelease: false
|
53
54
|
requirement: &id003 !ruby/object:Gem::Requirement
|
54
55
|
none: false
|
55
56
|
requirements:
|
56
|
-
- -
|
57
|
+
- - ">="
|
57
58
|
- !ruby/object:Gem::Version
|
58
59
|
hash: 3
|
59
60
|
segments:
|
60
|
-
- 2
|
61
61
|
- 0
|
62
|
-
version: "
|
62
|
+
version: "0"
|
63
63
|
type: :development
|
64
64
|
version_requirements: *id003
|
65
65
|
- !ruby/object:Gem::Dependency
|
66
|
-
name:
|
66
|
+
name: extlib
|
67
67
|
prerelease: false
|
68
68
|
requirement: &id004 !ruby/object:Gem::Requirement
|
69
69
|
none: false
|
@@ -77,7 +77,7 @@ dependencies:
|
|
77
77
|
type: :development
|
78
78
|
version_requirements: *id004
|
79
79
|
- !ruby/object:Gem::Dependency
|
80
|
-
name:
|
80
|
+
name: mongo
|
81
81
|
prerelease: false
|
82
82
|
requirement: &id005 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
@@ -91,7 +91,7 @@ dependencies:
|
|
91
91
|
type: :development
|
92
92
|
version_requirements: *id005
|
93
93
|
- !ruby/object:Gem::Dependency
|
94
|
-
name:
|
94
|
+
name: bson_ext
|
95
95
|
prerelease: false
|
96
96
|
requirement: &id006 !ruby/object:Gem::Requirement
|
97
97
|
none: false
|
@@ -105,7 +105,7 @@ dependencies:
|
|
105
105
|
type: :development
|
106
106
|
version_requirements: *id006
|
107
107
|
- !ruby/object:Gem::Dependency
|
108
|
-
name:
|
108
|
+
name: extlib
|
109
109
|
prerelease: false
|
110
110
|
requirement: &id007 !ruby/object:Gem::Requirement
|
111
111
|
none: false
|
@@ -116,10 +116,10 @@ dependencies:
|
|
116
116
|
segments:
|
117
117
|
- 0
|
118
118
|
version: "0"
|
119
|
-
type: :
|
119
|
+
type: :runtime
|
120
120
|
version_requirements: *id007
|
121
121
|
- !ruby/object:Gem::Dependency
|
122
|
-
name:
|
122
|
+
name: mongo
|
123
123
|
prerelease: false
|
124
124
|
requirement: &id008 !ruby/object:Gem::Requirement
|
125
125
|
none: false
|
@@ -133,7 +133,7 @@ dependencies:
|
|
133
133
|
type: :runtime
|
134
134
|
version_requirements: *id008
|
135
135
|
- !ruby/object:Gem::Dependency
|
136
|
-
name:
|
136
|
+
name: bson_ext
|
137
137
|
prerelease: false
|
138
138
|
requirement: &id009 !ruby/object:Gem::Requirement
|
139
139
|
none: false
|
@@ -147,7 +147,7 @@ dependencies:
|
|
147
147
|
type: :runtime
|
148
148
|
version_requirements: *id009
|
149
149
|
- !ruby/object:Gem::Dependency
|
150
|
-
name:
|
150
|
+
name: json
|
151
151
|
prerelease: false
|
152
152
|
requirement: &id010 !ruby/object:Gem::Requirement
|
153
153
|
none: false
|
@@ -160,20 +160,6 @@ dependencies:
|
|
160
160
|
version: "0"
|
161
161
|
type: :runtime
|
162
162
|
version_requirements: *id010
|
163
|
-
- !ruby/object:Gem::Dependency
|
164
|
-
name: json
|
165
|
-
prerelease: false
|
166
|
-
requirement: &id011 !ruby/object:Gem::Requirement
|
167
|
-
none: false
|
168
|
-
requirements:
|
169
|
-
- - ">="
|
170
|
-
- !ruby/object:Gem::Version
|
171
|
-
hash: 3
|
172
|
-
segments:
|
173
|
-
- 0
|
174
|
-
version: "0"
|
175
|
-
type: :runtime
|
176
|
-
version_requirements: *id011
|
177
163
|
description: " Small gem to add MongoDB Resources, resources have attributes that translate into document fields. When an attribute is declared, finders for the attribute are added to the Resource automatically\n"
|
178
164
|
email:
|
179
165
|
- janwillem.koelewijn@nedap.com
|