Class: HtmlGrid::Grid
- Inherits:
-
Object
- Object
- HtmlGrid::Grid
- Defined in:
- lib/htmlgrid/grid.rb
Defined Under Namespace
Classes: Row
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #[](x, y) ⇒ Object
- #add(arg, x, y, col = false) ⇒ Object
- #add_attribute(key, value, x, y, w = 1, h = 1) ⇒ Object
- #add_background(x, y, w = 1, h = 1) ⇒ Object
- #add_column(arg, x, y) ⇒ Object
- #add_component_style(style, x, y, w = 1, h = 1) ⇒ Object
- #add_field(arg, x, y) ⇒ Object
- #add_row(arg, x, y) ⇒ Object
- #add_style(style, x, y, w = 1, h = 1) ⇒ Object
- #add_tag(tag, x, y, w = 1, h = 1) ⇒ Object
- #each_field(x, y, w = 1, h = 1) ⇒ Object
- #field_attribute(key, x = 0, y = 0) ⇒ Object
-
#initialize(attributes = {}) ⇒ Grid
constructor
Grid.
- #initialize_grid(w, h) ⇒ Object
- #insert_row(y = 0, arg = nil) ⇒ Object
- #push(arg, x = 0, y = height) ⇒ Object
- #set_attribute(key, value) ⇒ Object
- #set_attributes(hash) ⇒ Object
- #set_colspan(x = 0, y = 0, span = (@width - x)) ⇒ Object
- #set_row_attributes(attr = {}, y = 0, offset = 0) ⇒ Object
- #to_html(cgi) ⇒ Object
Constructor Details
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
29 30 31 |
# File 'lib/htmlgrid/grid.rb', line 29 def height @height end |
#width ⇒ Object
Returns the value of attribute width.
29 30 31 |
# File 'lib/htmlgrid/grid.rb', line 29 def width @width end |
Instance Method Details
#[](x, y) ⇒ Object
329 330 331 332 333 |
# File 'lib/htmlgrid/grid.rb', line 329 def [](x, y) @rows[y][x] rescue nil end |
#add(arg, x, y, col = false) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/htmlgrid/grid.rb', line 208 def add(arg, x, y, col = false) if arg.is_a?(Enumerable) if arg.is_a?(String) add_field(arg, x, y) elsif arg.is_a?(Array) arg.each do |item| add_field(item, x || "", y) end elsif col add_column(arg, x, y) else add_row(arg, x, y) end else add_field(arg, x, y) end end |
#add_attribute(key, value, x, y, w = 1, h = 1) ⇒ Object
226 227 228 229 230 |
# File 'lib/htmlgrid/grid.rb', line 226 def add_attribute(key, value, x, y, w = 1, h = 1) each_field(x, y, w, h) { |field| field.add_attribute(key, value) } end |
#add_background(x, y, w = 1, h = 1) ⇒ Object
232 233 234 235 236 |
# File 'lib/htmlgrid/grid.rb', line 232 def add_background(x, y, w = 1, h = 1) each_field(x, y, w, h) { |field| field.add_background(x, w) } end |
#add_column(arg, x, y) ⇒ Object
238 239 240 241 242 243 244 |
# File 'lib/htmlgrid/grid.rb', line 238 def add_column(arg, x, y) offset = 0 arg.each do |item| add_field(item, x, y + offset) offset = offset.next end end |
#add_component_style(style, x, y, w = 1, h = 1) ⇒ Object
246 247 248 249 250 |
# File 'lib/htmlgrid/grid.rb', line 246 def add_component_style(style, x, y, w = 1, h = 1) each_field(x, y, w, h) { |field| field.add_component_style(style) } end |
#add_field(arg, x, y) ⇒ Object
252 253 254 255 |
# File 'lib/htmlgrid/grid.rb', line 252 def add_field(arg, x, y) initialize_grid(x + 1, y + 1) (@rows[y] ||= Row.new).add(arg, x) end |
#add_row(arg, x, y) ⇒ Object
257 258 259 260 261 262 263 |
# File 'lib/htmlgrid/grid.rb', line 257 def add_row(arg, x, y) offset = 0 arg.each do |item| add_field(item, x + offset, y) offset = offset.next end end |
#add_style(style, x, y, w = 1, h = 1) ⇒ Object
265 266 267 268 269 |
# File 'lib/htmlgrid/grid.rb', line 265 def add_style(style, x, y, w = 1, h = 1) each_field(x, y, w, h) { |field| field.add_style(style) } end |
#add_tag(tag, x, y, w = 1, h = 1) ⇒ Object
271 272 273 274 275 276 |
# File 'lib/htmlgrid/grid.rb', line 271 def add_tag(tag, x, y, w = 1, h = 1) initialize_grid(x + w, y + h) each_field(x, y, w, h) { |field| field.tag = tag } end |
#each_field(x, y, w = 1, h = 1) ⇒ Object
278 279 280 281 282 283 284 |
# File 'lib/htmlgrid/grid.rb', line 278 def each_field(x, y, w = 1, h = 1) y.upto([y + h, @height].min - 1) { |yy| @rows[yy].each_field(x, w) { |field| yield(field) } } end |
#field_attribute(key, x = 0, y = 0) ⇒ Object
286 287 288 |
# File 'lib/htmlgrid/grid.rb', line 286 def field_attribute(key, x = 0, y = 0) @rows[y].field_attribute(key, x) end |
#initialize_grid(w, h) ⇒ Object
197 198 199 200 201 202 203 204 205 206 |
# File 'lib/htmlgrid/grid.rb', line 197 def initialize_grid(w, h) if w > @width || h > @height floor = w > @width ? 0 : @height @width = [w, @width].max @height = [h, @height].max floor.upto(@height - 1) { |ii| (@rows[ii] ||= Row.new).initialize_row(@width) } end end |
#insert_row(y = 0, arg = nil) ⇒ Object
290 291 292 293 294 |
# File 'lib/htmlgrid/grid.rb', line 290 def insert_row(y = 0, arg = nil) @rows[y, 0] = Row.new @height += 1 add(arg, 0, y) end |
#push(arg, x = 0, y = height) ⇒ Object
296 297 298 299 |
# File 'lib/htmlgrid/grid.rb', line 296 def push(arg, x = 0, y = height) add(arg, x, y) set_colspan(x, y) end |
#set_attribute(key, value) ⇒ Object
301 302 303 |
# File 'lib/htmlgrid/grid.rb', line 301 def set_attribute(key, value) @attributes[key] = value end |
#set_attributes(hash) ⇒ Object
305 306 307 |
# File 'lib/htmlgrid/grid.rb', line 305 def set_attributes(hash) @attributes.update(hash) end |
#set_colspan(x = 0, y = 0, span = (@width - x)) ⇒ Object
309 310 311 312 313 |
# File 'lib/htmlgrid/grid.rb', line 309 def set_colspan(x = 0, y = 0, span = (@width - x)) span = span.to_i initialize_grid(x + span, y + 1) self[x, y].colspan = span end |
#set_row_attributes(attr = {}, y = 0, offset = 0) ⇒ Object
315 316 317 318 319 320 321 |
# File 'lib/htmlgrid/grid.rb', line 315 def set_row_attributes(attr = {}, y = 0, offset = 0) # TODO # At the moment, offset value is not used # But probably offset value is used in grid.c initialize_grid(offset, y + 1) @rows[y].set_attributes(attr) end |
#to_html(cgi) ⇒ Object
323 324 325 326 327 |
# File 'lib/htmlgrid/grid.rb', line 323 def to_html(cgi) cgi.table(@attributes) { @rows.collect { |row| row.to_html(cgi).force_encoding("utf-8") }.join } end |