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 = {:sort=>true,:ignore_headers=>false}) ⇒ Object

aligns itself with another table, used by diff

Parameters:

  • other (Workbook::Table)

    table to align with

  • options (Hash) (defaults to: {:sort=>true,:ignore_headers=>false})

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/workbook/modules/diff_sort.rb', line 141

def align other, options={:sort=>true,:ignore_headers=>false}

  options = {:sort=>true,:ignore_headers=>false}.merge(options)

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

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

  sother = options[:sort] ? Workbook::Table.new(sother.sort) : sother
  sself = options[:sort] ? Workbook::Table.new(sself.sort) : sself

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

  {:self=>sself, :other=>sother}
end

#align_row(sself, sother, row_index) ⇒ Object

for use in the align ‘while’ loop



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/workbook/modules/diff_sort.rb', line 165

def align_row sself, sother, row_index
  asd = 0
  if sself[row_index] and sother[row_index]
    asd = sself[row_index].key <=> sother[row_index].key
  elsif sself[row_index]
    asd = -1
  elsif sother[row_index]
    asd = 1
  end
  if asd == -1 and insert_placeholder?(sother, sself, row_index)
    sother.insert row_index, placeholder_row
    row_index -=1
  elsif asd == 1 and insert_placeholder?(sother, sself, row_index)
    sself.insert row_index, placeholder_row
    row_index -=1
  end

  row_index += 1
end

#create_diff_cell(scell, ocell) ⇒ Workbook::Cell

creates a new cell describing the difference between two cells

Returns:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/workbook/modules/diff_sort.rb', line 96

def create_diff_cell(scell, ocell)
  dcell = scell.nil? ? Workbook::Cell.new(nil) : scell
  if (scell == ocell)
    dcell.format = scell.format if scell
  elsif scell.nil?
    dcell = Workbook::Cell.new "(was: #{ocell.to_s})"
    dcell.format = diff_template.template.create_or_find_format_by 'destroyed'
  elsif ocell.nil?
    dcell = scell.clone
    fmt = scell.nil? ? :default : scell.format[:number_format]
    f = diff_template.template.create_or_find_format_by 'created', fmt
    f[:number_format] = scell.format[:number_format]
    dcell.format = f
  elsif scell != ocell
    dcell = Workbook::Cell.new "#{scell.to_s} (was: #{ocell.to_s})"
    f = diff_template.template.create_or_find_format_by 'updated'
    dcell.format = f
  end

  dcell
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 = []
  if options[:ignore_headers]
    iteration_cols = [aother.first.count,aself.first.count].max.times.collect
  else
    iteration_cols = (aother.header.to_symbols+aother.header.to_symbols).uniq
  end

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

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

    iteration_cols.each_with_index do |ch, ci|
      scell = srow[ch]
      ocell = orow[ch]
      row[ci] = create_diff_cell(scell, ocell)
    end
  end
  if !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:



121
122
123
124
125
126
# File 'lib/workbook/modules/diff_sort.rb', line 121

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:



133
134
135
# File 'lib/workbook/modules/diff_sort.rb', line 133

def diff_template= table
  @diff_template= table
end

#insert_placeholder?(sother, sself, row_index) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
188
# File 'lib/workbook/modules/diff_sort.rb', line 185

def insert_placeholder? sother, sself, row_index
  (sother[row_index].nil? or !sother[row_index].placeholder?) and
  (sself[row_index].nil? or !sself[row_index].placeholder?)
end

#placeholder_rowObject

returns a placeholder row, for internal use only



191
192
193
194
195
196
197
198
199
# File 'lib/workbook/modules/diff_sort.rb', line 191

def placeholder_row
  if defined?(@placeholder_row) and !@placeholder_row.nil?
    return @placeholder_row
  else
    @placeholder_row = Workbook::Row.new [nil]
    placeholder_row.placeholder = true
    return @placeholder_row
  end
end