collector 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -72,13 +72,16 @@ pickle = Pickle.new(brine: "vinegar", started_at: Time.now)
72
72
  PickleRepository.save(pickle)
73
73
  ```
74
74
 
75
- Repositories can get all models, find by ID, or find dynamically by any attribute.
75
+ Repositories can find all models, find by id, find dynamically by any attribute, and then find first by id or any other attribute.
76
76
 
77
77
  ```ruby
78
78
  PickleRepository.all
79
79
  PickleRepository.find_by_id(BSON::ObjectId("50af1f3fb392d4aa0d000001"))
80
80
  PickleRepository.find_by_color("green")
81
81
  PickleRepository.find_by_taste("delicious")
82
+ PickleRepository.find_first_by_id(BSON::ObjectId("50af1f3fb392d4aa0d000001"))
83
+ PickleRepository.find_first_by_color("green")
84
+ PickleRepository.find_first_by_taste("delicious")
82
85
  ```
83
86
 
84
87
  ### Requirements
@@ -59,6 +59,10 @@ module Collector
59
59
  end
60
60
  end
61
61
 
62
+ def find_first_by(attributes)
63
+ find_by(attributes).first
64
+ end
65
+
62
66
  def all
63
67
  find_by
64
68
  end
@@ -68,12 +72,14 @@ module Collector
68
72
  end
69
73
 
70
74
  def find_first_by_id(id)
71
- find_by_id(id).first
75
+ find_first_by(_id: id)
72
76
  end
73
77
 
74
78
  def method_missing(method_sym, *arguments, &block)
75
79
  if method_sym.to_s =~ /^find_by_(.*)$/
76
80
  find_by($1.to_sym => arguments.first)
81
+ elsif method_sym.to_s =~ /^find_first_by_(.*)$/
82
+ find_first_by($1.to_sym => arguments.first)
77
83
  else
78
84
  super
79
85
  end
@@ -82,6 +88,8 @@ module Collector
82
88
  def respond_to?(method_sym, include_private = false)
83
89
  if method_sym.to_s =~ /^find_by_(.*)$/
84
90
  true
91
+ elsif method_sym.to_s =~ /^find_first_by_(.*)$/
92
+ true
85
93
  else
86
94
  super
87
95
  end
@@ -1,3 +1,3 @@
1
1
  module Collector
2
- VERSION = "0.0.14"
2
+ VERSION = "0.0.15"
3
3
  end
@@ -107,6 +107,13 @@ describe Collector::Repository do
107
107
  end
108
108
  end
109
109
 
110
+ describe "find_first_by" do
111
+ it "finds the first document by a hash of attributes" do
112
+ TestRepository.expects(:find_by).with(attribute: "value").returns(mock(:first))
113
+ TestRepository.find_first_by(attribute: "value")
114
+ end
115
+ end
116
+
110
117
  describe "all" do
111
118
  it "finds by attributes without any attributes" do
112
119
  TestRepository.expects(:find_by).with()
@@ -123,8 +130,7 @@ describe Collector::Repository do
123
130
 
124
131
  describe "find_first_by_id" do
125
132
  it "finds first by id" do
126
- models = mock { expects(:first) }
127
- TestRepository.expects(:find_by).with(_id: "bson-id").returns(models)
133
+ TestRepository.expects(:find_first_by).with(_id: "bson-id")
128
134
  TestRepository.find_first_by_id("bson-id")
129
135
  end
130
136
  end
@@ -135,9 +141,18 @@ describe Collector::Repository do
135
141
  TestRepository.find_by_email("foobar@fibroblast.com")
136
142
  end
137
143
 
138
- it "responds to dynamically matched finders" do
144
+ it "dynamically matches find_first_by_ finders" do
145
+ TestRepository.expects(:find_first_by).with(email: "foobar@fibroblast.com")
146
+ TestRepository.find_first_by_email("foobar@fibroblast.com")
147
+ end
148
+
149
+ it "responds to dynamically matched find_by_ finders" do
139
150
  TestRepository.respond_to?(:find_by_email).must_equal true
140
151
  end
152
+
153
+ it "responds to dynamically matched find_first_by_ finders" do
154
+ TestRepository.respond_to?(:find_first_by_email).must_equal true
155
+ end
141
156
  end
142
157
 
143
158
  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.14
4
+ version: 0.0.15
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-07 00:00:00.000000000 Z
12
+ date: 2012-12-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport