Module: Rapport::Report

Defined in:
lib/rapport/report.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_modelObject

Returns the value of attribute current_model.



41
42
43
# File 'lib/rapport/report.rb', line 41

def current_model
  @current_model
end

#optionsObject

Returns the value of attribute options.



41
42
43
# File 'lib/rapport/report.rb', line 41

def options
  @options
end

#section_dataObject

Returns the value of attribute section_data.



41
42
43
# File 'lib/rapport/report.rb', line 41

def section_data
  @section_data
end

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/rapport/report.rb', line 4

def self.included(base)
  base.extend(ClassMethods)
  base.instance_variable_set(:@columns, [])
  base.instance_variable_set(:@cell_calculators, {})
  def base.inherited(subclass)
    super(subclass)
    subclass.instance_variable_set(:@columns, base.instance_variable_get(:@columns).dup )
    subclass.instance_variable_set(:@cell_calculators, base.instance_variable_get(:@cell_calculators).dup )
  end
end

Instance Method Details

#column_headersObject



74
75
76
# File 'lib/rapport/report.rb', line 74

def column_headers
  @_column_headers ||= columns.map{|c| c[0]}
end

#column_symbolsObject



78
79
80
# File 'lib/rapport/report.rb', line 78

def column_symbols
  @_column_symbols ||= columns.map{|c| c[1]}
end

#columnsObject



66
67
68
69
70
71
72
# File 'lib/rapport/report.rb', line 66

def columns
  if @_columns.nil?
    @_columns = []
    self.class.send(:instance_variable_get, :@columns).each {|col| @_columns << col.dup }
  end
  @_columns
end

#each_rowObject



59
60
61
62
63
64
# File 'lib/rapport/report.rb', line 59

def each_row
  each_model do |model, row_type|
    self.current_model = model # For error reporting
    yield formatted_row(model, row_type || Rapport.format_underscore(model.class.to_s))
  end
end

#formatted_cell_value(key, model, row_type) ⇒ Object



94
95
96
97
# File 'lib/rapport/report.rb', line 94

def formatted_cell_value(key, model, row_type)
  row_data[key] = raw_value = raw_cell_value(key, model, row_type)
  report_generator.format(cell_format_for(key), raw_value)
end

#generateObject



51
52
53
# File 'lib/rapport/report.rb', line 51

def generate
  report_generator.generate
end

#raw_cell_value(key, model, row_type) ⇒ Object



90
91
92
# File 'lib/rapport/report.rb', line 90

def raw_cell_value(key, model, row_type)
  self.row_data[key] = cell_calculator_for(key,row_type).call(model)
end

#report_generatorObject



47
48
49
# File 'lib/rapport/report.rb', line 47

def report_generator
  @_report_generator ||= ReportGenerator.from(self)
end

#row_dataObject



43
44
45
# File 'lib/rapport/report.rb', line 43

def row_data
  self.class.row_data
end

#table_nameObject



86
87
88
# File 'lib/rapport/report.rb', line 86

def table_name
  @table_name ||= options[:report_table_name] || "reports_#{Rapport.format_underscore(self.class).sub(/_?report_?/,'')}"
end

#to_model_classObject



82
83
84
# File 'lib/rapport/report.rb', line 82

def to_model_class
  @_to_model_class ||= Struct.new("#{self.class}Model", *column_symbols)
end

#to_sObject



99
100
101
# File 'lib/rapport/report.rb', line 99

def to_s
  [self.class, options[:format]].compact.map{|p| p.to_s }.join(' ')
end