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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/resque/reports/extensions/table_building.rb', line 50

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

  if column_value.is_a? Symbol
    column_value = if @row_object.respond_to?(column_value)
                     @row_object.public_send(column_value)
                   else
                     @row_object[column_value]
                   end
  end

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

  @table_row << encoded_string(column_value)
end

#add_column_header(column_name) ⇒ Object



46
47
48
# File 'lib/resque/reports/extensions/table_building.rb', line 46

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

#build_table_headerObject



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

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

#build_table_row(row_object) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/resque/reports/extensions/table_building.rb', line 68

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
33
34
35
36
37
38
39
# File 'lib/resque/reports/extensions/table_building.rb', line 30

def column(name, value, options = {})
  if options[:skip_if].present?
    if options[:skip_if].is_a?(Symbol)
      return if @instance.send(options.delete(:skip_if))
    elsif options[:skip_if].respond_to?(:call)
      return if options.delete(:skip_if).call
    end
  end
  add_column_header(name) || add_column_cell(value, options)
end

#data(force = false) ⇒ Object



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

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

#data_each(force = false) ⇒ Object



102
103
104
105
106
# File 'lib/resque/reports/extensions/table_building.rb', line 102

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

#data_sizeObject



108
109
110
# File 'lib/resque/reports/extensions/table_building.rb', line 108

def data_size
  @data_size ||= data.count
end

#encoded_string(obj) ⇒ Object

you may override default string endcoding



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

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

#finish_rowObject



90
91
92
# File 'lib/resque/reports/extensions/table_building.rb', line 90

def finish_row
  @table_row = []
end

#init_tableObject



41
42
43
44
# File 'lib/resque/reports/extensions/table_building.rb', line 41

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