Class: Tb::HeaderReader

Inherits:
Object
  • Object
show all
Includes:
EnumerableWithEach
Defined in:
lib/tb/headerreader.rb

Overview

lib/tb/headerreaderm.rb - reader mixin for table with header

Copyright (C) 2014 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.

Direct Known Subclasses

HeaderCSVReader, HeaderTSVReader

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EnumerableWithEach

#each

Methods included from Enumerable

#cat, #extsort_by, #natjoin2, #natjoin2_outer, #newfield, #to_fileenumerator, #with_cumulative_header, #with_header, #write_to_csv, #write_to_json, #write_to_ltsv, #write_to_pnm, #write_to_tsv, #write_with

Constructor Details

#initialize(get_array) ⇒ HeaderReader

Returns a new instance of HeaderReader.



34
35
36
37
38
39
# File 'lib/tb/headerreader.rb', line 34

def initialize(get_array)
  @get_array = get_array
  @header_array_hook = nil
  @row_array_hook = nil
  @enable_warning = true
end

Instance Attribute Details

#enable_warning ⇒ Object

Returns the value of attribute enable_warning.



42
43
44
# File 'lib/tb/headerreader.rb', line 42

def enable_warning
  @enable_warning
end

#header_array_hook ⇒ Object

Returns the value of attribute header_array_hook.



40
41
42
# File 'lib/tb/headerreader.rb', line 40

def header_array_hook
  @header_array_hook
end

#row_array_hook ⇒ Object

Returns the value of attribute row_array_hook.



41
42
43
# File 'lib/tb/headerreader.rb', line 41

def row_array_hook
  @row_array_hook
end

Instance Method Details

#get_hash ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tb/headerreader.rb', line 80

def get_hash
  read_header_once
  ary = @get_array.call
  if !ary
    return nil
  end
  @row_array_hook.call(ary) if  @row_array_hook
  hash = {}
  if @header.length < ary.length
    warn "Header too short: header has #{@header.length} fields but a record has #{ary.length} fields : #{ary[@header.length..-1].map(&:inspect).join(',')}" if @enable_warning
    ary[@header.length..-1] = []
  end
  ary.each_with_index {|v, i|
    field = @header[i]
    if !field.nil?
      hash[field] = v
    end
  }
  hash
end

#get_named_header ⇒ Object



75
76
77
78
# File 'lib/tb/headerreader.rb', line 75

def get_named_header
  read_header_once
  @header.compact
end

#header_and_each(header_proc) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/tb/headerreader.rb', line 101

def header_and_each(header_proc)
  header_proc.call(get_named_header) if header_proc
  while hash = get_hash
    yield hash
  end
  nil
end

#header_known? ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/tb/headerreader.rb', line 44

def header_known?
  true
end