Class: Rapport::CellFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rapport/report_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(procs = {}) ⇒ CellFormatter

Returns a new instance of CellFormatter.



75
76
77
# File 'lib/rapport/report_generator.rb', line 75

def initialize(procs = {})
  @procs = procs
end

Instance Method Details

#add_cell_format(type, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rapport/report_generator.rb', line 83

def add_cell_format(type, &block)
  proc = Proc.new(&block)
  @procs[type] = proc
  @procs[Rapport.format_underscore(type.to_s)] = proc if type.is_a?(Class)
  
  # Support ActiveSupport::TimeWithZone automatically
  if type == Time
    add_cell_format(ActiveSupport::TimeWithZone, &block) rescue nil
  end
  
end

#dupObject



79
80
81
# File 'lib/rapport/report_generator.rb', line 79

def dup
  CellFormatter.new(@procs.dup)
end

#format(type, value) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/rapport/report_generator.rb', line 95

def format(type,value)     
  if type.is_a?(Proc)
    type.call(value)
  else 
    proc = @procs[type] || @procs[value.class]
    proc.nil? ? value : proc.call(value)
  end
end