loady 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,56 +4,57 @@ require 'logger'
4
4
  module Loady
5
5
  class CsvLoader
6
6
 
7
- # valid options:
8
- # :skip_first_row => true -- default = false
9
- # :logger => Logger.new('/somewhere/file.log') -- default = Logger.new(STDOUT)
10
- # plus any valid options you can pass to CSV.new:
11
- # see http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/classes/CSV.html#M000190
12
- def self.read(filename, options={})
13
- success = 0
14
- warning = 0
15
-
16
- logger = options[:logger]
17
-
18
- unless logger
19
- logger = Logger.new(STDOUT)
20
- logger.datetime_format = "%H:%M:%S"
21
- end
22
-
23
- f = File.new(filename)
24
- f.readline if options[:skip_first_row]
25
-
26
- [:logger, :skip_first_row].each {|d| options.delete(d) }
27
-
28
- line_number = 0
29
-
30
- begin
31
- while (line = f.readline)
32
- begin
33
- line_number += 1
34
-
35
- row = CSV.parse(line, options)[0]
36
- row.extend Loady::Array
37
-
38
- unless row.empty?
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
- rescue EOFError
48
- f.close
49
- rescue Exception => message
50
- warning += 1
51
- line_number += 1
52
- logger.error "#{message.to_s + " - at line #{line_number}"}\n#{line}"
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
@@ -2,7 +2,7 @@ module Loady
2
2
  class MemoryLogger
3
3
  attr_reader :messages
4
4
 
5
- def initialize
5
+ def initialize(options={})
6
6
  @messages = []
7
7
  end
8
8
 
@@ -13,4 +13,4 @@ module Loady
13
13
  alias_method :warn, :info
14
14
  alias_method :error, :info
15
15
  end
16
- end
16
+ end
data/lib/loady/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Loady
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
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.0
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-03 00:00:00.000000000 Z
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: 3713324824844609253
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: 3713324824844609253
110
+ hash: -1716122112452948356
111
111
  requirements: []
112
112
  rubyforge_project: loady
113
113
  rubygems_version: 1.8.24