Module: Workbook::Modules::TableDiffSort

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

Instance Method Summary collapse

Instance Method Details

#align(other, options = {:sort=>true,:ignore_headers=>false}) ⇒ Object

aligns itself with another table, used by diff



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/workbook/modules/table_diff_sort.rb', line 80

def align other, options={:sort=>true,:ignore_headers=>false}
  
  options = {:sort=>true,:ignore_headers=>false}.merge(options)
  puts " - Removing empty rows"
  
  iteration_cols = nil
  sother = other.clone.remove_empty_lines!
  sself = self.clone.remove_empty_lines!
  
  if options[:ignore_headers]
    sother.header = false
    sself.header = false
  end
  puts " - Sorting"
  
  sother = options[:sort] ? Workbook::Table.new(sother.sort) : sother
  sself = options[:sort] ? Workbook::Table.new(sself.sort) : sself
  
  iteration_rows =  [sother.count,sself.count].max.times.collect
  puts " - Aligning"

  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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/workbook/modules/table_diff_sort.rb', line 110

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

#diff(other, options = {:sort=>true,:ignore_headers=>false}) ⇒ Object

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/workbook/modules/table_diff_sort.rb', line 6

def diff other, options={:sort=>true,:ignore_headers=>false}
  puts "#diff"
  
  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.sheet.table
  puts " - Creating diff-table. Estimated time: #{aself.count*aself.first.count*0.0063}s "
  maxri = (aself.count-1)
  for ri in 0..maxri do
    row = diff_table[ri]
    row = diff_table[ri] = Workbook::Row.new(nil, diff_table)
    # aselfrow = aself[ri]
    #           aselfother
    srow = aself[ri]
    orow = aother[ri]
    
    iteration_cols.each_with_index do |ch, ci|
      scell = srow[ch]
      ocell = orow[ch]
      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
      
      row[ci]=dcell
    end
  end
  if !options[:ignore_headers]
    diff_table[0].format = diff_template.template.create_or_find_format_by 'header'
  end

  diff_template
end

#diff_templateObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/workbook/modules/table_diff_sort.rb', line 60

def diff_template
  return @diff_template if @diff_template
  diffbook = Workbook::Book.new
  difftable = diffbook.sheet.table
  template = diffbook.template
  f = template.create_or_find_format_by 'destroyed'
  f[:background_color]=:red
  f = template.create_or_find_format_by 'updated'
  f[:background_color]=:yellow
  f = template.create_or_find_format_by 'created'
  f[:background_color]=:lime
  f = template.create_or_find_format_by 'header'
  f[:rotation] = 72
  f[:font_weight] = :bold
  f[:height] = 80
  @diff_template = diffbook
  return diffbook
end

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

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/workbook/modules/table_diff_sort.rb', line 130

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



136
137
138
139
140
141
142
143
144
# File 'lib/workbook/modules/table_diff_sort.rb', line 136

def placeholder_row 
  if @placeholder_row != nil
    return @placeholder_row 
  else
    @placeholder_row = Workbook::Row.new [nil]
    placeholder_row.placeholder = true
    return @placeholder_row 
  end
end