collector 0.0.7 → 0.0.8

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.
@@ -4,7 +4,7 @@ module Collector
4
4
  module Model
5
5
 
6
6
  def self.included(base)
7
- attr_reader :created_at, :updated_at
7
+ attr_reader :id, :created_at, :updated_at
8
8
  end
9
9
 
10
10
  def initialize(attributes = {})
@@ -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.reject { |key, val| val.nil? }
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
@@ -1,3 +1,3 @@
1
1
  module Collector
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -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 "#collection" do
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 "#save" do
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 "#save_without_updating_timestamps" do
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 "#serialize" do
53
- it "returns a models attributes without nil values" do
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: "bar" })
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: