loady 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +13 -2
- data/lib/loady.rb +26 -0
- data/lib/loady/csv_loader.rb +2 -2
- data/lib/loady/version.rb +1 -1
- data/test/test_csv_loader.rb +11 -14
- data/test/test_loady.rb +17 -0
- metadata +6 -16
data/README.rdoc
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Loady is a simple file reader and logger. Use it to quickly load a csv file, continue on error rows, and do basic logging.
|
4
4
|
|
5
|
+
Currently it only works with ruby 1.9.
|
6
|
+
|
5
7
|
== Install
|
6
8
|
|
7
9
|
gem install loady
|
@@ -19,7 +21,8 @@ By default, messages are logged to the standard output.
|
|
19
21
|
Basic usage:
|
20
22
|
|
21
23
|
Loady::CsvLoader.read "/your/file.csv" do |row|
|
22
|
-
|
24
|
+
# your code to process each row goes here
|
25
|
+
puts "#{row[0]}, #{row[1]}, etc."
|
23
26
|
end
|
24
27
|
|
25
28
|
Skip first row and log to a file:
|
@@ -27,9 +30,17 @@ Skip first row and log to a file:
|
|
27
30
|
logger = Logger.new("/your/file.log")
|
28
31
|
|
29
32
|
Loady::CsvLoader.read "/your/file.csv", :logger => logger, :skip_first_row => true do |row|
|
30
|
-
# do some stuff
|
33
|
+
# do some stuff for each row
|
34
|
+
end
|
35
|
+
|
36
|
+
Name your attributes:
|
37
|
+
|
38
|
+
Loady::CsvLoader.read "/your/file.csv" do |row|
|
39
|
+
attrs = Loady.named_attribute_hash row, [:name, :year, :mom]
|
40
|
+
Monkey.create attrs
|
31
41
|
end
|
32
42
|
|
43
|
+
|
33
44
|
== Development
|
34
45
|
|
35
46
|
Fork away. Please create a topic branch and write passing tests if you are submitting a pull request.
|
data/lib/loady.rb
CHANGED
@@ -1 +1,27 @@
|
|
1
1
|
require 'loady/csv_loader.rb'
|
2
|
+
|
3
|
+
module Loady
|
4
|
+
|
5
|
+
# Loady::named_attribute_hash row, [:name, :age, :email]
|
6
|
+
# => { :name => 'Bob', :age => '21', :email => 'bob@example.com' }
|
7
|
+
#
|
8
|
+
# options:
|
9
|
+
# :strip => false -- default = true -- strip each row value
|
10
|
+
def named_attribute_hash(row, attrs, options={})
|
11
|
+
options = options.merge!(:strip => true){|k,o,n| o }
|
12
|
+
|
13
|
+
h = {}
|
14
|
+
|
15
|
+
attrs.each_with_index do |attr, i|
|
16
|
+
if options[:strip]
|
17
|
+
h[attr] = row[i].strip
|
18
|
+
else
|
19
|
+
h[attr] = row[i]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
h
|
24
|
+
end
|
25
|
+
module_function :named_attribute_hash
|
26
|
+
|
27
|
+
end
|
data/lib/loady/csv_loader.rb
CHANGED
@@ -7,7 +7,7 @@ module Loady
|
|
7
7
|
# options:
|
8
8
|
# :skip_first_row => true -- default = false
|
9
9
|
# :logger => Logger.new('/somewhere/file.log') -- default = Logger.new(STDOUT)
|
10
|
-
def self.read(filename, options
|
10
|
+
def self.read(filename, options={})
|
11
11
|
success = 0
|
12
12
|
warning = 0
|
13
13
|
|
@@ -55,6 +55,6 @@ module Loady
|
|
55
55
|
logger.fatal "Unhandled error: \n#{message}\n"
|
56
56
|
end
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
end
|
60
60
|
end
|
data/lib/loady/version.rb
CHANGED
data/test/test_csv_loader.rb
CHANGED
@@ -2,9 +2,6 @@ require File.expand_path('test_helper.rb', File.dirname(__FILE__))
|
|
2
2
|
|
3
3
|
class CsvLoaderTest < Test::Unit::TestCase
|
4
4
|
context "Simple class" do
|
5
|
-
setup do
|
6
|
-
end
|
7
|
-
|
8
5
|
should "read file1" do
|
9
6
|
monkeys = []
|
10
7
|
|
@@ -12,11 +9,11 @@ class CsvLoaderTest < Test::Unit::TestCase
|
|
12
9
|
monkeys << { :name => row[0], :year => row[1] }
|
13
10
|
end
|
14
11
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
assert_equal monkeys.count, 10, "total rows read"
|
13
|
+
assert_equal monkeys[0][:name], "Bubbles", "first row name"
|
14
|
+
assert_equal monkeys[0][:year], "2000", "first row year"
|
15
|
+
assert_equal monkeys[9][:name], "King Kong", "last row name"
|
16
|
+
assert_equal monkeys[9][:year], "1933", "last row year"
|
20
17
|
end
|
21
18
|
|
22
19
|
should "read file2" do
|
@@ -27,12 +24,12 @@ class CsvLoaderTest < Test::Unit::TestCase
|
|
27
24
|
monkeys << { :name => row[0], :year => row[1] }
|
28
25
|
end
|
29
26
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
27
|
+
assert_equal monkeys.count, 10, "total rows read"
|
28
|
+
assert_equal monkeys[0][:name], "Bubbles", "first row name"
|
29
|
+
assert_equal monkeys[0][:year], "2000", "first row year"
|
30
|
+
assert_equal monkeys[9][:name], "King Kong", "last row name"
|
31
|
+
assert_equal monkeys[9][:year], "1933", "last row year"
|
35
32
|
end
|
36
|
-
|
33
|
+
|
37
34
|
end
|
38
35
|
end
|
data/test/test_loady.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path('test_helper.rb', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
class CsvLoaderTest < Test::Unit::TestCase
|
4
|
+
context "helper" do
|
5
|
+
|
6
|
+
should "build named_attribute_hash" do
|
7
|
+
row = ['Bubbles ', '2000', ' King Kong ']
|
8
|
+
attrs = Loady.named_attribute_hash row, [:name, :year, :mom]
|
9
|
+
|
10
|
+
assert_equal attrs.size, 3
|
11
|
+
assert_equal attrs[:name], 'Bubbles'
|
12
|
+
assert_equal attrs[:year], '2000'
|
13
|
+
assert_equal attrs[:mom], 'King Kong'
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loady
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
version: 0.0.1
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Tee Parham
|
@@ -14,7 +10,7 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date: 2011-
|
13
|
+
date: 2011-04-14 00:00:00 -06:00
|
18
14
|
default_executable:
|
19
15
|
dependencies:
|
20
16
|
- !ruby/object:Gem::Dependency
|
@@ -25,8 +21,6 @@ dependencies:
|
|
25
21
|
requirements:
|
26
22
|
- - ">="
|
27
23
|
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 0
|
30
24
|
version: "0"
|
31
25
|
type: :development
|
32
26
|
version_requirements: *id001
|
@@ -38,8 +32,6 @@ dependencies:
|
|
38
32
|
requirements:
|
39
33
|
- - ">="
|
40
34
|
- !ruby/object:Gem::Version
|
41
|
-
segments:
|
42
|
-
- 0
|
43
35
|
version: "0"
|
44
36
|
type: :development
|
45
37
|
version_requirements: *id002
|
@@ -66,6 +58,7 @@ files:
|
|
66
58
|
- test/csv/file2.csv
|
67
59
|
- test/test_csv_loader.rb
|
68
60
|
- test/test_helper.rb
|
61
|
+
- test/test_loady.rb
|
69
62
|
has_rdoc: true
|
70
63
|
homepage: ""
|
71
64
|
licenses: []
|
@@ -80,21 +73,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
80
73
|
requirements:
|
81
74
|
- - ">="
|
82
75
|
- !ruby/object:Gem::Version
|
83
|
-
segments:
|
84
|
-
- 0
|
85
76
|
version: "0"
|
86
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
78
|
none: false
|
88
79
|
requirements:
|
89
80
|
- - ">="
|
90
81
|
- !ruby/object:Gem::Version
|
91
|
-
segments:
|
92
|
-
- 0
|
93
82
|
version: "0"
|
94
83
|
requirements: []
|
95
84
|
|
96
85
|
rubyforge_project: loady
|
97
|
-
rubygems_version: 1.
|
86
|
+
rubygems_version: 1.6.2
|
98
87
|
signing_key:
|
99
88
|
specification_version: 3
|
100
89
|
summary: CSV file loader with simple logging
|
@@ -103,3 +92,4 @@ test_files:
|
|
103
92
|
- test/csv/file2.csv
|
104
93
|
- test/test_csv_loader.rb
|
105
94
|
- test/test_helper.rb
|
95
|
+
- test/test_loady.rb
|