collector 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -76,6 +76,49 @@ PickleRepository.save(pickle)
76
76
 
77
77
  Collector will only work with 1.9.x and above. Specifically it's tested with 1.9.2 and 1.9.3.
78
78
 
79
+ ## FAQ
80
+
81
+ ### What databases does it support?
82
+
83
+ Currently only MongoDB. I'd like to add an in-memory store, and if possible, a file store and Redis store.
84
+
85
+ ### Why is this better than an ORM?
86
+
87
+ If you don't already know why you need or want the Repository Pattern, then don't use it yet. It took me far longer than it should have to realize the benefits, despite having them explained to me many times. I just never really understood them until I'd actually experienced the pain this pattern solves myself. Once you do, come back and try it out.
88
+
89
+ ### This looks awfully similar to [curator](http://github.com/braintreee/curator). What's the difference?
90
+
91
+ I rolled my own application-specific version of the Repository Pattern for each project I worked on before I realized I was using it often enough to merit extracting it into a gem. Right about that time Braintree announced curator, and since both their implentation and mine were very similar—except theirs was further along and had more features—I decided to use theirs instead. But after using it for a few months and then reading through the code to try and contribute back to it, I decided to go back to my own implementation and extract it into a gem after all.
92
+
93
+ The biggest diference between curator and collector is that although curator is open source, I feel like a lot of the decisions made were and are in the interest of Braintree and the people that work there, rather than what is best for the project and the people who use it. The specific, functional differens are:
94
+
95
+ <table>
96
+ <thead>
97
+ <tr>
98
+ <td>Project</td>
99
+ <td>Stores</td>
100
+ <td>Test suite</td>
101
+ <td>Ruby version(s)</td>
102
+ </tr>
103
+ </thead>
104
+
105
+ <tbody>
106
+ <tr>
107
+ <td>collector</td>
108
+ <td>MongoDB</td>
109
+ <td>MiniTest</td>
110
+ <td>1.9.x</td>
111
+ </tr>
112
+
113
+ <tr>
114
+ <td>curator</td>
115
+ <td>MongoDB, Riak, In-memory</td>
116
+ <td>RSpec</td>
117
+ <td>1.9.x, 1.8.7</td>
118
+ </tr>
119
+ </tbody>
120
+ </table>
121
+
79
122
  ## Contributing
80
123
 
81
124
  1. Fork it
@@ -43,12 +43,6 @@ module Collector
43
43
  model.attributes.with_indifferent_access
44
44
  end
45
45
 
46
- # def all
47
- # collection.find_all.map do |document|
48
- # deserialize!(document)
49
- # end
50
- # end
51
-
52
46
  def deserialize!(attributes)
53
47
  attributes = attributes.with_indifferent_access
54
48
  attributes["id"] = attributes.delete("_id")
@@ -59,6 +53,30 @@ module Collector
59
53
  model.new(attributes)
60
54
  end
61
55
 
56
+ def all
57
+ collection.find_all.map do |document|
58
+ deserialize!(document)
59
+ end
60
+ end
61
+
62
+ def method_missing(method_sym, *arguments, &block)
63
+ if method_sym.to_s =~ /^find_by_(.*)$/
64
+ collection.find($1.to_sym => arguments.first).map do |document|
65
+ deserialize!(document)
66
+ end
67
+ else
68
+ super
69
+ end
70
+ end
71
+
72
+ def respond_to?(method_sym, include_private = false)
73
+ if method_sym.to_s =~ /^find_by_(.*)$/
74
+ true
75
+ else
76
+ super
77
+ end
78
+ end
79
+
62
80
  end
63
81
 
64
82
  end
@@ -1,3 +1,3 @@
1
1
  module Collector
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -83,4 +83,34 @@ 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
88
+ document_1 = stub
89
+ document_2 = stub
90
+ documents = [document_1, document_2]
91
+ TestRepository.expects(:deserialize!).with(document_1)
92
+ TestRepository.expects(:deserialize!).with(document_2)
93
+ collection = mock { expects(:find_all).returns(documents) }
94
+ TestRepository.expects(:collection).returns(collection)
95
+ TestRepository.all
96
+ end
97
+ end
98
+
99
+ describe "finders" do
100
+ it "dynamically matches find_by finders" do
101
+ document_1 = stub
102
+ document_2 = stub
103
+ documents = [document_1, document_2]
104
+ TestRepository.expects(:deserialize!).with(document_1)
105
+ TestRepository.expects(:deserialize!).with(document_2)
106
+ collection = mock { expects(:find).with(email: "foobar@fibroblast.com").returns(documents) }
107
+ TestRepository.expects(:collection).returns(collection)
108
+ TestRepository.find_by_email("foobar@fibroblast.com")
109
+ end
110
+
111
+ it "responds to dynamically matched finders" do
112
+ TestRepository.respond_to?(:find_by_email).must_equal true
113
+ end
114
+ end
115
+
86
116
  end
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.8
4
+ version: 0.0.9
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-11-22 00:00:00.000000000 Z
12
+ date: 2012-11-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport