Class: Extract::SheetDefinition

Inherits:
Object
  • Object
show all
Includes:
FromHash
Defined in:
lib/extract/sheet_definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sheetObject

Returns the value of attribute sheet.



4
5
6
# File 'lib/extract/sheet_definition.rb', line 4

def sheet
  @sheet
end

Class Method Details

.load(file, output, sheet_name = nil) ⇒ Object



144
145
146
147
148
149
# File 'lib/extract/sheet_definition.rb', line 144

def load(file,output,sheet_name=nil)
  res = new
  res.sheet = Sheet.load(file,sheet_name)
  res.output_cells = output
  res
end

Instance Method Details

#[](c) ⇒ Object



102
103
104
# File 'lib/extract/sheet_definition.rb', line 102

def [](c)
  sheet[c]
end

#deps(cell, ops = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/extract/sheet_definition.rb', line 53

def deps(cell,ops={})
  raw = sheet.deps(cell)
  if ops[:table]
    res = []
    raw.each do |c|
      t = tables.for_cell(c)
      res << (t || c)
    end
    res.uniq.sort
  else
    raw
  end
end

#each_inputObject



109
110
111
112
113
# File 'lib/extract/sheet_definition.rb', line 109

def each_input
  input_cells.each do |cell|
    yield cell, cell_names[cell],sheet[cell]
  end
end

#each_otherObject



136
137
138
139
140
141
# File 'lib/extract/sheet_definition.rb', line 136

def each_other
  each_other_basic do |c|
    d = sheet.deps(c)
    yield c,cell_names[c],sheet[c],sheet.cells[c],d
  end
end

#each_other_basicObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/extract/sheet_definition.rb', line 121

def each_other_basic
  res = []
  bad = input_cells + output_cells
  sheet.cells.each do |k,v|
    if !bad.include?(k)
      res << k
    end
  end

  res.each do |c|
    d = sheet.deps(c)
    yield c if sheet.cells[c].present? && d.size > 0
  end
end

#each_outputObject



115
116
117
118
119
# File 'lib/extract/sheet_definition.rb', line 115

def each_output
  output_cells.sort.each do |cell|
    yield cell, cell_names[cell],sheet[cell],dep_map[cell],sheet.cells[cell]
  end
end

#left(c) ⇒ Object



14
15
16
17
18
19
# File 'lib/extract/sheet_definition.rb', line 14

def left(c)
  col = c[0..0]
  row = c[1..-1]
  col = prev_letter(col)
  "#{col}#{row}"
end

#output_cells=(arr) ⇒ Object



35
36
37
# File 'lib/extract/sheet_definition.rb', line 35

def output_cells=(arr)
  @output_cells = Extract.expand_cells(arr).uniq
end

#prev_letter(letter) ⇒ Object



8
9
10
11
12
13
# File 'lib/extract/sheet_definition.rb', line 8

def prev_letter(letter)
  r = ("A".."Z").to_a
  raise "bad letter #{letter}" unless r.index(letter)
  i = r.index(letter) - 1
  r[i]
end

#raw_value(c) ⇒ Object



105
106
107
# File 'lib/extract/sheet_definition.rb', line 105

def raw_value(c)
  sheet.cells[c]
end

#save!(res = nil) ⇒ Object



95
96
97
98
99
100
# File 'lib/extract/sheet_definition.rb', line 95

def save!(res=nil)
  res ||= Persist::Sheet.new
  setup_persisted_sheet! res
  res.save!
  res
end

#setup_persisted_sheet!(res = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/extract/sheet_definition.rb', line 75

def setup_persisted_sheet!(res=nil)
  res.cells = {}
  res.input_cells = []
  res.output_cells = []

  sheet.cells.each do |k,v|
    res.cells[k] = v
  end

  input_cells.each do |c|
    res.input_cells << c
  end

  output_cells.each do |c|
    res.output_cells << c
  end

  res
end