11# The HTML module is a namespace only.
22module HTML
3-
43 # The Table::Row class is a subclass of array that encapsulates an
54 # html table row, i.e. <tr> element.
65 class Table ::Row < Array
@@ -12,9 +11,13 @@ class Table::Row < Array
1211 @indent_level = 3
1312 @end_tags = true
1413
15- # Returns a new Table::Row object. Optionally takes a block. If +arg+
16- # is provided it is treated as content. If +header+ is false, that
17- # content is transformed into a Row::Data object. Otherwise, it is
14+ # Gets and sets whether or not content is converted into a Row::Header
15+ # object or a Row::Data object.
16+ attr_accessor :header
17+
18+ # Returns a new Table::Row object. Optionally takes a block. If +arg+
19+ # is provided it is treated as content. If +header+ is false, that
20+ # content is transformed into a Row::Data object. Otherwise, it is
1821 # converted into a Row::Header object.
1922 #
2023 # See the # Table::Row#content= method for more information.
@@ -24,9 +27,7 @@ class Table::Row < Array
2427 def initialize ( arg = nil , header = false , &block )
2528 @html_begin = '<tr'
2629 @html_end = '</tr>'
27-
2830 @header = header
29-
3031 instance_eval ( &block ) if block_given?
3132 self . content = arg if arg
3233 end
@@ -38,13 +39,6 @@ def header?
3839 @header
3940 end
4041
41- # Sets whether or not content is converted into a Row::Header object
42- # or a Row::Data object.
43- #
44- def header = ( bool )
45- @header = bool
46- end
47-
4842 # Adds content to the Row object.
4943 #
5044 # Because a Row object doesn't store any of its own content, the
@@ -70,27 +64,8 @@ def header=(bool)
7064 # row.html => <tr><th>foo</th></tr>
7165 #
7266 def content = ( arg )
73- case arg
74- when String
75- if @header
76- push ( Table ::Row ::Header . new ( arg ) )
77- else
78- push ( Table ::Row ::Data . new ( arg ) )
79- end
80- when Array
81- arg . each do |e |
82- if e . is_a? ( Table ::Row ::Data ) || e . is_a? ( Table ::Row ::Header )
83- push ( e )
84- else
85- if @header
86- push ( Table ::Row ::Header . new ( e ) )
87- else
88- push ( Table ::Row ::Data . new ( e ) )
89- end
90- end
91- end
92- else
93- push ( arg )
67+ Array ( arg ) . each do |e |
68+ push_content ( e )
9469 end
9570 end
9671
@@ -105,7 +80,7 @@ def self.indent_level
10580 #
10681 def self . indent_level = ( num )
10782 expect ( num , Integer )
108- raise ArgumentError if num < 0
83+ raise ArgumentError , "Indent level must be non-negative" if num < 0
10984 @indent_level = num
11085 end
11186
@@ -118,7 +93,7 @@ def self.end_tags?
11893 end
11994
12095 # Sets the behavior for whether or not the end tags for this class,
121- # </tr>, are included for each row or not. Only true and false are
96+ # </tr>, are included for each row or not. Only true and false are
12297 # valid arguments.
12398 #
12499 def self . end_tags = ( bool )
@@ -127,68 +102,60 @@ def self.end_tags=(bool)
127102 end
128103
129104 # This method has been redefined to only allow certain classes to be
130- # accepted as arguments. Specifically, they are Data and Header. An
131- # Array is also valid, but only if it only includes Data or Header
132- # objects.
105+ # accepted as arguments. Specifically, they are Data and Header. An
106+ # Array is also valid, but only if it only includes Data or Header objects.
133107 #
134108 def []=( index , obj )
135- if obj . is_a? ( Array )
136- obj . each { |o | expect ( o , [ Data , Header ] ) }
137- else
138- expect ( obj , [ Data , Header ] )
139- end
109+ objs = obj . is_a? ( Array ) ? obj : [ obj ]
110+ objs . each { |o | expect ( o , [ Data , Header ] ) }
140111 super
141112 end
142113
143114 # This method has been redefined to only allow certain classes to be
144- # accepted as arguments. Specifically, they are String, Integer,
145- # Data and Header.
115+ # accepted as arguments. Specifically, they are String, Integer, Data ,
116+ # and Header.
146117 #
147118 # A plain string or number pushed onto a Row is automatically
148119 # converted to a Data object.
149120 #
150121 def push ( *args )
151- args . each do |obj |
152- if obj . is_a? ( String ) || obj . is_a? ( Integer )
153- td = Table ::Row ::Data . new ( obj . to_s )
154- super ( td )
155- next
156- else
157- expect ( obj , [ Data , Header ] )
158- end
159- super ( obj )
160- end
122+ args . each { |obj | super ( convert_content ( obj ) ) }
161123 end
162124
163125 # This method has been redefined to only allow certain classes to be
164- # accepted as arguments. The rules are the same as they are for
165- # Row#push.
126+ # accepted as arguments. The rules are the same as they are for Row#push.
166127 #
167128 def <<( obj )
168- if obj . is_a? ( String ) || obj . is_a? ( Integer )
169- td = Table ::Row ::Data . new ( obj . to_s )
170- super ( td )
171- else
172- expect ( obj , [ Data , Header ] )
173- end
174- super
129+ super ( convert_content ( obj ) )
175130 end
176131
177132 # This method has been redefined to only allow certain classes to be
178- # accepted as arguments. The rules are the same as they are for
179- # Row#push.
133+ # accepted as arguments. The rules are the same as they are for Row#push.
180134 #
181135 def unshift ( obj )
136+ super ( convert_content ( obj ) )
137+ end
138+
139+ alias to_s html
140+ alias to_str html
141+
142+ private
143+
144+ def push_content ( e )
145+ if e . is_a? ( Table ::Row ::Data ) || e . is_a? ( Table ::Row ::Header )
146+ push ( e )
147+ else
148+ push ( @header ? Table ::Row ::Header . new ( e ) : Table ::Row ::Data . new ( e ) )
149+ end
150+ end
151+
152+ def convert_content ( obj )
182153 if obj . is_a? ( String ) || obj . is_a? ( Integer )
183- td = Table ::Row ::Data . new ( obj . to_s )
184- super ( td )
154+ Table ::Row ::Data . new ( obj . to_s )
185155 else
186156 expect ( obj , [ Data , Header ] )
157+ obj
187158 end
188- super
189159 end
190-
191- alias to_s html
192- alias to_str html
193160 end
194161end
0 commit comments