Class: Tb::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/tb/ropen.rb,
lib/tb/reader.rb

Overview

lib/tb/reader.rb - Tb::Reader class

Copyright © 2011 Tanaka Akira <[email protected]>

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rawreader, opts = {}) ⇒ Reader

Returns a new instance of Reader.



28
29
30
31
32
33
# File 'lib/tb/reader.rb', line 28

def initialize(rawreader, opts={})
  @opt_n = opts[:numeric]
  @opt_close = opts[:close]
  @reader = rawreader
  @fieldset = nil
end

Class Method Details

.open(filename, opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tb/ropen.rb', line 28

def self.open(filename, opts={})
  opts = opts.dup
  case filename
  when /\Acsv:/
    filename = $'
    rawreader_maker = lambda {|io| Tb::CSVReader.new(io) }
  when /\Atsv:/
    filename = $'
    rawreader_maker = lambda {|io| Tb::TSVReader.new(io) }
  when /\Ap[pgbn]m:/
    filename = $'
    rawreader_maker = lambda {|io| Tb.pnm_stream_input(io) }
  when /\.csv\z/
    rawreader_maker = lambda {|io| Tb::CSVReader.new(io) }
  when /\.tsv\z/
    rawreader_maker = lambda {|io| Tb::TSVReader.new(io) }
  when /\.p[pgbn]m\z/
    rawreader_maker = lambda {|io| Tb.pnm_stream_input(io) }
  else
    rawreader_maker = lambda {|io| Tb::CSVReader.new(io) }
  end
  unless filename.respond_to? :to_str
    raise ArgumentError, "unexpected filename: #{filename.inspect}"
  end
  if filename == '-'
    rawreader = rawreader_maker.call(STDIN)
  else
    io = File.open(filename)
    opts[:close] = io
    rawreader = rawreader_maker.call(io)
  end
  reader = self.new(rawreader, opts)
  if block_given?
    begin
      yield reader
    ensure
      reader.close
    end
  else
    reader
  end
end

Instance Method Details

#closeObject



97
98
99
100
101
102
# File 'lib/tb/reader.rb', line 97

def close
  @reader.close
  if @opt_close
    @opt_close.close
  end
end

#eachObject



82
83
84
85
86
87
# File 'lib/tb/reader.rb', line 82

def each
  while ary = self.shift
    yield ary
  end
  nil
end

#field_from_index(i) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
73
# File 'lib/tb/reader.rb', line 69

def field_from_index(i)
  raise ArgumentError, "negative index: #{i}" if i < 0
  self.header
  @fieldset.field_from_index(i)
end

#field_from_index_ex(i) ⇒ Object

Raises:

  • (ArgumentError)


63
64
65
66
67
# File 'lib/tb/reader.rb', line 63

def field_from_index_ex(i)
  raise ArgumentError, "negative index: #{i}" if i < 0
  self.header
  @fieldset.field_from_index_ex(i)
end

#headerObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tb/reader.rb', line 35

def header
  return @fieldset.header if @fieldset
  if @opt_n
    @fieldset = Tb::FieldSet.new
  else
    while ary = @reader.shift
      if ary.all? {|elt| elt.nil? || elt == '' }
        next
      else
        @fieldset = Tb::FieldSet.new(*ary)
        return @fieldset.header
      end
    end
    @fieldset = Tb::FieldSet.new
  end
  return @fieldset.header
end

#index_from_field(f) ⇒ Object



58
59
60
61
# File 'lib/tb/reader.rb', line 58

def index_from_field(f)
  self.header
  @fieldset.index_from_field(f)
end

#index_from_field_ex(f) ⇒ Object



53
54
55
56
# File 'lib/tb/reader.rb', line 53

def index_from_field_ex(f)
  self.header
  @fieldset.index_from_field_ex(f)
end

#read_allObject



89
90
91
92
93
94
95
# File 'lib/tb/reader.rb', line 89

def read_all
  result = []
  while ary = self.shift
    result << ary
  end
  result
end

#shiftObject



75
76
77
78
79
80
# File 'lib/tb/reader.rb', line 75

def shift
  header
  ary = @reader.shift
  field_from_index_ex(ary.length-1) if ary && !ary.empty?
  ary
end