module Rapport::Report

Attributes

current_model[RW]
options[RW]
section_data[RW]

Public Class Methods

included(base) click to toggle source
# 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

Public Instance Methods

column_headers() click to toggle source
# File lib/rapport/report.rb, line 74
def column_headers
  @_column_headers ||= columns.map{|c| c[0]}
end
column_symbols() click to toggle source
# File lib/rapport/report.rb, line 78
def column_symbols
  @_column_symbols ||= columns.map{|c| c[1]}
end
columns() click to toggle source
# 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_row() { |formatted_row(model, row_type || format_underscore)| ... } click to toggle source
# 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) click to toggle source
# 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
generate() click to toggle source
# File lib/rapport/report.rb, line 51
def generate
  report_generator.generate
end
raw_cell_value(key, model, row_type) click to toggle source
# 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_generator() click to toggle source
# File lib/rapport/report.rb, line 47
def report_generator
  @_report_generator ||= ReportGenerator.from(self)
end
row_data() click to toggle source
# File lib/rapport/report.rb, line 43
def row_data
  self.class.row_data
end
table_name() click to toggle source
# 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_class() click to toggle source
# File lib/rapport/report.rb, line 82
def to_model_class
  @_to_model_class ||= Struct.new("#{self.class}Model", *column_symbols)
end
to_s() click to toggle source
# File lib/rapport/report.rb, line 99
def to_s
  [self.class, options[:format]].compact.map{|p| p.to_s }.join(' ')
end

Protected Instance Methods

each_model(&block) click to toggle source
# File lib/rapport/report.rb, line 111
def each_model(&block)
  raise "#each_model has not been implemented"
end
section(section_data = {}) { || ... } click to toggle source
# File lib/rapport/report.rb, line 105
def section(section_data = {}, &block)
  self.section_data = section_data
  yield
  self.section_data = nil
end

Private Instance Methods

_calculators() click to toggle source
# File lib/rapport/report.rb, line 126
def _calculators
  @_calculators ||= self.class.send(:instance_variable_get, :@cell_calculators).dup
end
base_calculator(key, calculator) click to toggle source
# File lib/rapport/report.rb, line 163
def base_calculator(key, calculator)
  base_key = key
  if calculator[:map_to]
    return Rapport.safe_proc(calculator[:map_to]) if calculator[:map_to].respond_to?(:call)
    base_key = calculator[:map_to].to_sym
  end
  Rapport.safe_send(base_key)
end
cell_calculator_for(key,row_type) click to toggle source
# File lib/rapport/report.rb, line 130
def cell_calculator_for(key,row_type)
  calculator = _calculators[key] ||= {}

  if calculator[row_type].nil?
    base_calculator = base_calculator(key,calculator)
    calculator_for_row_type = if calculator[:through]
      calculator[:through] = [calculator[:through]] unless calculator[:through].is_a?(Enumerable)
      through_calculator(calculator[:through], base_calculator)
    else
      base_calculator
    end
    calculator[row_type] = calculator_for_row_type
  elsif !calculator[row_type].respond_to?(:call) 
    row_type_key = calculator[row_type]
    calculator[row_type] = Rapport.safe_send(row_type_key)
  end

  calculator[row_type]
end
cell_format_for(key) click to toggle source
# File lib/rapport/report.rb, line 172
def cell_format_for(key)
  _calculators[key][:"format_#{options[:format]}"] || _calculators[key][:format]
end
formatted_row(model, row_type) click to toggle source
# File lib/rapport/report.rb, line 117
def formatted_row(model, row_type)
  self.class.row_data = {}
  out = column_symbols.map do |key|
    formatted_cell_value(key, model, row_type)
  end
  self.class.row_data = {}
  out
end
through_calculator(throughs, base_calculator) click to toggle source
# File lib/rapport/report.rb, line 150
def through_calculator(throughs, base_calculator)
  lambda do |m|
    path = m
    throughs.each do |hint|
      if path.respond_to?(hint)
        new_path = path.send(hint)
        path = new_path unless new_path.nil?
      end
    end
    base_calculator.call(path)
  end      
end