Class: Rubypivot::SpreadTable

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypivot/spread_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_source, options = {}) ⇒ SpreadTable



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rubypivot/spread_table.rb', line 175

def initialize(data_source, options = {})
  @rows = []
  @options = {}
  @options_for_line = {}
  options.each do |k, v|
    if [:title, :data_line_class, :header_line_class, :data_title_class, :header_title_class, :data_format].include?(k)
      @options_for_line[k] = v
    else
      @options[k] = v
    end
  end
  @attribs = []

  if data_source.is_a? Array
    data_source.each do |line|
      @rows << SpreadTableLine.new(:data, line, @options_for_line)
    end
  elsif data_source.is_a? Hash
    data_source.each do |title, values|
      @rows << SpreadTableLine.new(:data, values, title: title)
    end
  else
    @rows << SpreadTableLine.new(:data, line, line_options)
  end
  set_line_type(:header, @options[:header], false)
  set_line_type(:total, @options[:total], false)
  calc_data_size
end

Instance Attribute Details

#data_heightObject (readonly)

Returns the value of attribute data_height.



173
174
175
# File 'lib/rubypivot/spread_table.rb', line 173

def data_height
  @data_height
end

#data_widthObject (readonly)

Returns the value of attribute data_width.



173
174
175
# File 'lib/rubypivot/spread_table.rb', line 173

def data_width
  @data_width
end

#optionsObject

Returns the value of attribute options.



174
175
176
# File 'lib/rubypivot/spread_table.rb', line 174

def options
  @options
end

#rowsObject

Returns the value of attribute rows.



174
175
176
# File 'lib/rubypivot/spread_table.rb', line 174

def rows
  @rows
end

#total_widthObject (readonly)

Returns the value of attribute total_width.



173
174
175
# File 'lib/rubypivot/spread_table.rb', line 173

def total_width
  @total_width
end

Instance Method Details

#add_line(line_type = :data, line = [], line_options = {}) ⇒ Object



221
222
223
224
# File 'lib/rubypivot/spread_table.rb', line 221

def add_line(line_type = :data, line = [], line_options = {})
  @rows << SpreadTableLine.new(line_type, line, line_options)
  calc_data_size
end

#calc_data_sizeObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rubypivot/spread_table.rb', line 244

def calc_data_size
  @total_width = 0
  @data_width = 0
  @data_height = 0
  @rows.each do |row|
    w = row.data_width
    @total_width = w if @total_width < w
    next if row.line_type != :data
    @data_width = w if @data_width < w
    @data_height += 1
  end
  @total_width += 1 # Including title column
  self
end

#eachObject



280
281
282
283
284
# File 'lib/rubypivot/spread_table.rb', line 280

def each
  @rows.each do |row|
    yield row
  end
end

#each_data_lineObject



286
287
288
289
290
291
# File 'lib/rubypivot/spread_table.rb', line 286

def each_data_line
  @rows.each do |row|
    next if row.line_type != :data
    yield row
  end
end

#each_header_lineObject



293
294
295
296
297
298
# File 'lib/rubypivot/spread_table.rb', line 293

def each_header_line
  @rows.each do |row|
    next if row.line_type == :data
    yield row
  end
end

#each_non_header_lineObject



300
301
302
303
304
305
# File 'lib/rubypivot/spread_table.rb', line 300

def each_non_header_line
  @rows.each do |row|
    next if row.line_type == :header
    yield row
  end
end

#get_row(position) ⇒ Object Also known as: line



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/rubypivot/spread_table.rb', line 226

def get_row(position)
  if position.is_a?(Symbol)
    if position == :last
      @rows.last
    else
      @rows.first
    end
  else
    pos = position.to_i
    @rows[pos] if pos >= 0 && pos < @rows.size - 1
  end
end

#set_cell_callback(callback) ⇒ Object



273
274
275
276
277
278
# File 'lib/rubypivot/spread_table.rb', line 273

def set_cell_callback(callback)
  return if callback.nil? || !callback.is_a?(Method)
  each_non_header_line do |line|
    line.set_cell_callback(callback)
  end
end

#set_cell_class(callback) ⇒ Object



259
260
261
262
263
264
# File 'lib/rubypivot/spread_table.rb', line 259

def set_cell_class(callback)
  return unless callback
  each_data_line do |line|
    line.set_cell_class(callback)
  end
end

#set_line_class(klass) ⇒ Object



266
267
268
269
270
271
# File 'lib/rubypivot/spread_table.rb', line 266

def set_line_class(klass)
  return unless klass
  each do |line|
    line.set_line_class(klass)
  end
end

#set_line_type(line_type, position = nil, recalc = true) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rubypivot/spread_table.rb', line 204

def set_line_type(line_type, position = nil, recalc = true)
  return if line_type.nil? || position.nil?
  return unless SpreadTableLine::LINE_TYPES[line_type]
  case position
  when :first, :top
    @rows.first.set_line_type(line_type, @options_for_line)
  when :last, :bottom
    @rows.last.set_line_type(line_type, @options_for_line)
  else
    row = get_row(position)
    if row
      @rows[pos].set_line_type(line_type, @options_for_line)
    end
  end
  calc_data_size if recalc
end

#to_grid(framework = :bootstrap, widths = [], options = {}) ⇒ Object

Bootstrap grid



324
325
326
327
328
329
330
# File 'lib/rubypivot/spread_table.rb', line 324

def to_grid(framework = :bootstrap, widths = [], options = {})
  res = ""
  @rows.each do |row|
    res << row.to_grid(framework, widths)
  end
  res
end

#to_html(options = {}) ⇒ Object



313
314
315
316
317
318
319
320
321
322
# File 'lib/rubypivot/spread_table.rb', line 313

def to_html(options = {})
  line_end = options.delete(:line_end)
  res = HtmlTag.new('table', options).open
  res << "\n" if line_end == :cr
  @rows.each do |row|
    res << row.to_html(line_end: line_end)
  end
  res << "</table>\n"
  res
end

#to_sObject



307
308
309
310
311
# File 'lib/rubypivot/spread_table.rb', line 307

def to_s
  @rows.each do |row|
    puts row.to_s
  end
end

#total_heightObject



240
241
242
# File 'lib/rubypivot/spread_table.rb', line 240

def total_height
  @rows.size
end