Class: Editeur::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/editeur/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, sep = "\n") ⇒ Reader

Returns a new instance of Reader.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/editeur/reader.rb', line 5

def initialize(path, sep="\n")
  if not File.exists? path
    return nil
  end

  @handle = File.new(path)
  @sep = sep
  @buf = ''
  @buf_size = 1024
  @lines = []
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



4
5
6
# File 'lib/editeur/reader.rb', line 4

def lines
  @lines
end

Instance Method Details

#eachObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/editeur/reader.rb', line 42

def each
  while true
    line = read
    if not line
      break
    end
    yield line
  end
  nil
end

#readObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/editeur/reader.rb', line 17

def read
  if @lines.length > 1
    return @lines.shift
  end 

  head = @lines.shift
  buf = @handle.read @buf_size 
  
  if (not head) and (not buf)
    return nil
  end

  head ||= ''
  if buf
    buf.gsub!("\n", ' ')
    chunks = buf.split @sep
    head += (chunks.shift)
    for chunk in chunks
      @lines << chunk
    end 
  end

  head
end