csv_lazy 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/csv_lazy.gemspec +1 -1
- data/lib/csv_lazy.rb +22 -3
- data/spec/csv_lazy_spec.rb +26 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/csv_lazy.gemspec
CHANGED
data/lib/csv_lazy.rb
CHANGED
@@ -12,7 +12,8 @@ class Csv_lazy
|
|
12
12
|
@args = {
|
13
13
|
:quote_char => '"',
|
14
14
|
:row_sep => "\n",
|
15
|
-
:col_sep => ";"
|
15
|
+
:col_sep => ";",
|
16
|
+
:headers => false
|
16
17
|
}.merge(args)
|
17
18
|
|
18
19
|
@io = @args[:io]
|
@@ -23,7 +24,7 @@ class Csv_lazy
|
|
23
24
|
@mutex = Mutex.new
|
24
25
|
#@debug = true
|
25
26
|
|
26
|
-
accepted = [:encode, :quote_char, :row_sep, :col_sep, :io, :debug]
|
27
|
+
accepted = [:encode, :quote_char, :row_sep, :col_sep, :io, :debug, :headers]
|
27
28
|
@args.each do |key, val|
|
28
29
|
if accepted.index(key) == nil
|
29
30
|
raise "Unknown argument: '#{key}'."
|
@@ -45,6 +46,15 @@ class Csv_lazy
|
|
45
46
|
@regex_read_until_row_sep = /\A(.+?)#{Regexp.escape(@args[:row_sep])}/
|
46
47
|
@regex_read_until_end = /\A(.+?)\Z/
|
47
48
|
|
49
|
+
if @args[:headers]
|
50
|
+
headers = []
|
51
|
+
read_row.each do |key|
|
52
|
+
headers << key.to_sym
|
53
|
+
end
|
54
|
+
|
55
|
+
@headers = headers
|
56
|
+
end
|
57
|
+
|
48
58
|
self.each(&blk) if blk
|
49
59
|
end
|
50
60
|
|
@@ -86,7 +96,16 @@ class Csv_lazy
|
|
86
96
|
if row.empty?
|
87
97
|
return false
|
88
98
|
else
|
89
|
-
|
99
|
+
if @headers
|
100
|
+
ret = {}
|
101
|
+
row.length.times do |count|
|
102
|
+
ret[@headers[count]] = row[count]
|
103
|
+
end
|
104
|
+
|
105
|
+
return ret
|
106
|
+
else
|
107
|
+
return row
|
108
|
+
end
|
90
109
|
end
|
91
110
|
end
|
92
111
|
|
data/spec/csv_lazy_spec.rb
CHANGED
@@ -83,4 +83,30 @@ describe "CsvLazy" do
|
|
83
83
|
|
84
84
|
lines_found.should eql(1)
|
85
85
|
end
|
86
|
+
|
87
|
+
it "should be able to use headers and return hashes instead" do
|
88
|
+
cont = "\"name\",age\r\n"
|
89
|
+
cont += "\"Kasper Johansen\",27\r\n"
|
90
|
+
cont += "\"Christina Stoeckel\",\"25\"\r\n"
|
91
|
+
|
92
|
+
line = 0
|
93
|
+
Csv_lazy.new(:col_sep => ",", :io => StringIO.new(cont), :headers => true, :row_sep => "\r\n") do |csv|
|
94
|
+
csv.class.should eql(Hash)
|
95
|
+
line += 1
|
96
|
+
csv.keys.length.should eql(2)
|
97
|
+
csv.length.should eql(2)
|
98
|
+
|
99
|
+
if line == 1
|
100
|
+
csv[:name].should eql("Kasper Johansen")
|
101
|
+
csv[:age].should eql("27")
|
102
|
+
elsif line == 2
|
103
|
+
csv[:name].should eql("Christina Stoeckel")
|
104
|
+
csv[:age].should eql("25")
|
105
|
+
else
|
106
|
+
raise "Wrong line: #{line}"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
line.should eql(2)
|
111
|
+
end
|
86
112
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_lazy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -111,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
111
111
|
version: '0'
|
112
112
|
segments:
|
113
113
|
- 0
|
114
|
-
hash:
|
114
|
+
hash: 222799097840594694
|
115
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|