remote_table 1.1.4 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/remote_table.rb CHANGED
@@ -54,6 +54,13 @@ class RemoteTable
54
54
  end
55
55
 
56
56
  def each(&blk)
57
+ to_a.each { |row| yield row }
58
+ end
59
+ alias :each_row :each
60
+
61
+ def to_a
62
+ return @to_a if @to_a.is_a? ::Array
63
+ @to_a = []
57
64
  format.each do |row|
58
65
  row['row_hash'] = ::RemoteTable.hasher.hash row
59
66
  # allow the transformer to return multiple "virtual rows" for every real row
@@ -64,26 +71,26 @@ class RemoteTable
64
71
  end
65
72
  next if properties.select and !properties.select.call(virtual_row)
66
73
  next if properties.reject and properties.reject.call(virtual_row)
67
- yield virtual_row
74
+ @to_a.push virtual_row
68
75
  end
69
76
  end
77
+ @to_a
70
78
  end
71
- alias :each_row :each
79
+ alias :rows :to_a
72
80
 
73
81
  # Get a row by row number
74
82
  def [](row_number)
75
83
  to_a[row_number]
76
84
  end
77
85
 
78
- # Get the whole row array back
79
- def to_a
80
- return @to_a if @to_a.is_a? ::Array
81
- @to_a = []
82
- each { |row| @to_a.push row }
83
- @to_a.freeze
86
+ # clear the row cache to save memory
87
+ def free
88
+ @to_a.clear if @to_a.is_a?(::Array)
89
+ @to_a = nil
90
+ ::GC.start
91
+ nil
84
92
  end
85
- alias :rows :to_a
86
-
93
+
87
94
  # Used internally to execute stuff in shells.
88
95
  def self.executor
89
96
  Executor.instance
@@ -20,9 +20,5 @@ class RemoteTable
20
20
  def each
21
21
  raise "must be defined by format"
22
22
  end
23
-
24
- def delete_file!
25
- ::FileUtils.rm_rf t.properties.staging_dir_path
26
- end
27
23
  end
28
24
  end
@@ -41,7 +41,7 @@ class RemoteTable
41
41
  yield ordered_hash if t.properties.keep_blank_rows or filled_values > 0
42
42
  end
43
43
  ensure
44
- delete_file!
44
+ t.local_file.delete
45
45
  end
46
46
 
47
47
  private
@@ -14,7 +14,7 @@ class RemoteTable
14
14
  yield hash if t.properties.keep_blank_rows or hash.any? { |k, v| v.present? }
15
15
  end
16
16
  ensure
17
- delete_file!
17
+ t.local_file.delete
18
18
  end
19
19
  private
20
20
  def parser
@@ -18,7 +18,7 @@ class RemoteTable
18
18
  yield hash if t.properties.keep_blank_rows or hash.any? { |k, v| v.present? }
19
19
  end
20
20
  ensure
21
- delete_file!
21
+ t.local_file.delete
22
22
  end
23
23
 
24
24
  private
@@ -31,6 +31,8 @@ class RemoteTable
31
31
  end
32
32
  yield ordered_hash if t.properties.keep_blank_rows or ordered_hash.any? { |k, v| v.present? }
33
33
  end
34
+ ensure
35
+ t.local_file.delete
34
36
  end
35
37
 
36
38
  private
@@ -2,7 +2,7 @@ require 'fileutils'
2
2
  require 'escape'
3
3
  require 'tmpdir'
4
4
  class RemoteTable
5
- class LocalFile
5
+ class LocalFile #:nodoc:all
6
6
 
7
7
  attr_reader :t
8
8
 
@@ -15,11 +15,25 @@ class RemoteTable
15
15
  @path
16
16
  end
17
17
 
18
+ def delete
19
+ ::FileUtils.rm_rf staging_dir_path
20
+ @path = nil
21
+ @staging_dir_path = nil
22
+ end
23
+
18
24
  private
19
25
 
26
+ def staging_dir_path #:nodoc:
27
+ return @staging_dir_path if @staging_dir_path.is_a?(::String)
28
+ srand # in case this was forked by resque
29
+ @staging_dir_path = ::File.join ::Dir.tmpdir, 'remote_table_gem', rand.to_s
30
+ ::FileUtils.mkdir_p @staging_dir_path
31
+ @staging_dir_path
32
+ end
33
+
20
34
  def save_locally
21
35
  return if @path.is_a?(::String)
22
- @path = ::File.join(t.properties.staging_dir_path, ::File.basename(t.properties.uri.path))
36
+ @path = ::File.join(staging_dir_path, ::File.basename(t.properties.uri.path))
23
37
  download
24
38
  decompress
25
39
  unpack
@@ -209,13 +209,5 @@ class RemoteTable
209
209
  Format::Delimited
210
210
  end
211
211
  end
212
-
213
- def staging_dir_path #:nodoc:
214
- return @staging_dir_path if @staging_dir_path.is_a?(::String)
215
- srand # in case this was forked by resque
216
- @staging_dir_path = ::File.join ::Dir.tmpdir, 'remote_table_gem', rand.to_s
217
- ::FileUtils.mkdir_p @staging_dir_path
218
- @staging_dir_path
219
- end
220
212
  end
221
213
  end
@@ -1,3 +1,3 @@
1
1
  class RemoteTable
2
- VERSION = "1.1.4"
2
+ VERSION = "1.1.6"
3
3
  end
@@ -43,4 +43,20 @@ class TestRemoteTable < Test::Unit::TestCase
43
43
  :select => lambda { |row| row['Vehicle Age'].to_i.to_s == row['Vehicle Age'] }
44
44
  assert_equal '9.09%', t[0]['LDGV']
45
45
  end
46
+
47
+ should 'not blow up if each is called twice' do
48
+ t = RemoteTable.new 'http://spreadsheets.google.com/pub?key=tObVAGyqOkCBtGid0tJUZrw'
49
+ assert_nothing_raised do
50
+ t.each { |row| }
51
+ t.each { |row| }
52
+ end
53
+ end
54
+
55
+ should 'allow itself to be cleared for save memory' do
56
+ t = RemoteTable.new 'http://spreadsheets.google.com/pub?key=tObVAGyqOkCBtGid0tJUZrw'
57
+ t.to_a
58
+ assert_equal Array, t.instance_variable_get(:@to_a).class
59
+ t.free
60
+ assert_equal NilClass, t.instance_variable_get(:@to_a).class
61
+ end
46
62
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remote_table
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 4
10
- version: 1.1.4
9
+ - 6
10
+ version: 1.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Seamus Abshere
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-03-30 00:00:00 -05:00
19
+ date: 2011-03-31 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency