collector 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/collector/model.rb +1 -1
- data/lib/collector/repository.rb +26 -2
- data/lib/collector/version.rb +1 -1
- data/test/collector/model_spec.rb +6 -0
- data/test/collector/repository_spec.rb +34 -7
- metadata +1 -1
data/lib/collector/model.rb
CHANGED
data/lib/collector/repository.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "active_support/inflector"
|
2
|
+
require "active_support/core_ext/hash"
|
2
3
|
|
3
4
|
module Collector
|
4
5
|
module Repository
|
@@ -27,12 +28,35 @@ module Collector
|
|
27
28
|
end
|
28
29
|
|
29
30
|
def save_without_updating_timestamps(model)
|
30
|
-
attributes = serialize(model)
|
31
|
+
attributes = serialize!(model)
|
31
32
|
collection.insert(attributes)
|
32
33
|
end
|
33
34
|
|
35
|
+
def serialize!(model)
|
36
|
+
attributes = serialize(model)
|
37
|
+
attributes["_id"] = attributes.delete("id")
|
38
|
+
attributes.reject! { |key, val| val.nil? }
|
39
|
+
attributes
|
40
|
+
end
|
41
|
+
|
34
42
|
def serialize(model)
|
35
|
-
model.attributes.
|
43
|
+
model.attributes.with_indifferent_access
|
44
|
+
end
|
45
|
+
|
46
|
+
# def all
|
47
|
+
# collection.find_all.map do |document|
|
48
|
+
# deserialize!(document)
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
|
52
|
+
def deserialize!(attributes)
|
53
|
+
attributes = attributes.with_indifferent_access
|
54
|
+
attributes["id"] = attributes.delete("_id")
|
55
|
+
deserialize(attributes)
|
56
|
+
end
|
57
|
+
|
58
|
+
def deserialize(attributes)
|
59
|
+
model.new(attributes)
|
36
60
|
end
|
37
61
|
|
38
62
|
end
|
data/lib/collector/version.rb
CHANGED
@@ -20,6 +20,12 @@ describe Collector::Model do
|
|
20
20
|
TestModel.new(foo: "bar").foo.must_equal "bar"
|
21
21
|
end
|
22
22
|
|
23
|
+
it "has an id" do
|
24
|
+
test_model = TestModel.new
|
25
|
+
test_model.instance_variable_set("@id", "foobar")
|
26
|
+
test_model.id.must_equal "foobar"
|
27
|
+
end
|
28
|
+
|
23
29
|
it "has a created_at and updated_at timestamp" do
|
24
30
|
now = Time.now
|
25
31
|
test_model = TestModel.new
|
@@ -17,7 +17,7 @@ describe Collector::Repository do
|
|
17
17
|
TestRepository.collection_name.must_equal "tests"
|
18
18
|
end
|
19
19
|
|
20
|
-
describe "
|
20
|
+
describe "collection" do
|
21
21
|
describe "when a connection is set" do
|
22
22
|
it "returns the mongo collection" do
|
23
23
|
collection = mock()
|
@@ -29,7 +29,7 @@ describe Collector::Repository do
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
describe "
|
32
|
+
describe "save" do
|
33
33
|
it "touches the model and then saves it" do
|
34
34
|
model = mock(:touch)
|
35
35
|
TestRepository.expects(:save_without_updating_timestamps).with(model)
|
@@ -37,10 +37,10 @@ describe Collector::Repository do
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
describe "
|
40
|
+
describe "save_without_updating_timestamps" do
|
41
41
|
it "serializes the model and then inserts it into the collection" do
|
42
42
|
model = stub()
|
43
|
-
TestRepository.expects(:serialize).with(model).returns({ foo: "bar" })
|
43
|
+
TestRepository.expects(:serialize!).with(model).returns({ foo: "bar" })
|
44
44
|
|
45
45
|
collection = mock(insert: { foo: "bar" })
|
46
46
|
TestRepository.stubs(:collection).returns(collection)
|
@@ -49,10 +49,37 @@ describe Collector::Repository do
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
describe "
|
53
|
-
it "
|
52
|
+
describe "serialize!" do
|
53
|
+
it "normalize id to _id" do
|
54
|
+
model = mock(attributes: { id: 123, foo: "bar" })
|
55
|
+
TestRepository.serialize!(model).must_equal({ "_id" => 123, "foo" => "bar" })
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns a model's attributes without nil values" do
|
54
59
|
model = mock(attributes: { foo: "bar", nothing: nil })
|
55
|
-
TestRepository.serialize(model).must_equal({ foo
|
60
|
+
TestRepository.serialize!(model).must_equal({ "foo" => "bar" })
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "serialize" do
|
65
|
+
it "returns a model's attributes" do
|
66
|
+
model = mock(attributes: { foo: "bar" })
|
67
|
+
TestRepository.serialize(model).must_equal({ "foo" => "bar" })
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "deserialize!" do
|
72
|
+
it "normalizes _id to id" do
|
73
|
+
TestRepository.expects(:deserialize).with("id" => 123, "name" => "Brandon")
|
74
|
+
TestRepository.deserialize!(_id: 123, name: "Brandon")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "deserialize" do
|
79
|
+
it "instantiates a new model from a hash of attributes" do
|
80
|
+
attributes = { first_name: "Brandon", last_name: "Weiss" }
|
81
|
+
TestRepository.model.expects(:new).with(attributes)
|
82
|
+
TestRepository.deserialize(attributes)
|
56
83
|
end
|
57
84
|
end
|
58
85
|
|