Class Bcat::Reader
In: lib/bcat/reader.rb
Parent: Object

ARGF style multi-file streaming interface. Input is read with IO#readpartial to avoid buffering.

Methods

each   new   open   open_command   open_files   sniff  

Attributes

args  [R] 
fds  [R] 
is_command  [R] 

Public Class methods

[Source]

    # File lib/bcat/reader.rb, line 11
11:     def initialize(is_command, args=[])
12:       @is_command = is_command
13:       @args = args
14:     end

Public Instance methods

[Source]

    # File lib/bcat/reader.rb, line 35
35:     def each
36:       yield @buf.shift while @buf.any?
37:       while fd = fds.first
38:         fds.shift and next if fd.closed?
39:         fd.sync = true
40:         begin
41:           while buf = fd.readpartial(4096)
42:             yield buf
43:           end
44:         rescue EOFError
45:           fd.close
46:         end
47:         fds.shift
48:       end
49:     end

[Source]

    # File lib/bcat/reader.rb, line 16
16:     def open
17:       @fds = is_command ? open_command : open_files
18:       @buf = []
19:     end

[Source]

    # File lib/bcat/reader.rb, line 21
21:     def open_command
22:       [IO.popen(args.join(' '), 'rb')]
23:     end

[Source]

    # File lib/bcat/reader.rb, line 25
25:     def open_files
26:       args.map do |f|
27:         if f == '-'
28:           $stdin
29:         else
30:           File.open(f, 'rb')
31:         end
32:       end
33:     end

[Source]

    # File lib/bcat/reader.rb, line 51
51:     def sniff
52:       @format ||=
53:         catch :detect do
54:           each do |chunk|
55:             @buf << chunk
56:             case chunk
57:             when /\A\s*</m
58:               throw :detect, 'html'
59:             when /\A\s*[^<]/m
60:               throw :detect, 'text'
61:             end
62:           end
63:           throw :detect, 'text'
64:         end
65:     end

[Validate]