Class: Rubypivot::SpreadTableLine

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypivot/spread_table.rb

Constant Summary collapse

LINE_TYPES =
{
  header: 'Header',
  data:   'Data  ',
  total:  'Total ',
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_type, line_data = [], options = {}) ⇒ SpreadTableLine

Returns a new instance of SpreadTableLine.



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
# File 'lib/rubypivot/spread_table.rb', line 26

def initialize(line_type, line_data = [], options = {})
  @options = options
  @attribute = {}
  @attribs = []
  set_line_type(line_type, @options)
  # Desparate to have an array, convert it if not
  if line_data.is_a?(Array)
    @line_data = line_data
  elsif line_data.is_a?(Hash)
    @line_data = []
    line_data.each do |key, value|
      @line_data << value
    end
  elsif line_data.is_a?(String)
    @line_data = line_data.split(/[ \t]+/)
  else
    @line_data = [line_data]
  end
  if options[:title]
    if options[:title] == :first
      @title = @line_data.shift
    else
      @title = options[:title].to_s
    end
  end
end

Instance Attribute Details

#attributObject

Returns the value of attribute attribut.



25
26
27
# File 'lib/rubypivot/spread_table.rb', line 25

def attribut
  @attribut
end

#line_dataObject

Returns the value of attribute line_data.



25
26
27
# File 'lib/rubypivot/spread_table.rb', line 25

def line_data
  @line_data
end

#line_typeObject (readonly)

Returns the value of attribute line_type.



24
25
26
# File 'lib/rubypivot/spread_table.rb', line 24

def line_type
  @line_type
end

#optionsObject (readonly)

Returns the value of attribute options.



24
25
26
# File 'lib/rubypivot/spread_table.rb', line 24

def options
  @options
end

#titleObject

Returns the value of attribute title.



25
26
27
# File 'lib/rubypivot/spread_table.rb', line 25

def title
  @title
end

Instance Method Details

#data_widthObject



70
71
72
# File 'lib/rubypivot/spread_table.rb', line 70

def data_width
  @line_data.size
end

#line_data_formattedObject



109
110
111
112
113
114
115
116
# File 'lib/rubypivot/spread_table.rb', line 109

def line_data_formatted
  return @line_data if @line_type == :header || @attribute[:data_format].nil?
  res = []
  @line_data.each do |data|
    res << @attribute[:data_format] % data
  end
  res
end

#make_col_class(options = {}) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/rubypivot/spread_table.rb', line 149

def make_col_class(options = {})
  klass = "col"
  klass << "-#{@attribute[:line_class]}" if @attribute[:line_class]
  klass << "-#{options[:width]}" if options[:width]
  # klass << " #{options[:klass]}" if options[:klass]
  klass
end

#set_cell_callback(callback) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/rubypivot/spread_table.rb', line 100

def set_cell_callback(callback)
  return if callback.nil? || !callback.is_a?(Method) 
  if @line_type == :header
    @attribute[:cell_callback] = callback
  else
    @attribute[:cell_callback] = callback
  end
end

#set_cell_class(callback) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubypivot/spread_table.rb', line 86

def set_cell_class(callback)
  return unless callback
  if callback.is_a?(Method)
    @line_data.each_with_index do |cell_data, idx|
      klass = callback.call(cell_data, @title)
      @attribs[idx] = klass if klass
    end
  else
    @line_data.each_with_index do |cell_data, idx|
      @attribs[idx] = callback.to_s
    end
  end
end

#set_line_class(klass) ⇒ Object



74
75
76
# File 'lib/rubypivot/spread_table.rb', line 74

def set_line_class(klass)
  @attribute[:line_class] = klass
end

#set_line_type(line_type, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubypivot/spread_table.rb', line 53

def set_line_type(line_type, options = {})
  @line_type = line_type
  @line_type = :data unless LINE_TYPES[@line_type] # at least vaid must be set
  if @line_type == :data
    @attribute[:line_class] = options[:data_line_class] if options[:data_line_class]
    @attribute[:title_class] = options[:data_title_class] if options[:data_title_class]
  else
    @attribute[:line_class] = options[:header_line_class] if options[:header_line_class]
    @attribute[:title_class] = options[:header_title_class] if options[:header_title_class]
  end
  if @line_type == :header
    #
  else
    @attribute[:data_format] = options[:data_format] if options[:data_format]
  end
end

#set_td_class(klass) ⇒ Object



78
79
80
# File 'lib/rubypivot/spread_table.rb', line 78

def set_td_class(klass)
  @attribute[:cell_class] = klass
end

#set_title_class(klass) ⇒ Object



82
83
84
# File 'lib/rubypivot/spread_table.rb', line 82

def set_title_class(klass)
  @attribute[:cell_class] = klass
end

#to_grid(framework = :bootstrap, widths = [], options = {}) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/rubypivot/spread_table.rb', line 157

def to_grid(framework = :bootstrap, widths = [], options = {})
  res = "<div class=\"row\">"
  if @title
    div = Rubypivot::HtmlTag.new('div', class: make_col_class(width: widths[0])) # klass: @attribute[:title_class]
    res << div.build{ @title } 
  end
  line_data_formatted.each_with_index do |cell_data, idx|
    div = Rubypivot::HtmlTag.new('div', class: make_col_class(width: widths[idx + 1])) # klass: @attribute[:title_class]
    res << div.build{ cell_data }
  end
  res << "</div>/n"
  res
end

#to_html(options = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rubypivot/spread_table.rb', line 129

def to_html(options = {})
  tr = Rubypivot::HtmlTag.new('tr', class: @attribute[:line_class])
  td_str = ""
  if @title
    td = Rubypivot::HtmlTag.new('td', class: @attribute[:title_class] || @attribute[:cell_class])
    td_str << td.build{ @title } 
  end
  line_data_formatted.each_with_index do |cell_data, idx|
    if @attribute[:cell_callback]
      td_str << @attribute[:cell_callback].call(cell_data, @title)
    else
      td = Rubypivot::HtmlTag.new('td', class: @attribs[idx])
      td_str << td.build{ cell_data }
    end
  end
  res = tr.build { td_str }
  res << "\n" if options[:line_end] == :cr
  res
end

#to_sObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/rubypivot/spread_table.rb', line 118

def to_s
  res = ""
  res << "#{LINE_TYPES[@line_type]}: "
  res << "#{@title}: " if @title
  res << line_data_formatted.join(', ')
  res << " : Line Class: #{@attribute[:line_class]}"
  res << " : Cell Class: #{@attribute[:cell_class]}"
  res << " : Title Class: #{@attribute[:title_class]}"
  res
end