loady 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +0 -15
- data/lib/loady/csv_loader.rb +27 -28
- data/lib/loady/version.rb +1 -1
- data/test/csv/file3.dat +11 -0
- data/test/test_csv_loader.rb +14 -0
- metadata +5 -5
data/README.rdoc
CHANGED
@@ -39,18 +39,3 @@ Name your attributes:
|
|
39
39
|
Monkey.create row.to_attributes [:name, :year, :mom]
|
40
40
|
end
|
41
41
|
|
42
|
-
|
43
|
-
== Development
|
44
|
-
|
45
|
-
Fork away. Please create a topic branch and write passing tests if you are submitting a pull request.
|
46
|
-
|
47
|
-
git clone git://github.com/[yourfork]/loady.git
|
48
|
-
cd loady
|
49
|
-
bundle install
|
50
|
-
rake test
|
51
|
-
git checkout -b your_fix
|
52
|
-
|
53
|
-
|
54
|
-
== ToDo
|
55
|
-
|
56
|
-
Read attribute names from header row
|
data/lib/loady/csv_loader.rb
CHANGED
@@ -4,9 +4,11 @@ require 'logger'
|
|
4
4
|
module Loady
|
5
5
|
class CsvLoader
|
6
6
|
|
7
|
-
# options:
|
7
|
+
# valid options:
|
8
8
|
# :skip_first_row => true -- default = false
|
9
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
|
10
12
|
def self.read(filename, options={})
|
11
13
|
success = 0
|
12
14
|
warning = 0
|
@@ -18,42 +20,39 @@ module Loady
|
|
18
20
|
logger.datetime_format = "%H:%M:%S"
|
19
21
|
end
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
begin
|
31
|
+
while (line = f.readline)
|
32
|
+
begin
|
33
|
+
line_number += 1
|
31
34
|
|
32
|
-
|
35
|
+
row = CSV.parse(line, options)[0]
|
33
36
|
|
34
|
-
|
35
|
-
|
37
|
+
unless row.empty?
|
38
|
+
yield row
|
36
39
|
|
37
|
-
|
38
|
-
end
|
39
|
-
rescue Exception => message
|
40
|
-
warning += 1
|
41
|
-
logger.warn "#{message.to_s.gsub("line 1", "line #{line_number}")}\n#{line}"
|
40
|
+
success += 1
|
42
41
|
end
|
42
|
+
rescue Exception => message
|
43
|
+
warning += 1
|
44
|
+
logger.warn "#{message.to_s.gsub("line 1", "line #{line_number}")}\n#{line}"
|
43
45
|
end
|
44
|
-
rescue EOFError
|
45
|
-
f.close
|
46
|
-
rescue Exception => message
|
47
|
-
warning += 1
|
48
|
-
line_number += 1
|
49
|
-
logger.error "#{message.to_s + " - at line #{line_number}"}\n#{line}"
|
50
46
|
end
|
51
|
-
|
52
|
-
|
53
|
-
|
47
|
+
rescue EOFError
|
48
|
+
f.close
|
54
49
|
rescue Exception => message
|
55
|
-
|
50
|
+
warning += 1
|
51
|
+
line_number += 1
|
52
|
+
logger.error "#{message.to_s + " - at line #{line_number}"}\n#{line}"
|
56
53
|
end
|
54
|
+
|
55
|
+
logger.info "Finished. Loaded #{success} rows. #{warning} unprocessed rows."
|
57
56
|
end
|
58
57
|
|
59
58
|
end
|
data/lib/loady/version.rb
CHANGED
data/test/csv/file3.dat
ADDED
data/test/test_csv_loader.rb
CHANGED
@@ -32,6 +32,20 @@ class CsvLoaderTest < Test::Unit::TestCase
|
|
32
32
|
assert_equal monkeys[9][:name], "King Kong", "last row name"
|
33
33
|
assert_equal monkeys[9][:year], "1933", "last row year"
|
34
34
|
end
|
35
|
+
|
36
|
+
should "read file3, a tab-delimited file" do
|
37
|
+
monkeys = []
|
38
|
+
|
39
|
+
Loady.csv "test/csv/file3.dat", :skip_first_row => true, :col_sep => "\t" do |row|
|
40
|
+
monkeys << { :name => row[0], :year => row[1] }
|
41
|
+
end
|
42
|
+
|
43
|
+
assert_equal monkeys.count, 10, "total rows read"
|
44
|
+
assert_equal monkeys[0][:name], "Bubbles", "first row name"
|
45
|
+
assert_equal monkeys[0][:year], "2000", "first row year"
|
46
|
+
assert_equal monkeys[9][:name], "King Kong", "last row name"
|
47
|
+
assert_equal monkeys[9][:year], "1933", "last row year"
|
48
|
+
end
|
35
49
|
|
36
50
|
end
|
37
51
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: loady
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Tee Parham
|
@@ -10,8 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
14
|
-
default_executable:
|
13
|
+
date: 2011-06-15 00:00:00 Z
|
15
14
|
dependencies:
|
16
15
|
- !ruby/object:Gem::Dependency
|
17
16
|
name: test-unit
|
@@ -57,10 +56,10 @@ files:
|
|
57
56
|
- loady.gemspec
|
58
57
|
- test/csv/file1.csv
|
59
58
|
- test/csv/file2.csv
|
59
|
+
- test/csv/file3.dat
|
60
60
|
- test/test_csv_loader.rb
|
61
61
|
- test/test_helper.rb
|
62
62
|
- test/test_loady.rb
|
63
|
-
has_rdoc: true
|
64
63
|
homepage: http://github.com/stackpilot/loady
|
65
64
|
licenses: []
|
66
65
|
|
@@ -84,13 +83,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
83
|
requirements: []
|
85
84
|
|
86
85
|
rubyforge_project: loady
|
87
|
-
rubygems_version: 1.
|
86
|
+
rubygems_version: 1.7.2
|
88
87
|
signing_key:
|
89
88
|
specification_version: 3
|
90
89
|
summary: CSV file loader with simple logging
|
91
90
|
test_files:
|
92
91
|
- test/csv/file1.csv
|
93
92
|
- test/csv/file2.csv
|
93
|
+
- test/csv/file3.dat
|
94
94
|
- test/test_csv_loader.rb
|
95
95
|
- test/test_helper.rb
|
96
96
|
- test/test_loady.rb
|