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
283 284 285 286 287 288 289 |
# File 'lib/htmlgrid/grid.rb', line 283 def [](x, y) begin @rows[y][x] rescue StandardError nil end end |
#add(arg, x, y, col = false) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/htmlgrid/grid.rb', line 180 def add(arg, x, y, col=false) if arg.kind_of?(Enumerable) if arg.kind_of?(String) add_field(arg, x, y) elsif arg.kind_of?(Array) arg.each do |item| add_field(item, x ? 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
197 198 199 200 201 |
# File 'lib/htmlgrid/grid.rb', line 197 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
202 203 204 205 206 |
# File 'lib/htmlgrid/grid.rb', line 202 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
207 208 209 210 211 212 213 |
# File 'lib/htmlgrid/grid.rb', line 207 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
214 215 216 217 218 |
# File 'lib/htmlgrid/grid.rb', line 214 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
219 220 221 222 |
# File 'lib/htmlgrid/grid.rb', line 219 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
223 224 225 226 227 228 229 |
# File 'lib/htmlgrid/grid.rb', line 223 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
230 231 232 233 234 |
# File 'lib/htmlgrid/grid.rb', line 230 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
235 236 237 238 239 240 |
# File 'lib/htmlgrid/grid.rb', line 235 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
241 242 243 244 245 246 247 |
# File 'lib/htmlgrid/grid.rb', line 241 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
248 249 250 |
# File 'lib/htmlgrid/grid.rb', line 248 def field_attribute(key, x=0, y=0) @rows[y].field_attribute(key, x) end |
#initialize_grid(w, h) ⇒ Object
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/htmlgrid/grid.rb', line 170 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
251 252 253 254 255 |
# File 'lib/htmlgrid/grid.rb', line 251 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
256 257 258 259 |
# File 'lib/htmlgrid/grid.rb', line 256 def push(arg, x=0, y=height) add(arg, x, y) set_colspan(x,y) end |
#set_attribute(key, value) ⇒ Object
260 261 262 |
# File 'lib/htmlgrid/grid.rb', line 260 def set_attribute(key, value) @attributes[key] = value end |
#set_attributes(hash) ⇒ Object
263 264 265 |
# File 'lib/htmlgrid/grid.rb', line 263 def set_attributes(hash) @attributes.update(hash) end |
#set_colspan(x = 0, y = 0, span = (@width - x)) ⇒ Object
266 267 268 269 270 |
# File 'lib/htmlgrid/grid.rb', line 266 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
271 272 273 274 275 276 277 |
# File 'lib/htmlgrid/grid.rb', line 271 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
278 279 280 281 282 |
# File 'lib/htmlgrid/grid.rb', line 278 def to_html(cgi) cgi.table(@attributes) { @rows.collect { |row| row.to_html(cgi).force_encoding('utf-8') }.join } end |