Class: HTML::Table
- Inherits:
-
Array
- Object
- Array
- HTML::Table
- Extended by:
- Mixin::StrongTyping
- Defined in:
- lib/html/table.rb
Overview
The Table class encapsulates methods associated with an html table element. It is the “outermost” class of the html-table classes.
Defined Under Namespace
Classes: Body, Caption, ColGroup, Content, Foot, Head, Row, TableSection
Constant Summary collapse
- VERSION =
The version of the html-table library
'1.7.1'.freeze
Class Method Summary collapse
-
.global_end_tags=(bool) ⇒ Object
Sets the end tag class variable.
-
.global_end_tags? ⇒ Boolean
Returns true or false, depending on whether or not end tags have been turned on or off, respectively.
-
.html_case ⇒ Object
Returns either “lower” or “upper”, indicating the case of all HTML tags in the final output.
-
.html_case=(arg) ⇒ Object
Sets the case of all HTML tags to either lower or upper.
-
.indent_level ⇒ Object
Returns the number of spaces that tags for this class are indented.
-
.indent_level=(num) ⇒ Object
Sets the number of spaces that tags for this class are indented.
Instance Method Summary collapse
-
#<<(obj) ⇒ Object
This method has been redefined to only allow certain subclasses to be accepted as arguments.
-
#[]=(index, obj) ⇒ Object
This method has been redefined to only allow certain subclasses to be assigned using a direct index notation.
-
#content=(arg) ⇒ Object
(also: #data=)
Adds content to the table.
-
#header(arg = nil) ⇒ Object
A shortcut for creating Table::Row::Header objects in the constructor using the DSL style syntax.
-
#header=(arg) ⇒ Object
Adds a Table::Row::Header object (or an Array of them) to the Table object.
-
#initialize(arg = nil, html_options = {}, &block) ⇒ Table
constructor
Returns a new Table object.
-
#push(*args) ⇒ Object
This method has been redefined to only allow certain subclasses to be accepted as arguments.
-
#unshift(obj) ⇒ Object
This method has been redefined to only allow certain subclasses to be unshifted onto a Table object.
Methods included from Mixin::StrongTyping
Methods included from Mixin::HtmlHandler
Methods included from Mixin::AttributeHandler
#abbr, #abbr=, #align, #align=, #axis, #axis=, #background, #background=, #bgcolor, #bgcolor=, #border, #border=, #bordercolor, #bordercolor=, #bordercolordark, #bordercolordark=, #bordercolorlight, #bordercolorlight=, #cellpadding, #cellpadding=, #cellspacing, #cellspacing=, #char, #char=, #charoff, #charoff=, #class_, #class_=, #col, #col=, #colspan, #colspan=, #configure, #content, #frame, #frame=, #height, #height=, #hspace, #hspace=, #nowrap, #nowrap=, #rowspan, #rowspan=, #rules, #rules=, #span, #span=, #style, #style=, #summary, #summary=, #valign, #valign=, #vspace, #vspace=, #width, #width=
Constructor Details
#initialize(arg = nil, html_options = {}, &block) ⇒ Table
Returns a new Table object. Optionally takes a block which is eval’d if provided. If an argument is provided it is interpreted as content. See the Table#content= method for how that data will be interpreted.
Examples:
# A single data item
HTML::Table.new(1).html
# Output
<table>
<tr>
<td>1</td>
</tr>
</table>
# One row per data item
HTML::Table.new(['Matz', 'Larry', 'Guido']).html
# Output
<table>
<tr>
<td>Matz</td>
</tr>
<tr>
<td>Larry</td>
</tr>
<tr>
<td>Guido</td>
</tr>
</tr>
</table>
# Multiple data items per row
Table.new{ |t|
t.content = [['a','b'], [1,2], ['x','y']]
}.html
# Output
<table>
<tr>
<td>a</td>
<td>b</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>x</td>
<td>y</td>
</tr>
</table>
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/html/table.rb', line 93 def initialize(arg = nil, = {}, &block) @html_begin = '<table' @html_body = '' @html_end = '</table>' instance_eval(&block) if block self.content = arg if arg # Assume html_options are attributes .each do |key, val| send("#{key}=", val) end end |
Class Method Details
.global_end_tags=(bool) ⇒ Object
Sets the end tag class variable. This is used to set whether or not to include optional end tags in the final HTML output. The argument sent to this method must be true or false. The default value is true.
Note that mandatory end tags are unaffected by this setting.
155 156 157 158 |
# File 'lib/html/table.rb', line 155 def self.=(bool) expect(bool, [TrueClass, FalseClass]) = bool end |
.global_end_tags? ⇒ Boolean
Returns true or false, depending on whether or not end tags have been turned on or off, respectively.
145 146 147 |
# File 'lib/html/table.rb', line 145 def self. end |
.html_case ⇒ Object
Returns either “lower” or “upper”, indicating the case of all HTML tags in the final output.
163 164 165 |
# File 'lib/html/table.rb', line 163 def self.html_case @html_case end |
.html_case=(arg) ⇒ Object
Sets the case of all HTML tags to either lower or upper. The only valid arguments to this method are ‘upper’ or ‘lower’.
170 171 172 173 174 175 176 177 178 |
# File 'lib/html/table.rb', line 170 def self.html_case=(arg) expect(arg, String) arg.downcase! unless %w[upper lower].include?(arg) msg = "Argument to html_case() must be 'upper' or 'lower'" raise ArgumentError, msg end @html_case = arg end |
.indent_level ⇒ Object
Returns the number of spaces that tags for this class are indented. For the Table class, the indention level defaults to 0.
Note that each class has its own default indentation level (a multiple of 3).
186 187 188 |
# File 'lib/html/table.rb', line 186 def self.indent_level @indent_level end |
.indent_level=(num) ⇒ Object
Sets the number of spaces that tags for this class are indented.
192 193 194 195 196 |
# File 'lib/html/table.rb', line 192 def self.indent_level=(num) expect(num, Integer) raise ArgumentError, 'indent level must be >= 0' if num < 0 @indent_level = num end |
Instance Method Details
#<<(obj) ⇒ Object
This method has been redefined to only allow certain subclasses to be accepted as arguments.
The restrictions and behavior are identical to the push() method.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/html/table.rb', line 277 def <<(obj) expect(obj, [Caption, ColGroup, Body, Foot, Head, Row, Row::Data, Row::Header]) case obj when Table::Row::Data, Table::Row::Header # Each get their own row self << Table::Row.new(obj) when Table::Caption # Always the first row if self[0].is_a?(Table::Caption) self[0] = obj else unshift(obj) end when Table::Head # Always at row 0 or 1 unshift(obj) if self[0].is_a?(Table::Caption) self[0], self[1] = self[1], self[0] end else super end end |
#[]=(index, obj) ⇒ Object
This method has been redefined to only allow certain subclasses to be assigned using a direct index notation. Specifically, only Caption, ColGroup, Body, Foot, Head and Row objects may be use assigned using direct index notation.
In addition, a Caption can only be assigned to index 0. A Head can only be assigned to index 0, or index 1 if a Caption already exists. A Foot may only be assigned as the last element.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/html/table.rb', line 207 def []=(index, obj) expect(obj, [Caption, ColGroup, Body, Foot, Head, Row]) # Only allow Caption objects at index 0 if index != 0 && obj.is_a?(HTML::Table::Caption) msg = 'CAPTION can only be added at index 0' raise ArgumentError, msg end # Only allow Head objects at index 0 or 1 if obj.is_a?(HTML::Table::Head) if self[0].is_a?(HTML::Table::Caption) && index != 1 msg = 'THEAD must be at index 1 when Caption is included' raise ArgumentError, msg end if !self[0].is_a?(HTML::Table::Caption) && index != 0 msg = 'THEAD must be at index 0 when no Caption is included' raise ArgumentError, msg end end if obj.is_a?(HTML::Table::Foot) && index != -1 msg = 'FOOT must be last element' raise ArgumentError, msg end super end |
#content=(arg) ⇒ Object Also known as: data=
Adds content to the table. How this method behaves depends on the type of argument being passed.
The arg may be a Table::Row object, an Array of Table::Row objects, an Array of Array’s, an Array of Strings, or a single String. In the last two cases, a single Table::Row with a single Table::Row::Data object is created, with the string as the content.
114 115 116 117 118 119 120 |
# File 'lib/html/table.rb', line 114 def content=(arg) if arg.is_a?(Array) arg.each { |e| self << Table::Row.new(e) } else self << Table::Row.new(arg) end end |
#header(arg = nil) ⇒ Object
A shortcut for creating Table::Row::Header objects in the constructor using the DSL style syntax.
127 128 129 |
# File 'lib/html/table.rb', line 127 def header(arg = nil) self.header = arg if arg end |
#header=(arg) ⇒ Object
Adds a Table::Row::Header object (or an Array of them) to the Table object.
134 135 136 137 138 139 140 |
# File 'lib/html/table.rb', line 134 def header=(arg) if arg.is_a?(Array) arg.each { |h| self << Table::Row.new(h, true) } else self << Table::Row::Header.new(arg) end end |
#push(*args) ⇒ Object
This method has been redefined to only allow certain subclasses to be accepted as arguments. Specifically, only Caption, ColGroup, Body, Foot, Head, Row, Row::Data and Row::Header objects may be pushed onto a Table.
Pushing a Data or Header object onto a Table object creates its own row for each. If a Caption object is pushed onto the Table, it will automatically be bumped to the first element. If a Head object is pushed onto the Table, it is automatically bumped to the first element, or the second element if a Caption already exists.
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/html/table.rb', line 248 def push(*args) args.each do |obj| expect(obj, [Caption, ColGroup, Body, Foot, Head, Row, Row::Data, Row::Header]) case obj when Table::Row::Data, Table::Row::Header push(Table::Row.new(obj)) when Table::Caption if self[0].is_a?(Table::Caption) self[0] = obj else unshift(obj) end when Table::Head unshift(obj) if self[0].is_a?(Table::Caption) self[0], self[1] = self[1], self[0] end else super(obj) end end end |
#unshift(obj) ⇒ Object
This method has been redefined to only allow certain subclasses to be unshifted onto a Table object. Specifically, they are Caption, ColGroup, Body, Foot, Head and Row.
303 304 305 306 |
# File 'lib/html/table.rb', line 303 def unshift(obj) expect(obj, [Caption, ColGroup, Body, Foot, Head, Row]) super end |