believer 0.2.17 → 0.2.18

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -173,6 +173,12 @@ Album.allow_filtering(true) # -> SELECT * FROM albums ALLOW FILTERING
173
173
  Album.allow_filtering(false) # -> SELECT * FROM albums
174
174
  ```
175
175
 
176
+ #### Query methods
177
+ All query instances are decorated with a few data manipulation methods:
178
+ * update_all(values): updates all objects returned by the query with the given values
179
+ * destroy_all: destroys all objects returned by the query. This first loads the object before deleting, allowing callbacks to be called.
180
+ * destroy_all: directly deletes rows the query references. No object are loaded.
181
+
176
182
  #### Method chaining
177
183
  All query methods can be chained.
178
184
  This is done by creating and returning a clone of the receiver. The clone is the receiver of the query method.
data/lib/believer.rb CHANGED
@@ -50,6 +50,8 @@ require 'believer/limit'
50
50
  require 'believer/order_by'
51
51
  require 'believer/filter_command'
52
52
  require 'believer/act_as_enumerable'
53
+ require 'believer/updating'
54
+ require 'believer/purging'
53
55
  require 'believer/query'
54
56
  require 'believer/empty_result'
55
57
  require 'believer/delete'
@@ -0,0 +1,38 @@
1
+ module Believer
2
+ module Purging
3
+
4
+ # Destroys all objects returned by the query. This first loads the object before deleting, allowing callbacks to be called.
5
+ def destroy_all
6
+ objects = to_a
7
+ objects.each do |obj|
8
+ obj.destroy
9
+ end
10
+ objects.size
11
+ end
12
+
13
+ # Directly deletes rows the query references. No object are loaded.
14
+ def delete_all
15
+ del = Delete.new(:record_class => self.record_class)
16
+ del.wheres = self.wheres.dup
17
+ del.execute
18
+ end
19
+
20
+ def delete_all_chunked(options = {})
21
+ cnt = count
22
+ chunk_size = options[:delete_batch_chunk_size] || (self.limit_to.nil? ? nil : self.limit_to.size) || cnt
23
+ key_cols = self.record_class.primary_key_columns
24
+ deleted_count = 0
25
+ while deleted_count < cnt
26
+ batch = Batch.new(:record_class => @record_class)
27
+ rows_to_delete = clone.select(key_cols).limit(chunk_size).execute
28
+ rows_to_delete.each do |row_to_delete|
29
+ batch << Delete.new(:record_class => self.record_class).where(row_to_delete)
30
+ end
31
+ batch.execute
32
+ deleted_count += batch.commands.size
33
+ end
34
+ deleted_count
35
+ end
36
+
37
+ end
38
+ end
@@ -3,6 +3,8 @@ require 'believer/extending'
3
3
  module Believer
4
4
  class Query < FilterCommand
5
5
  include Extending
6
+ include Updating
7
+ include Purging
6
8
  include ActAsEnumerable
7
9
 
8
10
  DEFAULT_FILTER_LIMIT = 10000
@@ -99,37 +101,6 @@ module Believer
99
101
  cql
100
102
  end
101
103
 
102
- def destroy_all
103
- objects = to_a
104
- objects.each do |obj|
105
- obj.destroy
106
- end
107
- objects.size
108
- end
109
-
110
- def delete_all
111
- del = Delete.new(:record_class => self.record_class)
112
- del.wheres = self.wheres.dup
113
- del.execute
114
- end
115
-
116
- def delete_all_chunked(options = {})
117
- cnt = count
118
- chunk_size = options[:delete_batch_chunk_size] || (self.limit_to.nil? ? nil : self.limit_to.size) || cnt
119
- key_cols = self.record_class.primary_key_columns
120
- deleted_count = 0
121
- while deleted_count < cnt
122
- batch = Batch.new(:record_class => @record_class)
123
- rows_to_delete = clone.select(key_cols).limit(chunk_size).execute
124
- rows_to_delete.each do |row_to_delete|
125
- batch << Delete.new(:record_class => self.record_class).where(row_to_delete)
126
- end
127
- batch.execute
128
- deleted_count += batch.commands.size
129
- end
130
- deleted_count
131
- end
132
-
133
104
  def to_a
134
105
  if @loaded_objects.nil?
135
106
  result = execute
@@ -0,0 +1,21 @@
1
+ module Believer
2
+ module Updating
3
+
4
+ # Updates all objects returned by the query with the given values
5
+ # @param values [Hash] a hash of new values
6
+ def update_all(values)
7
+ q = clone
8
+ q.selects = primary_key_columns
9
+
10
+ batch = Batch.new(:record_class => record_class)
11
+
12
+ q.each do |obj|
13
+ update = Update.create(obj)
14
+ update.values = values
15
+ batch << update
16
+ end
17
+ batch.execute
18
+ end
19
+
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  module Believer
2
2
  module Version
3
- VERSION = '0.2.17'
3
+ VERSION = '0.2.18'
4
4
  end
5
5
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Believer::Updating do
4
+
5
+ it "should update all" do
6
+ Test::Album.create(
7
+ [
8
+ {:artist_name => 'Beatles', :name => 'Help', :release_date => Time.utc(1965)},
9
+ {:artist_name => 'Jethro Tull', :name => 'Aqualung', :release_date => Time.utc(1971)},
10
+ {:artist_name => 'Jethro Tull', :name => 'Standup', :release_date => Time.utc(1969)},
11
+ {:artist_name => 'Michael Jackson', :name => 'Thriller', :release_date => Time.utc(1982)}
12
+ ])
13
+
14
+ Test::Album.where(:artist_name => 'Jethro Tull').update_all(:release_date => Time.utc(2000))
15
+ albums = Test::Album.where(:artist_name => 'Jethro Tull').to_a
16
+ albums.each do |album|
17
+ expect(album.release_date).to eql(Time.utc(2000))
18
+ end
19
+
20
+ help = Test::Album.where(:artist_name => 'Beatles', :name => 'Help').first
21
+ expect(help.release_date).to eql(Time.utc(1965))
22
+ end
23
+
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: believer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.17
4
+ version: 0.2.18
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-11-21 00:00:00.000000000 Z
12
+ date: 2013-11-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -162,6 +162,7 @@ files:
162
162
  - lib/believer/observer.rb
163
163
  - lib/believer/order_by.rb
164
164
  - lib/believer/persistence.rb
165
+ - lib/believer/purging.rb
165
166
  - lib/believer/query.rb
166
167
  - lib/believer/querying.rb
167
168
  - lib/believer/relation.rb
@@ -169,6 +170,7 @@ files:
169
170
  - lib/believer/test/database.rb
170
171
  - lib/believer/test/test_run_life_cycle.rb
171
172
  - lib/believer/update.rb
173
+ - lib/believer/updating.rb
172
174
  - lib/believer/values.rb
173
175
  - lib/believer/version.rb
174
176
  - lib/believer/where_clause.rb
@@ -194,6 +196,7 @@ files:
194
196
  - spec/believer/test_run_life_cycle_spec.rb
195
197
  - spec/believer/time_series_spec.rb
196
198
  - spec/believer/update_spec.rb
199
+ - spec/believer/updating_spec.rb
197
200
  - spec/believer/where_spec.rb
198
201
  - spec/spec_helper.rb
199
202
  - spec/support/setup_database.rb
@@ -244,6 +247,7 @@ test_files:
244
247
  - spec/believer/test_run_life_cycle_spec.rb
245
248
  - spec/believer/time_series_spec.rb
246
249
  - spec/believer/update_spec.rb
250
+ - spec/believer/updating_spec.rb
247
251
  - spec/believer/where_spec.rb
248
252
  - spec/spec_helper.rb
249
253
  - spec/support/setup_database.rb