collector 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f67cd923c0bfdec2effecea4b750ba9b3f7ba8a
4
- data.tar.gz: 94cd9b47c2060bd13ffd3325775324c1d4d461dd
3
+ metadata.gz: abed7c5e4060dc56b6f1bfd6c1d663ccb99d3266
4
+ data.tar.gz: c15c846510d601f66b03ca4dee9bd5961cce54f3
5
5
  SHA512:
6
- metadata.gz: cb4e10a1276cf3e249f253ddbdcd625c83bac8ca57cc5b9d1f8b59d3c9a4fc3645566dd20927ee30dfb6a5519a6314e11f688868b93cd56d166df3d7ee26c945
7
- data.tar.gz: ff6096b388a902c66c9475458781678bc3ff457548ec4b6f0ee5e20e3bd56326c6556e1ded1cdb84214e11f5ac34bcaec00748473ca399a2d2a83c5e9b685c2d
6
+ metadata.gz: b1df54655d7eb6080b59a5c7f8916e31770f46849739097da2f7dae472f8c9d35de2dce56c3eb7fb1fe028edbac5c037e2b41cf125e476af207de92e1944b4d8
7
+ data.tar.gz: cbbbcc86b3b015f76112a2c2d375e1d97dd16c482196f526e61ac0423489ae40bf0c1dbf22fde3953ac70f6a5801472d6739b179a5cc85e9676124c8e1967bdd
data/Gemfile CHANGED
@@ -2,7 +2,6 @@ source "https://rubygems.org"
2
2
 
3
3
  group :test do
4
4
  gem "rake", "~> 0.9.2.2"
5
- gem "turn", "~> 0.9.6"
6
5
  gem "minitest", "~> 4.1.0"
7
6
  gem "mocha", "~> 0.12.7", require: false
8
7
  gem "timecop", "~> 0.5.3"
@@ -23,5 +23,10 @@ module Collector
23
23
  end
24
24
  end
25
25
 
26
+ def ==(other_model)
27
+ return nil if self.id.nil? || other_model.id.nil?
28
+ self.id == other_model.id
29
+ end
30
+
26
31
  end
27
32
  end
@@ -1,3 +1,3 @@
1
1
  module Collector
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -68,4 +68,27 @@ describe Collector::Model do
68
68
  end
69
69
  end
70
70
 
71
+ describe "#==" do
72
+ it "compares two models with the same id" do
73
+ model_1 = TestModel.new(id: "foobar")
74
+ model_2 = TestModel.new(id: "foobar")
75
+
76
+ model_1.must_equal model_2
77
+ end
78
+
79
+ it "compares two models with different ids" do
80
+ model_1 = TestModel.new(id: "foobar")
81
+ model_2 = TestModel.new(id: "barfoo")
82
+
83
+ model_1.wont_equal model_2
84
+ end
85
+
86
+ it "compares two models with no ids" do
87
+ model_1 = TestModel.new(id: nil)
88
+ model_2 = TestModel.new(id: nil)
89
+
90
+ model_1.wont_equal model_2
91
+ end
92
+ end
93
+
71
94
  end
@@ -9,27 +9,27 @@ describe Collector::Repository do
9
9
  end
10
10
 
11
11
  Object.send(:remove_const, :TestRepository) if Object.const_defined?(:TestRepository)
12
- class TestRepository
12
+ class TestModelRepository
13
13
  include Collector::Repository
14
14
  end
15
15
  end
16
16
 
17
17
  it "has a model derived from its class name" do
18
- TestRepository.model.name.must_equal "Test"
18
+ TestModelRepository.model.name.must_equal "TestModel"
19
19
  end
20
20
 
21
21
  it "has a collection_name derived from its model" do
22
- TestRepository.collection_name.must_equal "tests"
22
+ TestModelRepository.collection_name.must_equal "test_models"
23
23
  end
24
24
 
25
25
  describe "collection" do
26
26
  describe "when a connection is set" do
27
27
  it "returns the mongo collection" do
28
28
  collection = mock()
29
- connection = mock { stubs(:[]).with("tests").returns(collection) }
29
+ connection = mock { stubs(:[]).with("test_models").returns(collection) }
30
30
  Collector.stubs(:connection).returns(connection)
31
31
 
32
- TestRepository.collection.must_equal collection
32
+ TestModelRepository.collection.must_equal collection
33
33
  end
34
34
  end
35
35
  end
@@ -37,33 +37,32 @@ describe Collector::Repository do
37
37
  describe "save" do
38
38
  it "touches the model and then saves it" do
39
39
  model = mock(:touch)
40
- TestRepository.expects(:save_without_updating_timestamps).with(model)
41
- TestRepository.save(model)
40
+ TestModelRepository.expects(:save_without_updating_timestamps).with(model)
41
+ TestModelRepository.save(model)
42
42
  end
43
43
  end
44
44
 
45
45
  describe "save_without_updating_timestamps" do
46
46
  it "serializes the model and then saves it into the collection" do
47
47
  model = stub()
48
- TestRepository.expects(:serialize!).with(model).returns({ foo: "bar" })
48
+ TestModelRepository.expects(:serialize!).with(model).returns({ foo: "bar" })
49
49
 
50
50
  collection = mock(save: { foo: "bar" })
51
- TestRepository.stubs(:collection).returns(collection)
51
+ TestModelRepository.stubs(:collection).returns(collection)
52
52
 
53
- TestRepository.save_without_updating_timestamps(model)
53
+ TestModelRepository.save_without_updating_timestamps(model)
54
54
  end
55
55
 
56
- it "returns the model with the its id set" do
57
- model = TestModel.new
58
- TestRepository.expects(:serialize!).with(model).returns({ foo: "bar" })
56
+ it "returns the model with its id set" do
57
+ model = TestModel.new(id: "123abc")
58
+ TestModelRepository.expects(:serialize!).with(model).returns({ foo: "bar" })
59
59
 
60
60
  object_id = stub(to_s: "123abc")
61
61
  collection = mock { expects(:save).with({ foo: "bar" }).returns(object_id) }
62
- TestRepository.stubs(:collection).returns(collection)
62
+ TestModelRepository.stubs(:collection).returns(collection)
63
63
 
64
- updated_model = TestRepository.save_without_updating_timestamps(model)
64
+ updated_model = TestModelRepository.save_without_updating_timestamps(model)
65
65
  updated_model.must_equal model
66
- updated_model.id.must_equal "123abc"
67
66
  end
68
67
  end
69
68
 
@@ -72,43 +71,43 @@ describe Collector::Repository do
72
71
  model = stub(id: "50c58f4ab392d4381a000001")
73
72
 
74
73
  collection = mock { expects(:remove).with(_id: BSON::ObjectId("50c58f4ab392d4381a000001")) }
75
- TestRepository.stubs(:collection).returns(collection)
74
+ TestModelRepository.stubs(:collection).returns(collection)
76
75
 
77
- TestRepository.delete(model)
76
+ TestModelRepository.delete(model)
78
77
  end
79
78
  end
80
79
 
81
80
  describe "serialize!" do
82
81
  it "normalize id to _id and converts to a BSON::ObjectId" do
83
82
  model = mock(attributes: { id: "50c58f4ab392d4381a000001", foo: "bar" })
84
- TestRepository.serialize!(model).must_equal({ "_id" => BSON::ObjectId("50c58f4ab392d4381a000001"), "foo" => "bar" })
83
+ TestModelRepository.serialize!(model).must_equal({ "_id" => BSON::ObjectId("50c58f4ab392d4381a000001"), "foo" => "bar" })
85
84
  end
86
85
 
87
86
  it "returns a model's attributes without nil values" do
88
87
  model = mock(attributes: { foo: "bar", nothing: nil })
89
- TestRepository.serialize!(model).must_equal({ "foo" => "bar" })
88
+ TestModelRepository.serialize!(model).must_equal({ "foo" => "bar" })
90
89
  end
91
90
  end
92
91
 
93
92
  describe "serialize" do
94
93
  it "returns a model's attributes" do
95
94
  model = mock(attributes: { foo: "bar" })
96
- TestRepository.serialize(model).must_equal({ "foo" => "bar" })
95
+ TestModelRepository.serialize(model).must_equal({ "foo" => "bar" })
97
96
  end
98
97
  end
99
98
 
100
99
  describe "deserialize!" do
101
100
  it "normalizes _id to id and converts to a string id" do
102
- TestRepository.expects(:deserialize).with("id" => "50c58f4ab392d4381a000001", "name" => "Brandon")
103
- TestRepository.deserialize!(_id: BSON::ObjectId("50c58f4ab392d4381a000001"), name: "Brandon")
101
+ TestModelRepository.expects(:deserialize).with("id" => "50c58f4ab392d4381a000001", "name" => "Brandon")
102
+ TestModelRepository.deserialize!(_id: BSON::ObjectId("50c58f4ab392d4381a000001"), name: "Brandon")
104
103
  end
105
104
  end
106
105
 
107
106
  describe "deserialize" do
108
107
  it "instantiates a new model from a hash of attributes" do
109
108
  attributes = { first_name: "Brandon", last_name: "Weiss" }
110
- TestRepository.model.expects(:new).with(attributes)
111
- TestRepository.deserialize(attributes)
109
+ TestModelRepository.model.expects(:new).with(attributes)
110
+ TestModelRepository.deserialize(attributes)
112
111
  end
113
112
  end
114
113
 
@@ -117,82 +116,82 @@ describe Collector::Repository do
117
116
  document_1 = stub
118
117
  document_2 = stub
119
118
  documents = [document_1, document_2]
120
- TestRepository.expects(:deserialize!).with(document_1)
121
- TestRepository.expects(:deserialize!).with(document_2)
119
+ TestModelRepository.expects(:deserialize!).with(document_1)
120
+ TestModelRepository.expects(:deserialize!).with(document_2)
122
121
  collection = mock { expects(:find).with(attribute: "value").returns(documents) }
123
- TestRepository.expects(:collection).returns(collection)
124
- TestRepository.find_by(attribute: "value")
122
+ TestModelRepository.expects(:collection).returns(collection)
123
+ TestModelRepository.find_by(attribute: "value")
125
124
  end
126
125
 
127
126
  it "finds all documents if no attributes are given" do
128
127
  document_1 = stub
129
128
  document_2 = stub
130
129
  documents = [document_1, document_2]
131
- TestRepository.expects(:deserialize!).with(document_1)
132
- TestRepository.expects(:deserialize!).with(document_2)
130
+ TestModelRepository.expects(:deserialize!).with(document_1)
131
+ TestModelRepository.expects(:deserialize!).with(document_2)
133
132
  collection = mock { expects(:find).with({}).returns(documents) }
134
- TestRepository.expects(:collection).returns(collection)
135
- TestRepository.find_by
133
+ TestModelRepository.expects(:collection).returns(collection)
134
+ TestModelRepository.find_by
136
135
  end
137
136
 
138
137
  it "normalizes the id into a BSON::ObjectId if it's valid ObjectId" do
139
138
  collection = mock { expects(:find).with(_id: BSON::ObjectId("50c58f4ab392d4381a000001")).returns([]) }
140
- TestRepository.expects(:collection).returns(collection)
141
- TestRepository.find_by(_id: "50c58f4ab392d4381a000001")
139
+ TestModelRepository.expects(:collection).returns(collection)
140
+ TestModelRepository.find_by(_id: "50c58f4ab392d4381a000001")
142
141
  end
143
142
 
144
143
  it "does nothing with the id if it's not a valid BSON::ObjectId" do
145
144
  collection = mock { expects(:find).with(_id: "lols").returns([]) }
146
- TestRepository.expects(:collection).returns(collection)
147
- TestRepository.find_by(_id: "lols")
145
+ TestModelRepository.expects(:collection).returns(collection)
146
+ TestModelRepository.find_by(_id: "lols")
148
147
  end
149
148
  end
150
149
 
151
150
  describe "find_first_by" do
152
151
  it "finds the first document by a hash of attributes" do
153
- TestRepository.expects(:find_by).with(attribute: "value").returns(mock(:first))
154
- TestRepository.find_first_by(attribute: "value")
152
+ TestModelRepository.expects(:find_by).with(attribute: "value").returns(mock(:first))
153
+ TestModelRepository.find_first_by(attribute: "value")
155
154
  end
156
155
  end
157
156
 
158
157
  describe "all" do
159
158
  it "finds by attributes without any attributes" do
160
- TestRepository.expects(:find_by).with()
161
- TestRepository.all
159
+ TestModelRepository.expects(:find_by).with()
160
+ TestModelRepository.all
162
161
  end
163
162
  end
164
163
 
165
164
  describe "find_by_id" do
166
165
  it "finds by id" do
167
- TestRepository.expects(:find_by).with(_id: "bson-id")
168
- TestRepository.find_by_id("bson-id")
166
+ TestModelRepository.expects(:find_by).with(_id: "bson-id")
167
+ TestModelRepository.find_by_id("bson-id")
169
168
  end
170
169
  end
171
170
 
172
171
  describe "find_first_by_id" do
173
172
  it "finds first by id" do
174
- TestRepository.expects(:find_first_by).with(_id: "bson-id")
175
- TestRepository.find_first_by_id("bson-id")
173
+ TestModelRepository.expects(:find_first_by).with(_id: "bson-id")
174
+ TestModelRepository.find_first_by_id("bson-id")
176
175
  end
177
176
  end
178
177
 
179
178
  describe "dynamic finders" do
180
179
  it "dynamically matches find_by_ finders" do
181
- TestRepository.expects(:find_by).with(email: "foobar@fibroblast.com")
182
- TestRepository.find_by_email("foobar@fibroblast.com")
180
+ TestModelRepository.expects(:find_by).with(email: "foobar@fibroblast.com")
181
+ TestModelRepository.find_by_email("foobar@fibroblast.com")
183
182
  end
184
183
 
185
184
  it "dynamically matches find_first_by_ finders" do
186
- TestRepository.expects(:find_first_by).with(email: "foobar@fibroblast.com")
187
- TestRepository.find_first_by_email("foobar@fibroblast.com")
185
+ TestModelRepository.expects(:find_first_by).with(email: "foobar@fibroblast.com")
186
+ TestModelRepository.find_first_by_email("foobar@fibroblast.com")
188
187
  end
189
188
 
190
189
  it "responds to dynamically matched find_by_ finders" do
191
- TestRepository.respond_to?(:find_by_email).must_equal true
190
+ TestModelRepository.respond_to?(:find_by_email).must_equal true
192
191
  end
193
192
 
194
193
  it "responds to dynamically matched find_first_by_ finders" do
195
- TestRepository.respond_to?(:find_first_by_email).must_equal true
194
+ TestModelRepository.respond_to?(:find_first_by_email).must_equal true
196
195
  end
197
196
  end
198
197
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Weiss
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-12 00:00:00.000000000 Z
11
+ date: 2013-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  requirements: []
95
95
  rubyforge_project:
96
- rubygems_version: 2.0.0
96
+ rubygems_version: 2.0.3
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: An implementation of the Repository Pattern for MongoDB