Class: Tb::FileEnumerator::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/tb/fileenumerator.rb

Instance Method Summary collapse

Constructor Details

#initialize(fileenumerator) ⇒ Reader

Returns a new instance of Reader.



147
148
149
150
151
152
153
# File 'lib/tb/fileenumerator.rb', line 147

def initialize(fileenumerator)
  @use_count = 0
  @fileenumerator = fileenumerator
  @fileenumerator.send(:use_count_up)
  @io = @fileenumerator.instance_eval { @open_func.call }
  peek_reset
end

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/tb/fileenumerator.rb', line 155

def closed?
  @io.nil?
end

#nextObject



241
242
243
244
245
# File 'lib/tb/fileenumerator.rb', line 241

def next
  result = self.peek
  peek_reset
  result
end

#next_valuesObject



235
236
237
238
239
# File 'lib/tb/fileenumerator.rb', line 235

def next_values
  result = self.peek_values
  peek_reset
  result
end

#peekObject



227
228
229
230
231
232
233
# File 'lib/tb/fileenumerator.rb', line 227

def peek
  result = self.peek_values
  if result.kind_of?(Array) && result.length == 1
    result = result[0]
  end
  result
end

#peek_valuesObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/tb/fileenumerator.rb', line 208

def peek_values
  if !@io
    raise StopIteration
  end
  if !@peeked
    begin
      objs = Marshal.load(@io)
    rescue EOFError
      if @use_count == 0
        finalize
      end
      raise StopIteration
    end
    @peeked = true
    @peeked_objs = objs
  end
  @peeked_objs
end

#posObject



198
199
200
# File 'lib/tb/fileenumerator.rb', line 198

def pos
  @pos
end

#pos=(val) ⇒ Object



202
203
204
205
206
# File 'lib/tb/fileenumerator.rb', line 202

def pos=(val)
  @io.seek(val)
  peek_reset
  nil
end

#rewindObject



247
248
249
250
251
252
253
254
# File 'lib/tb/fileenumerator.rb', line 247

def rewind
  if !@io
    raise ArgumentError, "already closed."
  end
  @io.rewind
  peek_reset
  nil
end

#subeach_by(&distinguish_value) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/tb/fileenumerator.rb', line 256

def subeach_by(&distinguish_value)
  Enumerator.new {|y|
    begin
      vs = self.peek_values
    rescue StopIteration
      next
    end
    dv = distinguish_value.call(*vs)
    while true
      y.yield(*vs)
      self.next_values
      begin
        next_vs = self.peek_values
      rescue StopIteration
        break
      end
      next_dv = distinguish_value.call(*next_vs)
      if dv != next_dv
        break
      end
      vs = next_vs
      dv = next_dv
    end
    nil
  }
end

#useObject



178
179
180
181
182
183
184
185
# File 'lib/tb/fileenumerator.rb', line 178

def use
  use_begin
  begin
    yield
  ensure
    use_end
  end
end

#use_beginObject



167
168
169
# File 'lib/tb/fileenumerator.rb', line 167

def use_begin
  @use_count += 1
end

#use_endObject



171
172
173
174
175
176
# File 'lib/tb/fileenumerator.rb', line 171

def use_end
  @use_count -= 1
  if @use_count == 0
    finalize
  end
end