loady 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +8 -4
- data/lib/loady.rb +2 -27
- data/lib/loady/array.rb +27 -0
- data/lib/loady/version.rb +1 -1
- data/test/test_csv_loader.rb +6 -3
- data/test/test_loady.rb +13 -4
- metadata +3 -2
data/README.rdoc
CHANGED
@@ -27,7 +27,7 @@ Basic usage:
|
|
27
27
|
|
28
28
|
Skip first row and log to a file:
|
29
29
|
|
30
|
-
logger = Logger.new
|
30
|
+
logger = Logger.new "/your/file.log"
|
31
31
|
|
32
32
|
Loady::CsvLoader.read "/your/file.csv", :logger => logger, :skip_first_row => true do |row|
|
33
33
|
# do some stuff for each row
|
@@ -35,9 +35,8 @@ Skip first row and log to a file:
|
|
35
35
|
|
36
36
|
Name your attributes:
|
37
37
|
|
38
|
-
Loady::CsvLoader.read "/your/file.csv"
|
39
|
-
|
40
|
-
Monkey.create attrs
|
38
|
+
Loady::CsvLoader.read "/your/file.csv" do |row|
|
39
|
+
Monkey.create row.to_attributes [:name, :year, :mom]
|
41
40
|
end
|
42
41
|
|
43
42
|
|
@@ -50,3 +49,8 @@ Fork away. Please create a topic branch and write passing tests if you are submi
|
|
50
49
|
bundle install
|
51
50
|
rake test
|
52
51
|
git checkout -b your_fix
|
52
|
+
|
53
|
+
|
54
|
+
== ToDo
|
55
|
+
|
56
|
+
Read attribute names from header row
|
data/lib/loady.rb
CHANGED
@@ -1,27 +1,2 @@
|
|
1
|
-
require 'loady/
|
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
|
1
|
+
require 'loady/array'
|
2
|
+
require 'loady/csv_loader'
|
data/lib/loady/array.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
# usage:
|
4
|
+
# ['john', 'doe'].to_attributes([:first, :last]) => { :first => 'john', :last => 'doe' }
|
5
|
+
#
|
6
|
+
# options:
|
7
|
+
# :strip => true -- default = false
|
8
|
+
# -- array values must be strings if :strip is true
|
9
|
+
def to_attributes(names, options={})
|
10
|
+
options = options.merge!(:strip => true){|_,o| o }
|
11
|
+
|
12
|
+
h = {}
|
13
|
+
|
14
|
+
names.each_with_index do |name, i|
|
15
|
+
if i < self.size
|
16
|
+
if options[:strip]
|
17
|
+
h[name] = self[i].strip
|
18
|
+
else
|
19
|
+
h[name] = self[i]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
h
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/lib/loady/version.rb
CHANGED
data/test/test_csv_loader.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require File.expand_path('test_helper.rb', File.dirname(__FILE__))
|
2
2
|
|
3
3
|
class CsvLoaderTest < Test::Unit::TestCase
|
4
|
+
|
4
5
|
context "Simple class" do
|
6
|
+
|
5
7
|
should "read file1" do
|
6
8
|
monkeys = []
|
7
9
|
|
@@ -16,12 +18,12 @@ class CsvLoaderTest < Test::Unit::TestCase
|
|
16
18
|
assert_equal monkeys[9][:year], "1933", "last row year"
|
17
19
|
end
|
18
20
|
|
19
|
-
should "read file2" do
|
21
|
+
should "read file2 with logger using named attributes" do
|
20
22
|
logger = Logger.new("/dev/null")
|
21
23
|
monkeys = []
|
22
24
|
|
23
25
|
Loady::CsvLoader.read "test/csv/file2.csv", :logger => logger do |row|
|
24
|
-
monkeys <<
|
26
|
+
monkeys << row.to_attributes([:name, :year])
|
25
27
|
end
|
26
28
|
|
27
29
|
assert_equal monkeys.count, 10, "total rows read"
|
@@ -29,7 +31,8 @@ class CsvLoaderTest < Test::Unit::TestCase
|
|
29
31
|
assert_equal monkeys[0][:year], "2000", "first row year"
|
30
32
|
assert_equal monkeys[9][:name], "King Kong", "last row name"
|
31
33
|
assert_equal monkeys[9][:year], "1933", "last row year"
|
32
|
-
|
34
|
+
end
|
33
35
|
|
34
36
|
end
|
37
|
+
|
35
38
|
end
|
data/test/test_loady.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
require File.expand_path('test_helper.rb', File.dirname(__FILE__))
|
2
2
|
|
3
|
-
class
|
4
|
-
context "
|
3
|
+
class LoadyTest < Test::Unit::TestCase
|
4
|
+
context "array" do
|
5
5
|
|
6
|
-
should "
|
6
|
+
should "return named attributes" do
|
7
7
|
row = ['Bubbles ', '2000', ' King Kong ']
|
8
|
-
attrs =
|
8
|
+
attrs = row.to_attributes [:name, :year, :mom]
|
9
9
|
|
10
10
|
assert_equal attrs.size, 3
|
11
11
|
assert_equal attrs[:name], 'Bubbles'
|
@@ -13,5 +13,14 @@ class CsvLoaderTest < Test::Unit::TestCase
|
|
13
13
|
assert_equal attrs[:mom], 'King Kong'
|
14
14
|
end
|
15
15
|
|
16
|
+
should "return named attributes when missing values" do
|
17
|
+
row = ['Bubbles ', '2000']
|
18
|
+
attrs = row.to_attributes [:name, :year, :mom]
|
19
|
+
|
20
|
+
assert_equal attrs.size, 2
|
21
|
+
assert_equal attrs[:name], 'Bubbles'
|
22
|
+
assert_equal attrs[:year], '2000'
|
23
|
+
end
|
24
|
+
|
16
25
|
end
|
17
26
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: loady
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Tee Parham
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-15 00:00:00 -06:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -51,6 +51,7 @@ files:
|
|
51
51
|
- README.rdoc
|
52
52
|
- Rakefile
|
53
53
|
- lib/loady.rb
|
54
|
+
- lib/loady/array.rb
|
54
55
|
- lib/loady/csv_loader.rb
|
55
56
|
- lib/loady/version.rb
|
56
57
|
- loady.gemspec
|