loady 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/loady/csv_loader.rb +46 -45
- data/lib/loady/memory_logger.rb +2 -2
- data/lib/loady/version.rb +1 -1
- metadata +4 -4
data/lib/loady/csv_loader.rb
CHANGED
@@ -4,56 +4,57 @@ require 'logger'
|
|
4
4
|
module Loady
|
5
5
|
class CsvLoader
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
yield row
|
40
|
-
success += 1
|
7
|
+
class << self
|
8
|
+
# valid options:
|
9
|
+
# :skip_first_row => true -- default = false
|
10
|
+
# :logger => Logger.new('/somewhere/file.log') -- default = Logger.new(STDOUT)
|
11
|
+
# plus any valid options you can pass to CSV.new:
|
12
|
+
# see http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/classes/CSV.html#M000190
|
13
|
+
def read(filename, options={})
|
14
|
+
success = 0
|
15
|
+
warning = 0
|
16
|
+
|
17
|
+
logger = options.delete(:logger) || default_logger
|
18
|
+
|
19
|
+
f = File.new(filename)
|
20
|
+
f.readline if options.delete(:skip_first_row)
|
21
|
+
|
22
|
+
line_number = 0
|
23
|
+
|
24
|
+
begin
|
25
|
+
while (line = f.readline)
|
26
|
+
begin
|
27
|
+
line_number += 1
|
28
|
+
|
29
|
+
row = CSV.parse(line, options)[0]
|
30
|
+
row.extend Loady::Array
|
31
|
+
|
32
|
+
unless row.empty?
|
33
|
+
yield row
|
34
|
+
success += 1
|
35
|
+
end
|
36
|
+
rescue Exception => message
|
37
|
+
warning += 1
|
38
|
+
logger.warn "#{message.to_s.gsub("line 1", "line #{line_number}")}\n#{line}"
|
41
39
|
end
|
42
|
-
rescue Exception => message
|
43
|
-
warning += 1
|
44
|
-
logger.warn "#{message.to_s.gsub("line 1", "line #{line_number}")}\n#{line}"
|
45
40
|
end
|
41
|
+
rescue EOFError
|
42
|
+
f.close
|
43
|
+
rescue Exception => message
|
44
|
+
warning += 1
|
45
|
+
line_number += 1
|
46
|
+
logger.error "#{message.to_s} - at line #{line_number}\n#{line}"
|
46
47
|
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
logger
|
48
|
+
|
49
|
+
logger.info "Finished. Loaded #{success} rows. #{warning} unprocessed rows."
|
50
|
+
end
|
51
|
+
|
52
|
+
def default_logger
|
53
|
+
logger = Logger.new(STDOUT)
|
54
|
+
logger.datetime_format = "%H:%M:%S"
|
55
|
+
logger
|
53
56
|
end
|
54
57
|
|
55
|
-
logger.info "Finished. Loaded #{success} rows. #{warning} unprocessed rows."
|
56
58
|
end
|
57
|
-
|
58
59
|
end
|
59
60
|
end
|
data/lib/loady/memory_logger.rb
CHANGED
data/lib/loady/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loady
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
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: 2012-11-
|
12
|
+
date: 2012-11-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: test-unit
|
@@ -98,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
98
98
|
version: '0'
|
99
99
|
segments:
|
100
100
|
- 0
|
101
|
-
hash:
|
101
|
+
hash: -1716122112452948356
|
102
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
103
|
none: false
|
104
104
|
requirements:
|
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
107
|
version: '0'
|
108
108
|
segments:
|
109
109
|
- 0
|
110
|
-
hash:
|
110
|
+
hash: -1716122112452948356
|
111
111
|
requirements: []
|
112
112
|
rubyforge_project: loady
|
113
113
|
rubygems_version: 1.8.24
|