toystore-mongo 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -5,7 +5,7 @@ require 'plucky/version'
5
5
 
6
6
  group(:development) do
7
7
  gem 'bson_ext', Plucky::MongoVersion, :require => false
8
- gem 'SystemTimer'
8
+ gem 'SystemTimer', :platform => :ruby_18
9
9
  gem 'rake', '~> 0.8.7'
10
10
  gem 'rspec', '~> 2.3'
11
11
  gem 'log_buddy', '~> 0.5.0'
data/README.rdoc CHANGED
@@ -6,6 +6,23 @@ Mongo integration for Toystore.
6
6
 
7
7
  $ gem install toystore-mongo
8
8
 
9
+ == Usage
10
+
11
+ class User
12
+ include Toy::Mongo
13
+ store Mongo::Connection.new.db('myapp')['users']
14
+
15
+ attribute :name, String
16
+ end
17
+
18
+ Including Toy::Mongo includes Toy::Store and then does a few things:
19
+
20
+ * Includes Plucky querying so you can do things like User.count, User.all, User.first, and much more
21
+ * Sets the key factory to object id
22
+ * Overrides get so that it also works with string representation of object id
23
+ * Overrides get_multi so that it performs one query instead of one query per id
24
+ * Adds instance method atomic_update_attributes for persisting only the changes (see #persistable_changes)
25
+
9
26
  == Note on Patches/Pull Requests
10
27
 
11
28
  * Fork the project.
@@ -29,6 +29,26 @@ module Toy
29
29
  end
30
30
  end
31
31
 
32
+ def get(id)
33
+ super Plucky.to_object_id(id)
34
+ end
35
+
36
+ # Mongo does not guarantee sort order when using $in.
37
+ # So we manually sort in ruby for now. Not stoked about
38
+ # this, but it gets the job done.
39
+ def get_multi(*ids)
40
+ ids = ids.flatten
41
+ all(:_id => {'$in' => ids}).sort do |a, b|
42
+ index_a = ids.index(a.id)
43
+ index_b = ids.index(b.id)
44
+ if index_a.nil? || index_b.nil?
45
+ 1
46
+ else
47
+ index_a <=> index_b
48
+ end
49
+ end
50
+ end
51
+
32
52
  def query
33
53
  # TODO: add object id keys to convert
34
54
  Plucky::Query.new(store.client, :transformer => transformer).object_ids(object_id_attributes)
@@ -73,7 +93,12 @@ module Toy
73
93
  criteria = {'_id' => id}
74
94
  criteria.update(opts[:criteria]) if opts[:criteria]
75
95
  options[:safe] = opts.key?(:safe) ? opts[:safe] : store.options[:safe]
76
- store.client.update(criteria, update, options)
96
+
97
+ run_callbacks(:save) do
98
+ run_callbacks(:update) do
99
+ store.client.update(criteria, update, options)
100
+ end
101
+ end
77
102
  end
78
103
  end
79
104
  end
@@ -1,5 +1,5 @@
1
1
  module Toy
2
2
  module Mongo
3
- VERSION = '0.7.0'
3
+ VERSION = '0.8.0'
4
4
  end
5
5
  end
data/lib/toy/mongo.rb CHANGED
@@ -10,10 +10,10 @@ module Toy
10
10
  extend ActiveSupport::Concern
11
11
 
12
12
  included do
13
+ include Toy::Store
13
14
  include Querying
15
+
16
+ key(Toy::Identity::ObjectIdKeyFactory.new)
14
17
  end
15
18
  end
16
19
  end
17
-
18
- Toy.key_factory = Toy::Identity::ObjectIdKeyFactory.new
19
- Toy.plugin(Toy::Mongo)
data/spec/helper.rb CHANGED
@@ -15,6 +15,7 @@ Bundler.require(:development)
15
15
 
16
16
  require 'toy/mongo'
17
17
  require 'support/constants'
18
+ require 'support/callbacks_helper'
18
19
 
19
20
  STORE = Mongo::Connection.new.db('testing')['toystore-mongo']
20
21
 
@@ -23,7 +24,7 @@ Logger.new(log_path.join('test.log')).tap do |log|
23
24
  Toy.logger = log
24
25
  end
25
26
 
26
- Rspec.configure do |c|
27
+ RSpec.configure do |c|
27
28
  c.include(Support::Constants)
28
29
 
29
30
  c.before(:each) do
@@ -0,0 +1,22 @@
1
+ module CallbacksHelper
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ [ :before_create, :after_create,
6
+ :before_update, :after_update,
7
+ :before_save, :after_save,
8
+ :before_destroy, :after_destroy].each do |callback|
9
+ callback_method = "#{callback}_callback"
10
+ send(callback, callback_method)
11
+ define_method(callback_method) { history << callback.to_sym }
12
+ end
13
+ end
14
+
15
+ def history
16
+ @history ||= []
17
+ end
18
+
19
+ def clear_history
20
+ @history = nil
21
+ end
22
+ end
@@ -33,7 +33,7 @@ module Support
33
33
  def self.name; '#{name}' end
34
34
  def self.to_s; '#{name}' end
35
35
  """ if name
36
- model.send(:include, Toy::Store)
36
+ model.send(:include, Toy::Mongo)
37
37
  model.store(:mongo, STORE)
38
38
  end
39
39
  end
@@ -4,6 +4,7 @@ describe Toy::Mongo::Querying do
4
4
  uses_constants('User')
5
5
 
6
6
  before(:each) do
7
+ User.send(:include, CallbacksHelper)
7
8
  User.identity_map_off
8
9
  User.attribute(:name, String)
9
10
  User.attribute(:bio, String)
@@ -48,6 +49,12 @@ describe Toy::Mongo::Querying do
48
49
  @user.atomic_update('$set' => {'name' => 'Frank'})
49
50
  end
50
51
 
52
+ it "runs callbacks in correct order" do
53
+ doc = User.create.tap(&:clear_history)
54
+ doc.atomic_update({})
55
+ doc.history.should == [:before_save, :before_update, :after_update, :after_save]
56
+ end
57
+
51
58
  context "with :safe option" do
52
59
  it "overrides store's :safe option" do
53
60
  User.store(:mongo, STORE, :safe => false)
@@ -132,4 +139,40 @@ describe Toy::Mongo::Querying do
132
139
  @user.atomic_update_attributes(:name => 'Frank')
133
140
  end
134
141
  end
142
+
143
+ describe "#get" do
144
+ before(:each) do
145
+ @user = User.create
146
+ end
147
+
148
+ it "works for string object id" do
149
+ User.get(@user.id.to_s).should == @user
150
+ end
151
+
152
+ it "works for object id" do
153
+ User.get(@user.id).should == @user
154
+ end
155
+
156
+ it "returns nil for invalid object id" do
157
+ User.get('1234').should be_nil
158
+ end
159
+ end
160
+
161
+ describe "#get_multi" do
162
+ before(:each) do
163
+ @user1 = User.create
164
+ @user2 = User.create
165
+ @users = User.get_multi(@user2.id, @user1.id)
166
+ end
167
+
168
+ it "returns multiple documents in correct order" do
169
+ @users.should == [@user2, @user1]
170
+ end
171
+
172
+ it "performs one query" do
173
+ User.should_not_receive(:get)
174
+ User.should_receive(:all).once.and_return([])
175
+ User.get_multi(@user1.id, @user2.id)
176
+ end
177
+ end
135
178
  end
@@ -3,8 +3,9 @@ require 'helper'
3
3
  describe Toy::Mongo do
4
4
  it "defaults key factory to object_id" do
5
5
  klass = Class.new do
6
- include Toy::Store
6
+ include Toy::Mongo
7
7
  end
8
+
8
9
  klass.key_factory.should be_instance_of(Toy::Identity::ObjectIdKeyFactory)
9
10
  end
10
11
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toystore-mongo
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 63
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 7
8
+ - 8
9
9
  - 0
10
- version: 0.7.0
10
+ version: 0.8.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Nunemaker
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-07 00:00:00 Z
18
+ date: 2011-09-23 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: plucky
@@ -88,6 +88,7 @@ files:
88
88
  - lib/toystore-mongo.rb
89
89
  - spec/helper.rb
90
90
  - spec/spec.opts
91
+ - spec/support/callbacks_helper.rb
91
92
  - spec/support/constants.rb
92
93
  - spec/toy/extensions/bson_object_id_spec.rb
93
94
  - spec/toy/identity/object_id_key_factory_spec.rb
@@ -123,16 +124,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
124
  requirements: []
124
125
 
125
126
  rubyforge_project:
126
- rubygems_version: 1.7.2
127
+ rubygems_version: 1.8.9
127
128
  signing_key:
128
129
  specification_version: 3
129
130
  summary: Mongo integration for Toystore
130
131
  test_files:
131
132
  - spec/helper.rb
132
133
  - spec/spec.opts
134
+ - spec/support/callbacks_helper.rb
133
135
  - spec/support/constants.rb
134
136
  - spec/toy/extensions/bson_object_id_spec.rb
135
137
  - spec/toy/identity/object_id_key_factory_spec.rb
136
138
  - spec/toy/mongo/querying_spec.rb
137
139
  - spec/toy/mongo_spec.rb
138
- has_rdoc: