collector 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
data/collector.gemspec CHANGED
@@ -18,4 +18,6 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_dependency "activesupport", "~> 3.2.8"
21
+ gem.add_dependency "mongo", "~> 1.8.0"
22
+ gem.add_dependency "bson", "~> 1.8.0"
21
23
  end
@@ -1,5 +1,6 @@
1
1
  require "active_support/inflector"
2
2
  require "active_support/core_ext/hash"
3
+ require "bson"
3
4
 
4
5
  module Collector
5
6
  module Repository
@@ -54,6 +55,7 @@ module Collector
54
55
  end
55
56
 
56
57
  def find_by(attributes = {})
58
+ attributes[:_id] = normalize_id(attributes[:_id]) if attributes[:_id]
57
59
  collection.find(attributes).map do |document|
58
60
  deserialize!(document)
59
61
  end
@@ -95,6 +97,12 @@ module Collector
95
97
  end
96
98
  end
97
99
 
100
+ private
101
+
102
+ def normalize_id(id)
103
+ BSON::ObjectId.legal?(id) ? BSON::ObjectId.from_string(id) : id
104
+ end
105
+
98
106
  end
99
107
 
100
108
  end
@@ -1,3 +1,3 @@
1
1
  module Collector
2
- VERSION = "0.0.15"
2
+ VERSION = "0.0.16"
3
3
  end
@@ -105,6 +105,18 @@ describe Collector::Repository do
105
105
  TestRepository.expects(:collection).returns(collection)
106
106
  TestRepository.find_by
107
107
  end
108
+
109
+ it "normalizes the id into a BSON::ObjectId if it's valid ObjectId" do
110
+ collection = mock { expects(:find).with(_id: BSON::ObjectId("50c58f4ab392d4381a000001")).returns([]) }
111
+ TestRepository.expects(:collection).returns(collection)
112
+ TestRepository.find_by(_id: "50c58f4ab392d4381a000001")
113
+ end
114
+
115
+ it "does nothing with the id if it's not a valid BSON::ObjectId" do
116
+ collection = mock { expects(:find).with(_id: "lols").returns([]) }
117
+ TestRepository.expects(:collection).returns(collection)
118
+ TestRepository.find_by(_id: "lols")
119
+ end
108
120
  end
109
121
 
110
122
  describe "find_first_by" do
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.15
4
+ version: 0.0.16
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,6 +27,38 @@ dependencies:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: 3.2.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: mongo
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.8.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.8.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: bson
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.0
30
62
  description: An implementation of the Repository Pattern for MongoDB.
31
63
  email:
32
64
  - brandon@anti-pattern.com