Class: CopyPastePDF::Table
- Inherits:
-
Array
- Object
- Array
- CopyPastePDF::Table
- Defined in:
- lib/copy_paste_pdf/table.rb
Instance Method Summary collapse
-
#copy_into_cell_below(*indices) {|row| ... } ⇒ Object
Copies the values of the given cells from a line to any following lines whose corresponding cells are empty.
-
#merge_into_cell_above(*indices) ⇒ Object
Merges the values of the given cells from a line, whose other cells are empty, into the corresponding cells of the prececeding line.
-
#remove_empty_rows! ⇒ Object
Removes all empty rows from the table.
Instance Method Details
#copy_into_cell_below(*indices) {|row| ... } ⇒ Object
Copies the values of the given cells from a line to any following lines whose corresponding cells are empty.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/copy_paste_pdf/table.rb', line 21 def copy_into_cell_below(*indices) source = nil each do |row| if !block_given? || !yield(row) values = row.values_at(*indices) case values.count(&:nil?) when 0 source = values when indices.size if source indices.each_with_index do |index,i| if row[index] raise CopyToNonEmptyCellError, "destination cell #{index} already has a value #{row[index]}" else row[index] = source[i] end end else raise MissingSourceError, "#{row} is a destination, but it has no source" end else raise InvalidRowError, "#{row} is neither a source nor a destination" end end end end |
#merge_into_cell_above(*indices) ⇒ Object
Merges the values of the given cells from a line, whose other cells are empty, into the corresponding cells of the prececeding line.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/copy_paste_pdf/table.rb', line 53 def merge_into_cell_above(*indices) each_with_index.reverse_each do |row,i| if row.each_with_index.all?{|value,j| value.nil? || indices.include?(j)} indices.each do |index| if self[i - 1][index] self[i - 1][index] = "#{self[i - 1][index]}\n#{row[index]}" else raise MergeWithEmptyCellError, "cell #{index} is empty and can't be merged" end end delete_at(i) end end end |
#remove_empty_rows! ⇒ Object
Removes all empty rows from the table.
4 5 6 7 8 |
# File 'lib/copy_paste_pdf/table.rb', line 4 def remove_empty_rows! reject! do |row| row.all?(&:nil?) end end |