Module: Workbook::Modules::TableDiffSort

Included in:
Table
Defined in:
lib/workbook/modules/diff_sort.rb

Overview

Adds diffing and sorting functions

Instance Method Summary collapse

Instance Method Details

#align(other, options = {}) ⇒ Object

aligns itself with another table, used by diff

Parameters:

  • other (Workbook::Table)

    table to align with

  • options (Hash) (defaults to: {})

    default to: ‘:sort=>true,:ignore_headers=>false`



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/workbook/modules/diff_sort.rb', line 116

def align other, options = {}
  options = {sort: true, ignore_headers: false}.merge(options)

  sother = other.clone.remove_empty_lines!
  sself = clone.remove_empty_lines!

  if options[:ignore_headers]
    sother.header = false
    sself.header = false
  end

  sother = options[:sort] ? sother.sort : sother
  sself = options[:sort] ? sself.sort : sself

  row_index = 0
  while (row_index < [sother.count, sself.count].max) && (row_index < other.count + count)
    row_index = align_row(sself, sother, row_index)
  end

  {self: sself, other: sother}
end

#diff(other, options = {}) ⇒ Workbook::Table

create an overview of the differences between itself with another ‘previous’ table, returns a book with a single sheet and table (containing the diffs)

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/workbook/modules/diff_sort.rb', line 58

def diff other, options = {}
  options = {sort: true, ignore_headers: false}.merge(options)

  aligned = align(other, options)
  aself = aligned[:self]
  aother = aligned[:other]

  iteration_cols = []
  iteration_cols = if options[:ignore_headers]
    [aother.first.count, aself.first.count].max.times.collect
  else
    (aother.header.to_symbols + aother.header.to_symbols).uniq
  end

  diff_table = diff_template
  maxri = (aself.count - 1)

  (0..maxri).each do |ri|
    row = diff_table.rows[ri] = Workbook::Row.new(nil, diff_table)
    srow = aself.rows[ri]
    orow = aother.rows[ri]

    iteration_cols.each_with_index do |ch, ci|
      scell = srow[ch]
      ocell = orow[ch]
      row[ci] = create_diff_cell(scell, ocell)
    end
  end
  unless options[:ignore_headers]
    diff_table[0].format = diff_template.template.create_or_find_format_by "header"
  end

  diff_table
end

#diff_templateWorkbook::Table

Return template table to write the diff result in; in case non exists a default is generated.

Returns:



96
97
98
99
100
101
# File 'lib/workbook/modules/diff_sort.rb', line 96

def diff_template
  return @diff_template if defined?(@diff_template)
  diffbook = Workbook::Book.new_diff_template
  difftable = diffbook.sheet.table
  @diff_template ||= difftable
end

#diff_template=(table) ⇒ Workbook::Table

Set the template table to write the diff result in; in case non exists a default is generated. Make sure that the following formats exists: destroyed, updated, created and header.

Parameters:

Returns:



108
109
110
# File 'lib/workbook/modules/diff_sort.rb', line 108

def diff_template= table
  @diff_template = table
end

#sortObject



138
139
140
# File 'lib/workbook/modules/diff_sort.rb', line 138

def sort
  clone.sort!
end

#sort!Object



142
143
144
145
146
# File 'lib/workbook/modules/diff_sort.rb', line 142

def sort!
  header_row = @rows.delete_at(header_row_index) if header
  @rows = [header_row] + @rows.sort
  self
end