ar-ondemand 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Yjk2ZDkxMGM4MDFkY2E4MmVhY2U0ZjVhNDFiZjE5YmMyZmY2ZjkyYw==
4
+ NWZmY2Y4ZmY1MzQxODNmZjc1NDI2NmY4OTY3MTdkZGQ1OGZmZmZmMA==
5
5
  data.tar.gz: !binary |-
6
- MGYwY2ZlNzZkYjZhMjYxNGVkYTUyZjljMTQwZDVkMDQyYTVhNWNhNg==
6
+ Yzg2YzI2ZTA2MmMwNjM0OThjYTJjZTBlYmIyYTgwMTE0ODE0NDM2MQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NDA3ZmVlZDQxNzgyZDUwYjE2MmQwZWNiNjJkYmIyNGJhMjQyMmY0ZGRmMDY5
10
- MWQ0ZWNmZWVjNTFjNzk0N2M0NjRlNzg0NTFjMzgxMTAyNjUwMzliMzQzZjJl
11
- ZTRkZjYzNDVjOWI5N2YzZmQ3ZTU4NjRjMWY2NWJmMjIxZjA5MTE=
9
+ OWNjYjA5MzcwMGZjNWZiOWZlZDM4MjIzMTFjZTQ2ZDBhNWJjZTI1NzYxNjdh
10
+ NDYwM2JjOTg3ZjQxYTljMDYxYzQ1YjNhYWQxNjZhYjU5NDFhMGNmZWIwZDdk
11
+ YzhmZjkxZWQxNTI3OGE1ZjYxNDBjNmY5YmY1ZDk1NWE1NzExZGU=
12
12
  data.tar.gz: !binary |-
13
- MzA3Yjc4YmQ3ODA4NTIxMTFjNzIyY2M4MjhkMGM0MTQ4YTM3YTJlMTgxNWQy
14
- ZWNhZDFiNjQ0OTA3YzczODE5ZDcwMGRkODhhODU5NDVhMDU3ZGMyODkwODll
15
- OTkxMTgwMzFmYjY4MTJlYmYzZGJiYWFkOTZmN2Y2NTZkODMwZTM=
13
+ NjNiMTk3MjUwNWI3M2VhMmJmZmVmMjA2Mzg4YmZkMWNkNzFjYTNlMTYzNTgw
14
+ YWRkMTQwZWIwOTMyOTE3MGE1NzgwNTRiMDZmOWE0YmUwN2FhOTNjNzc2MDk1
15
+ NDkyYmNmNjdlODBkYTlkMDNhYjRlYzY1MWRlMzk3MmE1ODQ2OTg=
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .bundle
2
+ vendor/
3
+ bin/
4
+ *.log
5
+ spec/log
6
+ .ruby*
7
+ .rspec
8
+ ar-ondemand*.gem
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test, :development do
6
+ if RUBY_ENGINE == 'jruby'
7
+ gem 'activerecord-jdbc-adapter'
8
+ else
9
+ gem 'sqlite3'
10
+ end
11
+
12
+ gem 'rspec'
13
+ gem 'factory_girl'
14
+ gem 'shoulda'
15
+ gem 'shoulda-matchers'
16
+ gem 'pry'
17
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ar-ondemand (1.0.1)
5
+ activerecord (>= 3.2)
6
+ activesupport (>= 3.2)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activemodel (3.2.21)
12
+ activesupport (= 3.2.21)
13
+ builder (~> 3.0.0)
14
+ activerecord (3.2.21)
15
+ activemodel (= 3.2.21)
16
+ activesupport (= 3.2.21)
17
+ arel (~> 3.0.2)
18
+ tzinfo (~> 0.3.29)
19
+ activesupport (3.2.21)
20
+ i18n (~> 0.6, >= 0.6.4)
21
+ multi_json (~> 1.0)
22
+ arel (3.0.3)
23
+ builder (3.0.4)
24
+ coderay (1.1.0)
25
+ diff-lcs (1.2.5)
26
+ factory_girl (4.5.0)
27
+ activesupport (>= 3.0.0)
28
+ i18n (0.6.11)
29
+ method_source (0.8.2)
30
+ multi_json (1.10.1)
31
+ pry (0.10.1)
32
+ coderay (~> 1.1.0)
33
+ method_source (~> 0.8.1)
34
+ slop (~> 3.4)
35
+ rspec (2.99.0)
36
+ rspec-core (~> 2.99.0)
37
+ rspec-expectations (~> 2.99.0)
38
+ rspec-mocks (~> 2.99.0)
39
+ rspec-core (2.99.2)
40
+ rspec-expectations (2.99.2)
41
+ diff-lcs (>= 1.1.3, < 2.0)
42
+ rspec-mocks (2.99.2)
43
+ shoulda (3.5.0)
44
+ shoulda-context (~> 1.0, >= 1.0.1)
45
+ shoulda-matchers (>= 1.4.1, < 3.0)
46
+ shoulda-context (1.2.1)
47
+ shoulda-matchers (2.7.0)
48
+ activesupport (>= 3.0.0)
49
+ slop (3.6.0)
50
+ sqlite3 (1.3.10)
51
+ tzinfo (0.3.42)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ ar-ondemand!
58
+ factory_girl
59
+ pry
60
+ rspec
61
+ shoulda
62
+ shoulda-matchers
63
+ sqlite3
data/README.md CHANGED
@@ -1 +1,4 @@
1
1
  # ar-ondemand
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/ar-ondemand.svg)](http://badge.fury.io/rb/ar-ondemand)
4
+ [![](https://ci.solanolabs.com:443/cloudhealthtech/ar-ondemand/badges/170027.png?badge_token=bd73a19d5421a68f29e22ad15ad080cbabc56ba7)](https://ci.solanolabs.com:443/cloudhealthtech/ar-ondemand/suites/170027)
@@ -0,0 +1,55 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActiveRecord
4
+ module OnDemand
5
+ module DeleteAllByPkExtension
6
+ extend ::ActiveSupport::Concern
7
+
8
+ # Ripped from the find_in_batches function, but customized to return an ::ActiveRecord::OnDemand::ResultSet
9
+ def delete_all_by_pk(options = {})
10
+ relation = self
11
+
12
+ unless arel.orders.blank? && arel.taken.blank?
13
+ ActiveRecord::Base.logger.warn("Scoped order and limit are ignored, it's forced to be batch order and batch size")
14
+ end
15
+
16
+ if (finder_options = options.except(:start, :batch_size)).present?
17
+ raise "You can't specify an order, it's forced to be #{batch_order}" if options[:order].present?
18
+ raise "You can't specify a limit, it's forced to be the batch_size" if options[:limit].present?
19
+
20
+ relation = apply_finder_options(finder_options)
21
+ end
22
+
23
+ start = options.delete(:start)
24
+ batch_size = options.delete(:batch_size) || 1000
25
+
26
+ relation = relation.reorder(batch_order).limit(batch_size)
27
+ records = start ? query(relation.where(table[primary_key].gteq(start))) : query(relation)
28
+
29
+ while records.any?
30
+ records_size = records.size
31
+ primary_key_offset = records.last
32
+
33
+ self.unscoped.where(id: records).delete_all
34
+
35
+ break if records_size < batch_size
36
+
37
+ records = query relation.where(table[primary_key].gt(primary_key_offset))
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def query(ar)
44
+ results = ::ActiveRecord::Base.connection.exec_query ar.select([table[primary_key]]).to_sql
45
+ results.rows.flatten
46
+ end
47
+
48
+ def batch_order
49
+ "#{quoted_table_name}.#{quoted_primary_key} ASC"
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ ::ActiveRecord::Relation.send(:include, ::ActiveRecord::OnDemand::DeleteAllByPkExtension)
@@ -1,3 +1,3 @@
1
1
  module ArOnDemand
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
data/lib/ar-ondemand.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require_dependency 'ar-ondemand/on_demand'
2
- require_dependency 'ar-ondemand/for_reading'
2
+ require_dependency 'ar-ondemand/for_reading'
3
+ require_dependency 'ar-ondemand/delete_all_by_pk'
data/solano.yml ADDED
@@ -0,0 +1,8 @@
1
+ ---
2
+ :tddium:
3
+ :ruby_version: ruby-1.9.3-p392
4
+ :coverage: false
5
+ :rake:
6
+ :rails_env: test
7
+ :environment:
8
+ TZ: UTC
@@ -0,0 +1,3 @@
1
+ class AuditRecord < ActiveRecord::Base
2
+
3
+ end
data/spec/db/schema.rb ADDED
@@ -0,0 +1,11 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table "audit_records", :force => true do |t|
3
+ t.integer "customer_id"
4
+ t.integer "model_id"
5
+ t.integer "model_type_id"
6
+ t.string "action", :limit => 6
7
+ t.text "description"
8
+ t.datetime "created_at", :null => false
9
+ t.datetime "updated_at", :null => false
10
+ end
11
+ end
data/spec/db/seeds.rb ADDED
File without changes
@@ -0,0 +1,11 @@
1
+ # Read about factories at https://github.com/thoughtbot/factory_girl
2
+
3
+ FactoryGirl.define do
4
+ factory :audit_record do
5
+ customer_id 1
6
+ sequence(:model_id)
7
+ model_type_id { rand(100_000) }
8
+ action 'create'
9
+ description { (0...50).map { ('a'..'z').to_a[rand(26)] }.join }
10
+ end
11
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+ require 'ar-ondemand'
3
+
4
+ describe 'ForReading' do
5
+ context 'Testing' do
6
+ context 'Ensure Persistance Works' do
7
+ before(:each) do
8
+ create(:audit_record)
9
+ end
10
+
11
+ it 'should do persist' do
12
+ AuditRecord.all.length.should eq(1)
13
+ end
14
+
15
+ it 'should do foo' do
16
+ AuditRecord.first.action.should eq('create')
17
+ end
18
+ end
19
+
20
+ context 'Iterating' do
21
+ before(:each) do
22
+ (1..100).each do
23
+ create(:audit_record)
24
+ end
25
+ end
26
+
27
+ it 'should support iterating' do
28
+ total = 0
29
+ AuditRecord.where(customer_id: 1).for_reading.each do |r|
30
+ total += 1
31
+ end
32
+ total.should eq(100)
33
+ end
34
+
35
+ it 'should produce same results as regular iterating' do
36
+ records_a = Set.new
37
+ AuditRecord.where(customer_id: 1).for_reading.each do |r|
38
+ records_a.add r.id
39
+ end
40
+
41
+ records_b = Set.new
42
+ AuditRecord.where(customer_id: 1).each do |r|
43
+ records_b.add r.id
44
+ end
45
+
46
+ records_a.should eq(records_b)
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,19 @@
1
+ require 'rspec'
2
+ require 'rspec/core'
3
+ require 'rspec/expectations'
4
+ require 'active_support'
5
+ require 'active_record'
6
+
7
+ I18n.enforce_available_locales = false
8
+
9
+ Dir['spec/support/**/*.rb'].sort.each { |f| load f }
10
+
11
+ require 'factory_girl'
12
+
13
+ RSpec.configure do |config|
14
+ config.include FactoryGirl::Syntax::Methods
15
+ end
16
+
17
+ FactoryGirl.find_definitions
18
+
19
+ Dir['spec/app/models/*.rb'].sort.each { |f| load f }
@@ -0,0 +1,18 @@
1
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
2
+
3
+ ActiveRecord::Migration.suppress_messages do
4
+ load 'spec/db/schema.rb'
5
+ load 'spec/db/seeds.rb'
6
+ end
7
+
8
+ require 'shoulda'
9
+ require 'shoulda/matchers/integrations/rspec'
10
+
11
+ RSpec.configure do |config|
12
+ config.around do |example|
13
+ ActiveRecord::Base.transaction do
14
+ example.run
15
+ raise ActiveRecord::Rollback
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar-ondemand
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Frank
@@ -47,14 +47,26 @@ executables: []
47
47
  extensions: []
48
48
  extra_rdoc_files: []
49
49
  files:
50
+ - .gitignore
51
+ - Gemfile
52
+ - Gemfile.lock
50
53
  - README.md
51
54
  - ar-ondemand.gemspec
52
55
  - lib/ar-ondemand.rb
56
+ - lib/ar-ondemand/delete_all_by_pk.rb
53
57
  - lib/ar-ondemand/for_reading.rb
54
58
  - lib/ar-ondemand/on_demand.rb
55
59
  - lib/ar-ondemand/record.rb
56
60
  - lib/ar-ondemand/result.rb
57
61
  - lib/ar-ondemand/version.rb
62
+ - solano.yml
63
+ - spec/app/models/audit_record.rb
64
+ - spec/db/schema.rb
65
+ - spec/db/seeds.rb
66
+ - spec/factories/audit_record.rb
67
+ - spec/lib/for_reading_spec.rb
68
+ - spec/spec_helper.rb
69
+ - spec/support/active_record.rb
58
70
  homepage: https://github.com/CloudHealth/ar-ondemand
59
71
  licenses:
60
72
  - MIT
@@ -80,4 +92,3 @@ signing_key:
80
92
  specification_version: 4
81
93
  summary: ActiveRecord On-demand
82
94
  test_files: []
83
- has_rdoc: