perpetuity 0.4.5 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## Version 0.4.6
2
+
3
+ - Fix an inconsistency between `mongo` and `moped` gems. Previously, the MongoDB adapter relied on the fail-fast behavior of the `mongo` gem so we had no need to check for errors. Moped does not do that by default, which resulted in incorrect behavior of the gem when an object was not persisted.
4
+ - The `Mapper#select` DSL now allows the use of `!=` as criteria. Not all supported Ruby implementations allowed that operator to be overridden when the DSL was created, but they do now.
5
+
1
6
  ## Version 0.4.5
2
7
 
3
8
  - Move from the `mongo` gem from 10gen to the `moped` gem for talking to MongoDB. This resulted in performance gains of 30-80%, depending on query size.
@@ -0,0 +1,3 @@
1
+ module Perpetuity
2
+ DuplicateKeyError = Class.new(StandardError)
3
+ end
@@ -2,6 +2,7 @@ require 'moped'
2
2
  require 'perpetuity/mongodb/query'
3
3
  require 'perpetuity/mongodb/index'
4
4
  require 'set'
5
+ require 'perpetuity/exceptions/duplicate_key_error'
5
6
 
6
7
  module Perpetuity
7
8
  class MongoDB
@@ -20,6 +21,7 @@ module Perpetuity
20
21
 
21
22
  def session
22
23
  @session ||= Moped::Session.new(["#{host}:#{port}"])
24
+ @session.with(safe: true)
23
25
  end
24
26
 
25
27
  def connect
@@ -50,6 +52,13 @@ module Perpetuity
50
52
 
51
53
  collection(klass).insert attributes
52
54
  attributes[:_id]
55
+ rescue Moped::Errors::OperationFailure => e
56
+ if e.message =~ /duplicate key/
57
+ e.message =~ /\$(\w+)_\d.*dup key: { : (.*) }/
58
+ key = $1
59
+ value = $2.gsub("\\\"", "\"")
60
+ raise DuplicateKeyError, "Tried to insert #{klass} with duplicate unique index: #{key} => #{value}"
61
+ end
53
62
  end
54
63
 
55
64
  def count klass
@@ -29,9 +29,10 @@ module Perpetuity
29
29
  QueryExpression.new self, :lte, value
30
30
  end
31
31
 
32
- def not_equal? value
32
+ def != value
33
33
  QueryExpression.new self, :not_equal, value
34
34
  end
35
+ alias :not_equal? :'!='
35
36
 
36
37
  def =~ regexp
37
38
  QueryExpression.new self, :matches, regexp
@@ -1,3 +1,3 @@
1
1
  module Perpetuity
2
- VERSION = "0.4.5"
2
+ VERSION = "0.4.6"
3
3
  end
@@ -91,7 +91,7 @@ describe "retrieval" do
91
91
  end
92
92
 
93
93
  it 'selects objects using inequality' do
94
- selected = Perpetuity[Article].select { |article| article.title.not_equal? 'Draft' }
94
+ selected = Perpetuity[Article].select { |article| article.title != 'Draft' }
95
95
  ids = selected.map(&:id)
96
96
  ids.should_not include draft.id
97
97
  ids.should include published.id
@@ -28,7 +28,7 @@ module Perpetuity
28
28
  end
29
29
 
30
30
  it 'checks for inequality' do
31
- attribute.not_equal?(1).should be_a MongoDB::QueryExpression
31
+ (attribute != 1).should be_a MongoDB::QueryExpression
32
32
  end
33
33
 
34
34
  it 'checks for regexp matches' do
@@ -135,5 +135,20 @@ module Perpetuity
135
135
  mongo.active_indexes(collection).should_not include index
136
136
  end
137
137
  end
138
+
139
+ describe 'operation errors' do
140
+ let(:data) { { foo: 'bar' } }
141
+ let(:index) { mongo.index Object, :foo, unique: true }
142
+
143
+ before { mongo.activate_index! index }
144
+ after { mongo.drop_collection Object }
145
+
146
+ it 'raises an exception when insertion fails' do
147
+ mongo.insert Object, data
148
+
149
+ expect { mongo.insert Object, data }.to raise_error DuplicateKeyError,
150
+ 'Tried to insert Object with duplicate unique index: foo => "bar"'
151
+ end
152
+ end
138
153
  end
139
154
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perpetuity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.4.6
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: 2013-02-21 00:00:00.000000000 Z
12
+ date: 2013-03-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -79,6 +79,7 @@ files:
79
79
  - lib/perpetuity/attribute_set.rb
80
80
  - lib/perpetuity/config.rb
81
81
  - lib/perpetuity/data_injectable.rb
82
+ - lib/perpetuity/exceptions/duplicate_key_error.rb
82
83
  - lib/perpetuity/identity_map.rb
83
84
  - lib/perpetuity/mapper.rb
84
85
  - lib/perpetuity/mapper_registry.rb
@@ -150,21 +151,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
150
151
  - - ! '>='
151
152
  - !ruby/object:Gem::Version
152
153
  version: '0'
153
- segments:
154
- - 0
155
- hash: 3190074601022311513
156
154
  required_rubygems_version: !ruby/object:Gem::Requirement
157
155
  none: false
158
156
  requirements:
159
157
  - - ! '>='
160
158
  - !ruby/object:Gem::Version
161
159
  version: '0'
162
- segments:
163
- - 0
164
- hash: 3190074601022311513
165
160
  requirements: []
166
161
  rubyforge_project:
167
- rubygems_version: 1.8.25
162
+ rubygems_version: 1.8.24
168
163
  signing_key:
169
164
  specification_version: 3
170
165
  summary: Persistence library allowing serialization of Ruby objects