Class: Plist4r::Table
Overview
A data type representation for tables. The underlying store is an array of arrays (2d). The addressing scheme is based on (column, row) order, with Range objects to specify the bounds of any rectangle of elements withing the table.
A variety of methods are provided for manipulating the table data, including flipping, inserting, replacing and deleting. Operations can be column-based, row-based, or both.
Constant Summary collapse
- EmptyCell =
The value to assign to an empty cell
nil- EmptyCells =
The range of valid values for which match the empty cell criteria (for which the cell are ignored)
[nil, false]
- MinPadSize =
When allocating the arrays for a new table, the minimum size to pad around with empty cells.
10- OptionsHash =
%w[ size array pad_all fill_all ]
Class Method Summary collapse
-
.sanitize_ranges(*ranges) ⇒ Object
This class only understands a range addressing scheme, which is used to specify table locations in a [columns, rows] caresian system starting at col 0, row 0.
Instance Method Summary collapse
- #array(array = nil) ⇒ Object
- #ascii_col_width ⇒ Object
- #auto_size ⇒ Object
- #cell(col, row, value = nil) ⇒ Object
- #cells(col_range = nil, row_range = nil, value = nil) ⇒ Object
- #col(col_range, value = nil) ⇒ Object
- #col_insert(col_range, row_range, other_data, resize = true) ⇒ Object
- #col_range ⇒ Object
- #col_replace(col_range, data) ⇒ Object
- #crop(col_range, row_range) ⇒ Object
- #data_replace(col_range, row_range, other_data) ⇒ Object
- #each(col_range = nil, row_range = nil, &blk) ⇒ Object
- #fill(col_range, row_range, data = nil) ⇒ Object
- #fill_all(data) ⇒ Object
- #first(value = nil) ⇒ Object
-
#initialize(*args, &blk) ⇒ Table
constructor
A new instance of Table.
- #inspect(start_col = 0) ⇒ Object
- #inverse_fill(col_range, row_range, data = nil) ⇒ Object
- #map(col_range = nil, row_range = nil, &blk) ⇒ Object
- #pad(col_range, row_range, data) ⇒ Object
- #pad_all(data) ⇒ Object
-
#parse_opts(opts) ⇒ Object
Sets up those valid (settable) attributes as found the options hash.
- #replace(col_range, row_range, data) ⇒ Object
- #resize(col_range, row_range) ⇒ Object
- #row(row_range, value = nil) ⇒ Object
- #row_insert(col_range, row_range, other_data, resize = true) ⇒ Object
- #row_range ⇒ Object
- #row_replace(row_range, data) ⇒ Object
- #size(*args) ⇒ Object
- #translate(col_range, row_range, vector) ⇒ Object
- #transpose(col_range = nil, row_range = nil, keep_bounds = false) ⇒ Object
Constructor Details
#initialize(*args, &blk) ⇒ Table
Returns a new instance of Table.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/plist4r/mixin/table.rb', line 52 def initialize *args, &blk @array = [] case args[0] when nil resize 0..0, 0..0 when Hash parse_opts args[0] when Plist4r::Table %w[ col_range row_range array ].each do |a| self.send a.to_sym, args[0].send(a.to_sym).deep_clone end else raise "unsupported type" end resize 0..0, 0..0 unless @cr && @rr end |
Class Method Details
.sanitize_ranges(*ranges) ⇒ Object
This class only understands a range addressing scheme, which is used to specify table locations in a [columns, rows] caresian system starting at col 0, row 0.
Convert any Integer numbers into range objects. Check that all input ranges are positive, starting (and including) zero as the first index.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/plist4r/mixin/table.rb', line 19 def sanitize_ranges *ranges sanitized_ranges = [] ranges.flatten.each do |range| case range when Range if range.exclude_end? range = range.first..(range.last - 1) end when Integer range = (range..range) else raise "Unsupported type" end if range.first < 0 || range.last < 0 raise "range cannot cover negative values" end sanitized_ranges << range end sanitized_ranges end |
Instance Method Details
#array(array = nil) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/plist4r/mixin/table.rb', line 181 def array array=nil case array when nil @array when Array if array.multidim? @array = array auto_size unless @cr && @rr else raise "array type not supported" end else raise "Unsupported type" end end |
#ascii_col_width ⇒ Object
87 88 89 90 |
# File 'lib/plist4r/mixin/table.rb', line 87 def ascii_col_width lines = self.inspect.split "\n" lines[0].length end |
#auto_size ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/plist4r/mixin/table.rb', line 214 def auto_size ce = @array.size - 1 unless @cr && (0..ce).include_range?(@cr) @cr = 0..ce end re = 0 @array.each do |col| re = col.size - 1 if col.size - 1 > re end unless @rr && (0..re).include_range?(@rr) @rr = 0..re end end |
#cell(col, row, value = nil) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/plist4r/mixin/table.rb', line 138 def cell col, row, value=nil raise "unsupported type" unless col.is_a?(Integer) && row.is_a?(Integer) return nil if col < 0 || row < 0 case value when nil @array[col][row] else if value.is_a? Plist4r::Table @array[col][row] = value.cell col, row else @array[col][row] = value end end end |
#cells(col_range = nil, row_range = nil, value = nil) ⇒ Object
404 405 406 407 408 409 410 |
# File 'lib/plist4r/mixin/table.rb', line 404 def cells col_range=nil, row_range=nil, value=nil col_range = @cr if col_range.nil? ; row_range = @rr if row_range.nil? col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range replace col_range, row_range, value if value self.class.new :array => @array, :size => [col_range, row_range] end |
#col(col_range, value = nil) ⇒ Object
412 413 414 415 416 417 418 419 420 421 |
# File 'lib/plist4r/mixin/table.rb', line 412 def col col_range, value=nil col_range = Plist4r::Table.sanitize_ranges col_range case value when nil cells col_range, @rr else col_replace col_range, value end end |
#col_insert(col_range, row_range, other_data, resize = true) ⇒ Object
390 391 392 393 394 395 |
# File 'lib/plist4r/mixin/table.rb', line 390 def col_insert col_range, row_range, other_data, resize=true col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range translate col_range.first..@cr.last, @rr, [col_range.size, 0] replace col_range, row_range, other_data @cr = @cr.first..(@cr.last + col_range.size) if resize end |
#col_range ⇒ Object
130 131 132 |
# File 'lib/plist4r/mixin/table.rb', line 130 def col_range @cr end |
#col_replace(col_range, data) ⇒ Object
370 371 372 373 |
# File 'lib/plist4r/mixin/table.rb', line 370 def col_replace col_range, data col_range = Plist4r::Table.sanitize_ranges col_range replace col_range, @rr.first..@rr.last, data end |
#crop(col_range, row_range) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/plist4r/mixin/table.rb', line 262 def crop col_range, row_range col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range pad col_range, row_range, EmptyCell crop_obj = self.class.new :size => [0..(col_range.size - 1), 0..(row_range.size - 1)] col_range.each do |col| row_range.each do |row| crop_obj.array[col-col_range.first][row-row_range.first] = @array[col][row].deep_clone end end crop_obj end |
#data_replace(col_range, row_range, other_data) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/plist4r/mixin/table.rb', line 336 def data_replace col_range, row_range, other_data col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range self.pad col_range, row_range, EmptyCell o = other_data.deep_clone if o.col_range.size < col_range.size col_range = (col_range.first)..(row_range.first + o.col_range.size - 1) end if o.row_range.size < row_range.size row_range = (row_range.first)..(row_range.first + o.row_range.size - 1) end col_range.each do |col| row_range.each do |row| # @array[col][row] = o.array[col-col_range.first+o.col_range.first][row-row_range.first+o.row_range.first].deep_clone cell col, row, o.cell(col-col_range.first+o.col_range.first,row-row_range.first+o.row_range.first).deep_clone end end end |
#each(col_range = nil, row_range = nil, &blk) ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/plist4r/mixin/table.rb', line 171 def each col_range=nil, row_range=nil, &blk col_range = @cr if col_range.nil? ; row_range = @rr if row_range.nil? col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range row_range.each do |row| col_range.each do |col| yield cell(col, row) end end end |
#fill(col_range, row_range, data = nil) ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/plist4r/mixin/table.rb', line 276 def fill col_range, row_range, data=nil col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range data = EmptyCell unless data pad col_range, row_range, EmptyCell col_range.each do |c| row_range.each do |r| @array[c][r] = data.deep_clone end end end |
#fill_all(data) ⇒ Object
288 289 290 |
# File 'lib/plist4r/mixin/table.rb', line 288 def fill_all data fill @cr, @rr, data end |
#first(value = nil) ⇒ Object
154 155 156 |
# File 'lib/plist4r/mixin/table.rb', line 154 def first value=nil cell @cr.first, @rr.first, value end |
#inspect(start_col = 0) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/plist4r/mixin/table.rb', line 92 def inspect start_col=0 cell_width = Plist4r::Table.new :size => [@cr, 0..0], :fill_all => 5 @cr.each do |col| @rr.each do |row| cell_size = @array[col][row].inspect.size cell_width.cell(col, 0, cell_size) if cell_size > cell_width.cell(col,0) end end col_pad_pre = " " col_pad_post = " " vert_border = "|" horz_border = "-" row_sep = "" row_sep << " " * start_col @cr.each do |col| row_sep << vert_border + horz_border * (col_pad_pre.size + cell_width.cell(col,0) + col_pad_post.size) end row_sep << vert_border o = "" o << row_sep << "\n" @rr.each do |row| o << " " * start_col @cr.each do |col| cell_str = vert_border + col_pad_pre + " " * cell_width.cell(col,0) + col_pad_post cell_str[vert_border.size+col_pad_pre.size,@array[col][row].inspect.size] = @array[col][row].inspect o << cell_str end o << vert_border o << "\n" o << row_sep << "\n" end return o end |
#inverse_fill(col_range, row_range, data = nil) ⇒ Object
292 293 294 295 296 297 298 299 |
# File 'lib/plist4r/mixin/table.rb', line 292 def inverse_fill col_range, row_range, data=nil col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range data = EmptyCell unless data crop_obj = crop col_range, row_range fill @cr, @rr, data replace col_range, row_range, crop_obj end |
#map(col_range = nil, row_range = nil, &blk) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/plist4r/mixin/table.rb', line 158 def map col_range=nil, row_range=nil, &blk col_range = @cr if col_range.nil? ; row_range = @rr if row_range.nil? col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range t = Plist4r::Table.new :size => [col_range, row_range] row_range.each do |row| col_range.each do |col| t.cell col, row, yield(cell(col, row)) end end t end |
#pad(col_range, row_range, data) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/plist4r/mixin/table.rb', line 230 def pad col_range, row_range, data col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range if EmptyCells.include? data if col_range.end < MinPadSize - 1 col_range = 0..(MinPadSize - 1) else col_range = 0..(col_range.end) end if row_range.end < MinPadSize - 1 row_range = 0..(MinPadSize - 1) else row_range = 0..(row_range.end) end end (col_range.end - @array.size + 1).times do @array << [] end col_range.each do |col| row_range.each do |row| @array[col][row] = data.deep_clone if EmptyCells.include? @array[col][row] end end end |
#pad_all(data) ⇒ Object
258 259 260 |
# File 'lib/plist4r/mixin/table.rb', line 258 def pad_all data pad @cr, @rr, data end |
#parse_opts(opts) ⇒ Object
Sets up those valid (settable) attributes as found the options hash. Normally we dont call this method directly. Called from #initialize.
78 79 80 81 82 83 84 85 |
# File 'lib/plist4r/mixin/table.rb', line 78 def parse_opts opts self.class::OptionsHash.each do |opt| if opts[opt.to_sym] value = opts[opt.to_sym] eval "self.#{opt}(value)" end end end |
#replace(col_range, row_range, data) ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/plist4r/mixin/table.rb', line 358 def replace col_range, row_range, data col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range case data when Plist4r::Table data_replace col_range, row_range, data else fill col_range, row_range, data end self end |
#resize(col_range, row_range) ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/plist4r/mixin/table.rb', line 206 def resize col_range, row_range col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range @cr = col_range @rr = row_range pad @cr, @rr, EmptyCell end |
#row(row_range, value = nil) ⇒ Object
423 424 425 426 427 428 429 430 431 432 |
# File 'lib/plist4r/mixin/table.rb', line 423 def row row_range, value=nil row_range = Plist4r::Table.sanitize_ranges row_range case value when nil cells @cr, row_range else row_replace row_range, value end end |
#row_insert(col_range, row_range, other_data, resize = true) ⇒ Object
397 398 399 400 401 402 |
# File 'lib/plist4r/mixin/table.rb', line 397 def row_insert col_range, row_range, other_data, resize=true col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range translate @cr, row_range.first..@rr.last, [0, row_range.size] replace col_range, row_range, other_data @rr = @rr.first..(@rr.last + row_range.size) if resize end |
#row_range ⇒ Object
134 135 136 |
# File 'lib/plist4r/mixin/table.rb', line 134 def row_range @rr end |
#row_replace(row_range, data) ⇒ Object
375 376 377 378 |
# File 'lib/plist4r/mixin/table.rb', line 375 def row_replace row_range, data row_range = Plist4r::Table.sanitize_ranges row_range replace @cr.first..@cr.last, row_range, data end |
#size(*args) ⇒ Object
197 198 199 200 201 202 203 204 |
# File 'lib/plist4r/mixin/table.rb', line 197 def size *args if args.empty? [@cr, @rr] else col_range, row_range = args.flatten resize col_range, row_range end end |
#translate(col_range, row_range, vector) ⇒ Object
380 381 382 383 384 385 386 387 388 |
# File 'lib/plist4r/mixin/table.rb', line 380 def translate col_range, row_range, vector col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range crop_obj = crop col_range, row_range fill col_range, row_range, EmptyCell col_range = (col_range.begin+vector[0])..(col_range.end+vector[0]) row_range = (row_range.begin+vector[1])..(row_range.end+vector[1]) replace col_range, row_range, crop_obj end |
#transpose(col_range = nil, row_range = nil, keep_bounds = false) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/plist4r/mixin/table.rb', line 301 def transpose col_range=nil, row_range=nil, keep_bounds=false col_range = @cr if col_range.nil? ; row_range = @rr if row_range.nil? col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range if col_range == @cr && row_range == @rr && @cr.first == @rr.first @array = @array.transpose if keep_bounds @cr,@rr = [[@cr,@rr].min,[@cr,@rr].min] else @cr,@rr = [@rr, @cr] end self else col_range, row_range = Plist4r::Table.sanitize_ranges col_range, row_range crop_obj = crop col_range, row_range crop_obj.transpose fill col_range, row_range, EmptyCell ocr,orr = [col_range,row_range] col_range = (ocr.begin)..(ocr.begin+orr.size-1) row_range = (orr.begin)..(orr.begin+ocr.size-1) if keep_bounds && col_range.size != row_range.size if ocr.size > orr.size row_range = (row_range.begin)..(orr.end) else col_range = (col_range.begin)..(ocr.end) end end replace col_range, row_range, crop_obj end self end |