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 CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/csv_lazy.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "csv_lazy"
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
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
- return row
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
 
@@ -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.3
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: -779445160721638785
114
+ hash: 222799097840594694
115
115
  required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements: