collector 0.0.13 → 0.0.14

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.
@@ -53,23 +53,27 @@ module Collector
53
53
  model.new(attributes)
54
54
  end
55
55
 
56
- def all
57
- collection.find.map do |document|
56
+ def find_by(attributes = {})
57
+ collection.find(attributes).map do |document|
58
58
  deserialize!(document)
59
59
  end
60
60
  end
61
61
 
62
+ def all
63
+ find_by
64
+ end
65
+
62
66
  def find_by_id(id)
63
- collection.find(_id: id).map do |document|
64
- deserialize!(document)
65
- end
67
+ find_by(_id: id)
68
+ end
69
+
70
+ def find_first_by_id(id)
71
+ find_by_id(id).first
66
72
  end
67
73
 
68
74
  def method_missing(method_sym, *arguments, &block)
69
75
  if method_sym.to_s =~ /^find_by_(.*)$/
70
- collection.find($1.to_sym => arguments.first).map do |document|
71
- deserialize!(document)
72
- end
76
+ find_by($1.to_sym => arguments.first)
73
77
  else
74
78
  super
75
79
  end
@@ -1,3 +1,3 @@
1
1
  module Collector
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.14"
3
3
  end
@@ -83,42 +83,55 @@ describe Collector::Repository do
83
83
  end
84
84
  end
85
85
 
86
- describe "all" do
87
- it "returns all documents in a collection" do
86
+ describe "find_by" do
87
+ it "finds documents by a hash of attributes" do
88
88
  document_1 = stub
89
89
  document_2 = stub
90
90
  documents = [document_1, document_2]
91
91
  TestRepository.expects(:deserialize!).with(document_1)
92
92
  TestRepository.expects(:deserialize!).with(document_2)
93
- collection = mock { expects(:find).returns(documents) }
93
+ collection = mock { expects(:find).with(attribute: "value").returns(documents) }
94
94
  TestRepository.expects(:collection).returns(collection)
95
- TestRepository.all
95
+ TestRepository.find_by(attribute: "value")
96
96
  end
97
- end
98
97
 
99
- describe "find_by_id" do
100
- it "returns documents by their ID" do
101
- id = "bson-id"
98
+ it "finds all documents if no attributes are given" do
102
99
  document_1 = stub
103
100
  document_2 = stub
104
101
  documents = [document_1, document_2]
105
102
  TestRepository.expects(:deserialize!).with(document_1)
106
103
  TestRepository.expects(:deserialize!).with(document_2)
107
- collection = mock { expects(:find).with(_id: id).returns(documents) }
104
+ collection = mock { expects(:find).with({}).returns(documents) }
108
105
  TestRepository.expects(:collection).returns(collection)
109
- TestRepository.find_by_id(id)
106
+ TestRepository.find_by
110
107
  end
111
108
  end
112
109
 
113
- describe "finders" do
114
- it "dynamically matches find_by finders" do
115
- document_1 = stub
116
- document_2 = stub
117
- documents = [document_1, document_2]
118
- TestRepository.expects(:deserialize!).with(document_1)
119
- TestRepository.expects(:deserialize!).with(document_2)
120
- collection = mock { expects(:find).with(email: "foobar@fibroblast.com").returns(documents) }
121
- TestRepository.expects(:collection).returns(collection)
110
+ describe "all" do
111
+ it "finds by attributes without any attributes" do
112
+ TestRepository.expects(:find_by).with()
113
+ TestRepository.all
114
+ end
115
+ end
116
+
117
+ describe "find_by_id" do
118
+ it "finds by id" do
119
+ TestRepository.expects(:find_by).with(_id: "bson-id")
120
+ TestRepository.find_by_id("bson-id")
121
+ end
122
+ end
123
+
124
+ describe "find_first_by_id" do
125
+ it "finds first by id" do
126
+ models = mock { expects(:first) }
127
+ TestRepository.expects(:find_by).with(_id: "bson-id").returns(models)
128
+ TestRepository.find_first_by_id("bson-id")
129
+ end
130
+ end
131
+
132
+ describe "dynamic finders" do
133
+ it "dynamically matches find_by_ finders" do
134
+ TestRepository.expects(:find_by).with(email: "foobar@fibroblast.com")
122
135
  TestRepository.find_by_email("foobar@fibroblast.com")
123
136
  end
124
137
 
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.13
4
+ version: 0.0.14
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-06 00:00:00.000000000 Z
12
+ date: 2012-12-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport