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 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
@@ -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
- begin
22
- f = File.new(filename)
23
- f.readline if options[:skip_first_row]
24
-
25
- line_number = 0
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
- begin
28
- while (line = f.readline)
29
- begin
30
- line_number += 1
30
+ begin
31
+ while (line = f.readline)
32
+ begin
33
+ line_number += 1
31
34
 
32
- row = CSV.parse(line)[0]
35
+ row = CSV.parse(line, options)[0]
33
36
 
34
- unless row.empty?
35
- yield row
37
+ unless row.empty?
38
+ yield row
36
39
 
37
- success += 1
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
- logger.info "Finished. Loaded #{success} rows. #{warning} unprocessed rows."
53
-
47
+ rescue EOFError
48
+ f.close
54
49
  rescue Exception => message
55
- logger.fatal "Unhandled error: \n#{message}\n"
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
@@ -1,3 +1,3 @@
1
1
  module Loady
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,11 @@
1
+ monkey name best year
2
+ Bubbles 2000
3
+ Grape Ape 1975
4
+ Albert 1948
5
+ Koko 1971
6
+ Curious George 1941
7
+ Clyde 1975
8
+ Bear 1980
9
+ Mighty Joe Young 1998
10
+ Donkey Kong 1981
11
+ King Kong 1933
@@ -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.1.1
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-04-18 00:00:00 -06:00
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.6.2
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