csv_lazy 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/csv_lazy.gemspec +1 -1
  3. data/lib/csv_lazy.rb +24 -15
  4. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
data/csv_lazy.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{csv_lazy}
8
- s.version = "0.0.0"
8
+ s.version = "0.0.1"
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
@@ -1,7 +1,7 @@
1
1
  class Csv_lazy
2
2
  include Enumerable
3
3
 
4
- def initialize(args, &blk)
4
+ def initialize(args = {}, &blk)
5
5
  @args = {
6
6
  :quote_char => '"',
7
7
  :row_sep => "\n",
@@ -12,9 +12,11 @@ class Csv_lazy
12
12
  @eof = false
13
13
  @buffer = ""
14
14
  @debug = @args[:debug]
15
+ @encode = @args[:encode]
16
+ @mutex = Mutex.new
15
17
  #@debug = true
16
18
 
17
- accepted = [:quote_char, :row_sep, :col_sep, :io]
19
+ accepted = [:encode, :quote_char, :row_sep, :col_sep, :io]
18
20
  @args.each do |key, val|
19
21
  if accepted.index(key) == nil
20
22
  raise "Unknown argument: '#{key}'."
@@ -41,8 +43,10 @@ class Csv_lazy
41
43
 
42
44
  #Yields each row as an array.
43
45
  def each
44
- while row = read_row
45
- yield(row)
46
+ @mutex.synchronize do
47
+ while row = read_row
48
+ yield(row)
49
+ end
46
50
  end
47
51
  end
48
52
 
@@ -50,10 +54,12 @@ class Csv_lazy
50
54
 
51
55
  #Reads more content into the buffer.
52
56
  def read_buffer
53
- read = @io.read(4096)
57
+ read = @io.gets
58
+
54
59
  if !read
55
60
  @eof = true
56
61
  else
62
+ read = read.encode(@encode) if @encode
57
63
  @buffer << read
58
64
  end
59
65
  end
@@ -113,7 +119,7 @@ class Csv_lazy
113
119
  if !match_read
114
120
  read_buffer
115
121
  else
116
- @row << match_read[1]
122
+ add_col(match_read[1])
117
123
  break
118
124
  end
119
125
  end
@@ -132,29 +138,32 @@ class Csv_lazy
132
138
  raise "Dont know what to do (#{@buffer.length}): #{@buffer}"
133
139
  end
134
140
  elsif match = read_remove_regex(@regex_read_until_col_sep)
135
- @row << match[1]
141
+ add_col(match[1])
136
142
  return true
137
143
  elsif match = read_remove_regex(@regex_read_until_row_sep)
138
144
  puts "csv_lazy: Row seperator reached." if @debug
139
- @row << match[1]
145
+ add_col(match[1])
140
146
  return false
141
147
  elsif match = read_remove_regex(@regex_read_until_end)
148
+ #If the very end of the file has been reached, then add this data and stop parsing.
142
149
  if @eof
143
- @row << match[1]
150
+ add_col(match[1])
144
151
  return false
145
152
  end
146
153
 
154
+ #The end-of-file hasnt been reached. Add more data to buffer and try again.
147
155
  @buffer << match[0]
148
156
  read_buffer
149
- raise Csv_lazy::Retry
157
+ raise Errno::EAGAIN
150
158
  else
151
159
  raise "Dont know what to do with buffer: #{@buffer}"
152
160
  end
153
- rescue Csv_lazy::Retry
161
+ rescue Errno::EAGAIN
154
162
  retry
155
163
  end
156
- end
157
-
158
- class Csv_lazy::Retry < RuntimeError
159
-
164
+
165
+ #Adds a new column to the current row.
166
+ def add_col(str)
167
+ @row << str
168
+ end
160
169
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: csv_lazy
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.0
5
+ version: 0.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -94,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - ">="
96
96
  - !ruby/object:Gem::Version
97
- hash: 4190482346075524299
97
+ hash: 2827273816324907749
98
98
  segments:
99
99
  - 0
100
100
  version: "0"