activerecord-import-queue 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5b8a73b3a90243a2c30dc1a69733d32d8538b98b
4
+ data.tar.gz: f3f6e758485c63b770c465d5cadd1d7421ad0fed
5
+ SHA512:
6
+ metadata.gz: 5e6d0692d049f650e664f8d1e3cf0c171bbdbafbaa5af460767c66a7aeb88d0547ebf52281470f575a2e08c00c5180fa78e8f4f4ff5a313268fb1ab0a966f55c
7
+ data.tar.gz: 8f6c52546d44a0402a41a45893afe2406d0153792400cbcaeaa5fd05cdcfd8a1fac211554336c649c0e36864e07f8fb500b39bca2e675b22c739e754432b0888
@@ -0,0 +1,81 @@
1
+ =begin rdoc
2
+
3
+ ActiveRecordImportQueue will maintain a list of ActiveRecord objects and use
4
+ the activerecord-import '.import' extension to import them in to a database
5
+ in bulk.
6
+
7
+ =end
8
+
9
+ class ActiveRecordImportQueue
10
+
11
+ =begin rdoc
12
+
13
+ Create a new instance of the import queue.
14
+
15
+ Specify 'limit=n' to limit the number of queued records to the value 'n'.
16
+ This limit will be checked each time an object is queued.
17
+
18
+ =end
19
+
20
+ def initialize(params={ :limit => 100 })
21
+ @pending = {}
22
+ @count = 0
23
+ @params = params
24
+ end
25
+
26
+ =begin rdoc
27
+
28
+ Returns the total number of pending objects.
29
+
30
+ =end
31
+
32
+ def count_pending
33
+ @count
34
+ end
35
+
36
+ =begin rdoc
37
+
38
+ To queue a new object, use the '<<' method to push an ActiveRecord object on
39
+ to the queue.
40
+
41
+ =end
42
+
43
+ def <<(obj)
44
+ @pending[obj.class] ||= []
45
+ @pending[obj.class] << obj
46
+ incr_count
47
+ check_depth
48
+ end
49
+
50
+ =begin rdoc
51
+
52
+ Imports all queued objects irrespective of whether the queue limit has been
53
+ reached. This should be used at the end of an import run to insert remaining
54
+ objects in to the database.
55
+
56
+ =end
57
+
58
+ def finish
59
+ @pending.keys.collect { |klass| process_class(klass) }
60
+ end
61
+
62
+ private
63
+
64
+ def incr_count
65
+ @count += 1
66
+ end
67
+
68
+ def decr_count
69
+ @count -= 1
70
+ end
71
+
72
+ def process_class(klass)
73
+ klass.import(@pending[klass])
74
+ @pending[klass] = []
75
+ end
76
+
77
+ def check_depth
78
+ @pending.keys.collect { |klass| process_class(klass) if @pending[klass].count > @params[:limit] }
79
+ end
80
+
81
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-import-queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Hicks
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ActiveRecordImportQueue implements a simple queue which calls the 'import'
14
+ method of the base object when there are more than a configurable number of objects
15
+ pending
16
+ email: peter.hicks@poggs.co.uk
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/activerecord-import-queue.rb
22
+ homepage: http://opensource.poggs.com/ruby/gems/activerecord-import-queue
23
+ licenses:
24
+ - GPLv2
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.1.11
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: An ActiveRecord object queue for activerecord-import
46
+ test_files: []