Module: RIO::Ext::CSV::Filter::CSVMissing

Defined in:
lib/rio/ext/csv/filter.rb

Instance Method Summary collapse

Instance Method Details

#_calc_csv_columns(num_cols) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rio/ext/csv/filter.rb', line 53

def _calc_csv_columns(num_cols)
  require 'rio/arraynge'
  ycols = cx['col_args']
  ncols = cx['nocol_args']
  if ncols and ncols.empty?
    cx['csv_columns'] = []
  elsif ycols.nil? and ncols.nil?
    cx['csv_columns'] = nil
  else
    ncols = [] if ncols.nil?
    ycols = [(0..-1)] if ycols.nil? or ycols.empty?
    ncols = Arraynge.ml_arraynge(num_cols,ncols)
    ycols = Arraynge.ml_arraynge(num_cols,ycols)
    cx['csv_columns'] = Arraynge.ml_diff(ycols,ncols)
  end
end

#_calc_csv_fields(row) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rio/ext/csv/filter.rb', line 35

def _calc_csv_fields(row)
  num_cols = row.size-1

  require 'rio/arraynge'
  ycols = _fields_to_columns(row,cx['col_args']) unless cx['col_args'].nil?
  ncols = _fields_to_columns(row,cx['nocol_args'])  unless cx['nocol_args'].nil?
  if ncols and ncols.empty?
    cx['csv_columns'] = []
  elsif ycols.nil? and ncols.nil?
    cx['csv_columns'] = nil
  else
    ncols = [] if ncols.nil?
    ycols = [(0..-1)] if ycols.nil? or ycols.empty?
    ncols = Arraynge.ml_arraynge(num_cols,ncols)
    ycols = Arraynge.ml_arraynge(num_cols,ycols)
    cx['csv_columns'] = Arraynge.ml_diff(ycols,ncols)
  end
end

#_fields_to_columns(row, flds) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rio/ext/csv/filter.rb', line 100

def _fields_to_columns(row,flds)
  cols = []
  flds.each do |fld|
    case fld
    when Range
      ibeg = fld.begin.is_a?(Integer) ? fld.begin : row.index(fld.begin)
      iend = fld.end.is_a?(Integer) ? fld.end : row.index(fld.end)
      rng = fld.exclude_end? ? (ibeg...iend) : (ibeg..iend)
      cols << rng
    when Integer
      cols << fld
    else
      cols << row.index(fld)
    end
  end
  cols.flatten
end

#_trim(fields) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rio/ext/csv/filter.rb', line 69

def _trim(fields)
  _calc_csv_columns(fields.size-1)

  return fields if cx['csv_columns'].nil?
  case fields
  when ::CSV::Row
    fields.fields(*cx['csv_columns'])
  else
    cx['csv_columns'].map{|idx| fields[idx]}.flatten
  end
end

#_trim_col(mx, cols) ⇒ Object



95
96
97
98
99
# File 'lib/rio/ext/csv/filter.rb', line 95

def _trim_col(mx,cols)
  cols.map do |el|
    (el.is_a?(::Range) and el.max > mx ? el.min..mx : el)
  end
end

#_trim_row(row) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rio/ext/csv/filter.rb', line 80

def _trim_row(row)
  _calc_csv_fields(row)
  return row if cx['csv_columns'].nil?

  cols = cx['csv_columns']
  case row
  when ::CSV::Row
    hdrs = cols.map{|idx| row.headers[idx]}.flatten
    flds = cols.empty? ? [] : row.fields(*cols)
    row.class.new(hdrs,flds,row.header_row?)
  else
    flds = cols.map{|idx| row[idx]}.flatten
    ::CSV::Row.new([],flds)
  end
end

#cxObject



34
# File 'lib/rio/ext/csv/filter.rb', line 34

def cx() @cx end

#each_line(*args, &block) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rio/ext/csv/filter.rb', line 119

def each_line(*args,&block)
  while raw_rec = self.shift()
    case cx['stream_itertype']
    when 'lines' 
      yield _trim(raw_rec).to_csv(*cx['csv_args'])
    when 'records'
      case raw_rec
      when ::Array then yield _trim(raw_rec)
      else yield _trim(raw_rec.fields)
      end
    when 'rows'
      yield _trim_row(raw_rec)
    else
      yield _trim(raw_rec)
    end
  end
end

#each_line0(*args, &block) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rio/ext/csv/filter.rb', line 138

def each_line0(*args,&block)
  self.each(*args) do |raw_rec|
    case cx['stream_itertype']
    when 'lines' 
      yield _trim(raw_rec).to_csv(*cx['csv_args'])
    when 'records'
      case raw_rec
      when ::Array then yield _trim(raw_rec)
      else yield _trim(raw_rec.fields)
      end
    when 'rows'
      yield _trim_row(raw_rec)
    else
      yield _trim(raw_rec)
    end
  end
end

#set_cx(context) ⇒ Object



31
32
33
# File 'lib/rio/ext/csv/filter.rb', line 31

def set_cx(context)
  @cx = context
end