Class: HTML::Table
- Inherits:
-
Array
- Object
- Array
- HTML::Table
- Includes:
- AttributeHandler, HtmlHandler
- 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.3.6'
true
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, &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 HtmlHandler
Methods included from 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, &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>
89 90 91 92 93 94 95 |
# File 'lib/html/table.rb', line 89 def initialize(arg = nil, &block) @html_begin = '<table' @html_body = '' @html_end = '</table>' instance_eval(&block) if block self.content = arg if arg 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.
146 147 148 149 |
# File 'lib/html/table.rb', line 146 def self.(bool) expect(bool, [TrueClass, FalseClass]) @@global_end_tags = bool end |
.global_end_tags? ⇒ Boolean
Returns true or false, depending on whether or not end tags have been turned on or off, respectively.
136 137 138 |
# File 'lib/html/table.rb', line 136 def self. @@global_end_tags end |
.html_case ⇒ Object
Returns either “lower” or “upper”, indicating the case of all HTML tags in the final output.
154 155 156 |
# File 'lib/html/table.rb', line 154 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’.
161 162 163 164 165 166 167 168 169 |
# File 'lib/html/table.rb', line 161 def self.html_case=(arg) expect(arg, String) arg.downcase! unless arg == "upper" || arg == "lower" 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).
177 178 179 |
# File 'lib/html/table.rb', line 177 def self.indent_level @indent_level end |
.indent_level=(num) ⇒ Object
Sets the number of spaces that tags for this class are indented.
183 184 185 186 187 |
# File 'lib/html/table.rb', line 183 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.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/html/table.rb', line 270 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].kind_of?(Table::Caption) self[0] = obj else self.unshift(obj) end when Table::Head # Always at row 0 or 1 if self[0].kind_of?(Table::Caption) self.unshift(obj) self[0], self[1] = self[1], self[0] else self.unshift(obj) end else super(obj) 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.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/html/table.rb', line 198 def []=(index,obj) expect(obj, [Caption, ColGroup, Body, Foot, Head, Row]) # Only allow Caption objects at index 0 if index != 0 && obj.kind_of?(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.kind_of?(HTML::Table::Head) if self[0].kind_of?(HTML::Table::Caption) && index != 1 msg = "THEAD must be at index 1 when Caption is included" raise ArgumentError, msg end if !self[0].kind_of?(HTML::Table::Caption) && index != 0 msg = "THEAD must be at index 0 when no Caption is included" raise ArgumentError, msg end end if obj.kind_of?(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.
105 106 107 108 109 110 111 |
# File 'lib/html/table.rb', line 105 def content=(arg) if arg.kind_of?(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.
118 119 120 |
# File 'lib/html/table.rb', line 118 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.
125 126 127 128 129 130 131 |
# File 'lib/html/table.rb', line 125 def header=(arg) if arg.kind_of?(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.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/html/table.rb', line 237 def push(*args) args.each{ |obj| expect(obj, [Caption, ColGroup, Body, Foot, Head, Row, Row::Data, Row::Header] ) case obj when Table::Row::Data, Table::Row::Header self.push(Table::Row.new(obj)) when Table::Caption if self[0].kind_of?(Table::Caption) self[0] = obj else self.unshift(obj) end when Table::Head if self[0].kind_of?(Table::Caption) self.unshift(obj) self[0],self[1] = self[1],self[0] else self.unshift(obj) end else super(obj) 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.
300 301 302 303 |
# File 'lib/html/table.rb', line 300 def unshift(obj) expect(obj, [Caption, ColGroup, Body, Foot, Head, Row]) super end |