Class: Munger::Render::Html

Inherits:
Object
  • Object
show all
Defined in:
lib/munger/render/html.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report, options = {}) ⇒ Html

Returns a new instance of Html.



14
15
16
17
# File 'lib/munger/render/html.rb', line 14

def initialize(report, options = {})
  @report = report
  set_classes(options[:classes])
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



12
13
14
# File 'lib/munger/render/html.rb', line 12

def classes
  @classes
end

#reportObject (readonly)

Returns the value of attribute report.



12
13
14
# File 'lib/munger/render/html.rb', line 12

def report
  @report
end

Instance Method Details

#cycle(one, two) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/munger/render/html.rb', line 107

def cycle(one, two)
  if @current == one
    @current = two
  else
    @current = one
  end
end

#renderObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/munger/render/html.rb', line 25

def render
  x = Builder::XmlMarkup.new
  
  x.table(:class => @classes[:table]) do
    
    x.thead do
      x.tr do
        @report.columns.each do |column|
          x.th(:class => 'columnTitle') { x << @report.column_title(column) }
        end
      end
    end
    
    x.tbody do
      @report.process_data.each do |row|
      
        classes = []
        classes << row[:meta][:row_styles]
        classes << 'group' + row[:meta][:group].to_s if row[:meta][:group]
        classes << cycle('even', 'odd')
        classes.compact!

        if row[:meta][:group_header]
          classes << 'groupHeader' + row[:meta][:group_header].to_s 
        end
      
        row_attrib = {}
        row_attrib = {:class => classes.join(' ')} if classes.size > 0
      
        if row[:meta][:group_header]
          x.thead do
            x.tr(row_attrib) do
              header = @report.column_title(row[:meta][:group_name]) + ' : ' + row[:meta][:group_value].to_s
              x.th(:colspan => @report.columns.size) { x << header }
            end
          end
        else
          x.tr(row_attrib) do
            @report.columns.each do |column|
              
              cell_attrib = {}
              if cst = row[:meta][:cell_styles]
                cst = Item.ensure(cst)
                if cell_styles = cst[column]
                  cell_attrib = {:class => cell_styles.join(' ')}
                end
              end
              
              # x.td(cell_attrib) { x << row[:data][column].to_s }
              # TODO: Clean this up, I don't like it but it's working
              # output the cell
              # x.td(cell_attrib) { x << row[:data][column].to_s }
              x.td(cell_attrib) do
                formatter,*args = *@report.column_formatter(column)
                col_data = row[:data] #[column]
                if formatter && col_data[column]
                  formatted = if formatter.class == Proc
                    data = col_data.respond_to?(:data) ? col_data.data : col_data
                    formatter.call(data)
                  elsif col_data[column].respond_to? formatter
                    col_data[column].send(formatter, *args)
                  elsif
                    col_data[column].to_s
                  end
                else
                  formatted = col_data[column].to_s
                end
                x << formatted.to_s
              end
              
            end # columns
          end # x.tr
        end
        
      end # rows
      
    end # x.tbody
    
  end # x.table
  
end

#set_classes(options = nil) ⇒ Object



19
20
21
22
23
# File 'lib/munger/render/html.rb', line 19

def set_classes(options = nil)
  options = {} if !options
  default = {:table => 'report-table'}
  @classes = default.merge(options)
end

#valid?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/munger/render/html.rb', line 115

def valid?
  @report.is_a? Munger::Report
end