Class: CA::CSVReader::Processor
- Inherits:
-
Object
- Object
- CA::CSVReader::Processor
- Defined in:
- lib/carray/io/csv.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#names ⇒ Object
readonly
Returns the value of attribute names.
Instance Method Summary collapse
- #body(n = nil, cols = nil) ⇒ Object
- #column_names(*namelist) ⇒ Object
- #convert(data_type, options = {}, &block) ⇒ Object
- #header(name = "names") ⇒ Object
-
#initialize(io, sep, rs = $/, &block) ⇒ Processor
constructor
A new instance of Processor.
- #note(n = 1) ⇒ Object
- #process ⇒ Object
- #run ⇒ Object
- #select(*namelist) ⇒ Object
- #skip(n = 1) ⇒ Object
Constructor Details
#initialize(io, sep, rs = $/, &block) ⇒ Processor
Returns a new instance of Processor.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/carray/io/csv.rb', line 99 def initialize (io, sep, rs=$/, &block) @io = io @sep = sep @rs = rs @sc = StringScanner.new("") @block = block || proc { body } @namelist = nil @names = nil @headerlist = [] @header = {} @note = "" @table = nil @regexp_simple1 = /#{@sep}/ @regexp_simple2 = / *#{@sep} */ @regexp_pat1 = /\A([^"#{@sep}][^#{@sep}]*) *#{@sep} */ @regexp_pat2 = /\A"([^"]+)" *#{@sep} */ @regexp_pat3 = /\A"((?:[^"]+|"")+)" *#{@sep} */ @regexp_pat4 = /\A(?:""|) *#{@sep} */ end |
Instance Attribute Details
#names ⇒ Object (readonly)
Returns the value of attribute names.
173 174 175 |
# File 'lib/carray/io/csv.rb', line 173 def names @names end |
Instance Method Details
#body(n = nil, cols = nil) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/carray/io/csv.rb', line 186 def body (n=nil, cols=nil) unless n n = 0x80000000 end data = [] count = 0 if cols @cols = cols elsif @names @cols = @names.size else list = csv_feed() if list.nil? @rows = 0 @table = CArray.object(@rows, @cols) return end data.push(list) count += 1 @cols = list.size end lsize = nil while count < n and list = csv_feed(@cols) lsize = list.size if lsize == @cols data.push(list) elsif lsize <= @cols record = Array.new(@cols, nil) record[0,lsize] = list data.push(record) else extra = Array.new(lsize - @cols, nil) data.each do |row| row.push(*extra) end data.push(list) @cols = lsize # raise "csv parse error : too large column number at line #{@io.lineno}" end count += 1 end @rows = data.size @table = CArray.object(@rows, @cols){ data } @table[:eq,""] = nil end |
#column_names(*namelist) ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/carray/io/csv.rb', line 150 def column_names (*namelist) if @header.has_key?("names") warn "override header['names']" end @names = namelist.map(&:to_s) @header["names"] = @names return namelist end |
#convert(data_type, options = {}, &block) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/carray/io/csv.rb', line 282 def convert (data_type, ={}, &block) if block_given? if data_type.is_a?(Class) and data_type < CA::Struct @table = @table[:i, nil].convert(data_type, &block) else @table = @table.convert(data_type, , &block) end else if data_type.is_a?(Class) and data_type < CA::Struct @table = @table[:i,nil].convert(data_type) { |b| data_type.new(*b[0,nil]) } else @table = @table.to_type(data_type, ) end end end |
#header(name = "names") ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/carray/io/csv.rb', line 159 def header (name = "names") name = name.to_s list = csv_feed() if name == "names" if @names raise "already 'names' defined" end @names = list end @header[name] = list @headerlist.push(name) return list end |
#note(n = 1) ⇒ Object
175 176 177 178 179 180 |
# File 'lib/carray/io/csv.rb', line 175 def note (n=1) list = [] n.times { list << @io.gets(@rs) } @note << (text = list.join) return text end |
#process ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/carray/io/csv.rb', line 266 def process if @namelist @namelist.each_with_index do |name, i| yield(name, @table[nil, i]) end elsif @names @names.each_with_index do |name, i| yield(name, @table[nil, i]) end else @table.dim1.times do |i| yield(i, @table[nil,i]) end end end |
#run ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/carray/io/csv.rb', line 119 def run case @block.arity when 1 @block.call(self) when -1, 0 instance_exec(&@block) else raise "invalid block paramter" end header = @header note = @note @table.instance_exec{ @names = header["names"] @header = header @note = note } @table.extend(CA::TableMethods) @table.column_names = header["names"] class << @table attr_reader :note def header (name=nil) if name return @header[name.to_s] else return @header end end end return @table end |
#select(*namelist) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/carray/io/csv.rb', line 235 def select (*namelist) @namelist = namelist.empty? ? nil : namelist case @namelist when nil when Array index = (0...@cols).map.to_a index_list = @namelist.map{ |x| case x when Integer x when Range index[x] when String, Symbol if @names and i = @names.index(x.to_s) i else raise "invalid argument #{x}" end else raise "invalid argument" end }.flatten @table = @table[nil, CA_INT(index_list)].to_ca @header.keys.each do |k| @header[k] = @header[k].values_at(*index_list) end else raise end end |
#skip(n = 1) ⇒ Object
182 183 184 |
# File 'lib/carray/io/csv.rb', line 182 def skip (n=1) n.times { @io.gets(@rs) } end |