Module: Resque::Reports::Extensions::TableBuilding::ClassMethods

Defined in:
lib/resque/reports/extensions/table_building.rb

Overview

Defines table building methods

External(DSL):
  - source
  - table
  - column
Internal:
  - init_table
  - build_table_header
  - build_table_row(row_object)
  - data_each
  - data_size

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#source_methodObject

Returns the value of attribute source_method.



23
24
25
# File 'lib/resque/reports/extensions/table_building.rb', line 23

def source_method
  @source_method
end

Instance Method Details

#add_column_cell(column_value, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/resque/reports/extensions/table_building.rb', line 43

def add_column_cell(column_value, options = {})
  return if @header_collecting

  if column_value.is_a? Symbol
    column_value = @row_object[column_value]
  end

  if (formatter_name = options[:formatter])
    column_value = send("#{formatter_name}_formatter".to_sym, column_value)
  end

  @table_row << encoded_string(column_value)
end

#add_column_header(column_name) ⇒ Object



39
40
41
# File 'lib/resque/reports/extensions/table_building.rb', line 39

def add_column_header(column_name)
  @table_header << encoded_string(column_name) if @header_collecting
end

#build_table_headerObject



69
70
71
72
# File 'lib/resque/reports/extensions/table_building.rb', line 69

def build_table_header
  @header_collecting = true
  @table_block.call(Extensions::Dummy.new)
end

#build_table_row(row_object) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/resque/reports/extensions/table_building.rb', line 57

def build_table_row(row_object)
  @header_collecting = false

  @row_object = row_object.is_a?(Hash) ? row_object.with_indifferent_access : row_object

  row = @table_block.call(@row_object)

  finish_row

  row
end

#column(name, value, options = {}) ⇒ Object



30
31
32
# File 'lib/resque/reports/extensions/table_building.rb', line 30

def column(name, value, options = {})
  add_column_header(name) || add_column_cell(value, options)
end

#data(force = false) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/resque/reports/extensions/table_building.rb', line 83

def data(force = false)
  if force || @data.nil?
    @data = @instance.send(@source_method)
  else
    @data
  end
end

#data_each(force = false) ⇒ Object



91
92
93
94
95
# File 'lib/resque/reports/extensions/table_building.rb', line 91

def data_each(force = false)
  data(force).each do |element|
    yield element
  end
end

#data_sizeObject



97
98
99
# File 'lib/resque/reports/extensions/table_building.rb', line 97

def data_size
  @data_size ||= data.count
end

#encoded_string(obj) ⇒ Object

you may override default string endcoding



75
76
77
# File 'lib/resque/reports/extensions/table_building.rb', line 75

def encoded_string(obj)
  obj.to_s.encode('utf-8', invalid: :replace, undef: :replace)
end

#finish_rowObject



79
80
81
# File 'lib/resque/reports/extensions/table_building.rb', line 79

def finish_row
  @table_row = []
end

#init_tableObject



34
35
36
37
# File 'lib/resque/reports/extensions/table_building.rb', line 34

def init_table
  @table_header = []
  @table_row = []
end

#table(&block) ⇒ Object



26
27
28
# File 'lib/resque/reports/extensions/table_building.rb', line 26

def table(&block)
  @table_block = block
end