Class: UU::DataTable::DataTableModel
- Inherits:
-
Object
- Object
- UU::DataTable::DataTableModel
- Includes:
- UU::DataTable
- Defined in:
- uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb
Overview
DataTableModel class is used for editing DataTable data.
Defined Under Namespace
Classes: ColumnType, Property
Instance Method Summary (collapse)
-
- (Numeric) add_column(label, type = nil)
Adds a new column to the DataTable, and returns the index of the new column.
-
- (Numeric) add_row(row)
Adds a new row to the end of DataTable, and returns the index of the new row.
-
- (void) clear
Removes all rows and columns.
-
- (void) clear_rows
Removes all rows.
-
- (DataTableModel) clone
Returns copy of data representation as DataTableModel.
-
- (String) get_column_label(index)
Returns the label of column at specific index.
-
- (String) get_column_type(index)
Returns the type of column at specific index.
-
- (Object?) get_property(row_index, column_index, property_name)
Returns named property of the cell at the given row and column indexes.
-
- (Hash) get_row(index)
Returns row object on specific index.
-
- (Array) get_row_values(index)
Returns array of row values on specific index.
-
- (Object) get_value(row_index, column_index)
Returns the value of the cell at the given row and column indexes.
-
- (DataTableModel) initialize(data = nil)
constructor
Creates new instance of DataTableModel.
-
- (void) insert_column(index, label, type = nil)
Inserts a new column to the DataTable at specific index.
-
- (void) insert_row(index, row)
Inserts a new row to the DataTable at specific index.
-
- (Numeric) number_of_columns
Returns number of columns in DataTable.
-
- (Numeric) number_of_rows
Returns number of rows in DataTable.
-
- (void) remove_column(index)
Removes the column at specific index.
-
- (void) remove_row(index)
Removes the row at specific index.
-
- (void) set_column_label(index, label)
Sets label of column at specific index.
-
- (void) set_column_type(index, type = nil)
Sets type of column at specific index.
-
- (void) set_property(row_index, column_index, property_name, value)
Sets named property of the cell at the given row and column indexes.
-
- (void) set_row(index, row)
Set row at specific index, the original row on index will be overwritten.
-
- (void) set_value(row_index, column_index, value)
Sets value to the cell at the given row and column indexes.
-
- (Numeric) size
Returns number of rows in DataTable.
-
- (Array) to_a
Returns copy of data representation as Array in Array Format.
-
- (String) to_json(options = {})
Returns data representation as JSON String in DataTable format.
-
- (String) to_s
Returns data representation as JSON String in DataTable format.
Constructor Details
- (DataTableModel) initialize(data = nil)
Creates new instance of DataTableModel.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 88 def initialize(data = nil) if data.kind_of?String init_string(data) elsif data.kind_of?Hash init_hash(data) elsif data.kind_of?Array init_array(data) elsif data.kind_of?DataTableModel @json = data.to_h elsif data.nil? @json = empty_data else raise ArgumentError.new("DataTableModel data must be JSON String, Array, Hash or DataTableModel, but was #{data.class}.") end # TODO validace end |
Instance Method Details
- (Numeric) add_column(label, type = nil)
Adds a new column to the DataTable, and returns the index of the new column. All cells in a new column are filled with nil values.
218 219 220 221 222 223 224 225 226 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 218 def add_column(label, type = nil) if type.nil? @json[:cols] << {:label => label } else @json[:cols] << {:label => label, :type => type } end adjust_rows_add return @json[:cols].size - 1 end |
- (Numeric) add_row(row)
Adds a new row to the end of DataTable, and returns the index of the new row.
383 384 385 386 387 388 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 383 def add_row(row) parsed_row = parse_row(row) validate_row(parsed_row) @json[:rows] << parsed_row return @json[:rows].size - 1 end |
- (void) clear
This method returns an undefined value.
Removes all rows and columns.
617 618 619 620 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 617 def clear @json = empty_data return end |
- (void) clear_rows
This method returns an undefined value.
Removes all rows.
634 635 636 637 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 634 def clear_rows @json[:rows] = [] return end |
- (DataTableModel) clone
Returns copy of data representation as DataTableModel.
735 736 737 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 735 def clone return DataTableModel.new(to_h) end |
- (String) get_column_label(index)
Returns the label of column at specific index.
270 271 272 273 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 270 def get_column_label(index) check_boundaries(index,"index",number_of_columns,"number_of_columns", "columns") return deep_copy(@json[:cols][index][:label]) end |
- (String) get_column_type(index)
Returns the type of column at specific index.
289 290 291 292 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 289 def get_column_type(index) check_boundaries(index,"index",number_of_columns,"number_of_columns", "columns") return deep_copy(@json[:cols][index][:type]) end |
- (Object?) get_property(row_index, column_index, property_name)
Returns named property of the cell at the given row and column indexes.
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 500 def get_property(row_index, column_index, property_name) check_boundaries(row_index,"row_index",size,"size","rows") check_boundaries(column_index,"column_index",number_of_columns,"number_of_columns", "columns") if !property_name.kind_of?String raise ArgumentError.new("Parameter property_name must be String, but was #{property_name.class}.") end row = @json[:rows][row_index] column = row[:c][column_index] props = column[:p] if props.nil? return end return props[property_name.to_sym] end |
- (Hash) get_row(index)
Returns row object on specific index. Row object is Hash with Ruby Symbols used as keys. Modification of the returned hash object has no influence on the original data of DataTable.
123 124 125 126 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 123 def get_row(index) check_boundaries(index,"index",size,"size", "rows") return deep_copy(@json[:rows][index]) end |
- (Array) get_row_values(index)
Returns array of row values on specific index. Modification of the returned array object has no influence on the original data of DataTable.
143 144 145 146 147 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 143 def get_row_values(index) check_boundaries(index,"index",size,"size", "rows") row = deep_copy(@json[:rows][index]) return row_hash_to_array(row) end |
- (Object) get_value(row_index, column_index)
Returns the value of the cell at the given row and column indexes.
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 166 def get_value(row_index, column_index) check_boundaries(row_index,"row_index",size,"size","rows") check_boundaries(column_index,"column_index",number_of_columns,"number_of_columns", "columns") row = @json[:rows][row_index] if row.nil? return end column = row[:c][column_index] if column.nil? return end return deep_copy(column[:v]) end |
- (void) insert_column(index, label, type = nil)
This method returns an undefined value.
Inserts a new column to the DataTable at specific index. All existing columns from specific index to the end are shifted to right (index is increased by one). All cells in a new column are filled with nil values.
245 246 247 248 249 250 251 252 253 254 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 245 def insert_column(index, label, type = nil) check_boundaries(index,"index",number_of_columns,"number_of_columns", "columns") if type.nil? @json[:cols].insert(index, {:label => label }) else @json[:cols].insert(index, {:label => label, :type => type }) end adjust_rows_insert(index) return end |
- (void) insert_row(index, row)
This method returns an undefined value.
Inserts a new row to the DataTable at specific index. All existing row from specific index to the end are shifted (index is increased by one).
416 417 418 419 420 421 422 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 416 def insert_row(index, row) check_boundaries(index,"index",size,"size", "rows") parsed_row = parse_row(row) validate_row(parsed_row, index) @json[:rows].insert(index, parsed_row) return end |
- (Numeric) number_of_columns
Returns number of columns in DataTable. May be zero.
585 586 587 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 585 def number_of_columns return @json[:cols].size end |
- (Numeric) number_of_rows
Returns number of rows in DataTable. May be zero.
596 597 598 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 596 def number_of_rows return @json[:rows].size end |
- (void) remove_column(index)
This method returns an undefined value.
Removes the column at specific index. All existing columns after specific index are shifted to left (index is decreased by one).
354 355 356 357 358 359 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 354 def remove_column(index) check_boundaries(index,"index",number_of_columns,"number_of_columns", "columns") @json[:cols].delete_at(index) adjust_rows_delete(index) return end |
- (void) remove_row(index)
This method returns an undefined value.
Removes the row at specific index. All existing row after specific index are shifted (index is decreased by one).
472 473 474 475 476 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 472 def remove_row(index) check_boundaries(index,"index",size,"size", "rows") @json[:rows].delete_at(index) return end |
- (void) set_column_label(index, label)
This method returns an undefined value.
Sets label of column at specific index.
309 310 311 312 313 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 309 def set_column_label(index, label) check_boundaries(index,"index",number_of_columns,"number_of_columns", "columns") @json[:cols][index][:label] = label return end |
- (void) set_column_type(index, type = nil)
This method returns an undefined value.
Sets type of column at specific index.
333 334 335 336 337 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 333 def set_column_type(index, type = nil) check_boundaries(index,"index",number_of_columns,"number_of_columns", "columns") @json[:cols][index][:type] = type return end |
- (void) set_property(row_index, column_index, property_name, value)
This method returns an undefined value.
Sets named property of the cell at the given row and column indexes.
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 540 def set_property(row_index, column_index, property_name, value) check_boundaries(row_index,"row_index",size,"size","rows") check_boundaries(column_index,"column_index",number_of_columns,"number_of_columns", "columns") if !property_name.kind_of?String raise ArgumentError.new("Parameter property_name must be String, but was #{property_name.class}.") end row = @json[:rows][row_index] column = row[:c][column_index] props = column[:p] if props.nil? props = {} column[:p] = props end if value.nil? props.delete(property_name.to_sym) else props[property_name.to_sym] = value end if props.size == 0 column.delete(:p) end return end |
- (void) set_row(index, row)
This method returns an undefined value.
Set row at specific index, the original row on index will be overwritten.
449 450 451 452 453 454 455 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 449 def set_row(index, row) check_boundaries(index,"index",size,"size", "rows") parsed_row = parse_row(row) validate_row(parsed_row, index) @json[:rows][index] = parsed_row return end |
- (void) set_value(row_index, column_index, value)
This method returns an undefined value.
Sets value to the cell at the given row and column indexes.
197 198 199 200 201 202 203 204 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 197 def set_value(row_index, column_index, value) check_boundaries(row_index,"row_index",size,"size","rows") check_boundaries(column_index,"column_index",number_of_columns,"number_of_columns", "columns") row = @json[:rows][row_index] row[:c][column_index][:v] = deep_copy(value) return end |
- (Numeric) size
Returns number of rows in DataTable. May be zero.
574 575 576 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 574 def size return number_of_rows end |
- (Array) to_a
Returns copy of data representation as Array in Array Format. Modification of the returned array object has no influence on the original data of DataTable.
714 715 716 717 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 714 def to_a array = hash_to_array(@json) return deep_copy(array) end |
- (String) to_json(options = {})
Returns data representation as JSON String in DataTable format.
691 692 693 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 691 def to_json( = {}) return @json.to_json() end |
- (String) to_s
Returns data representation as JSON String in DataTable format.
663 664 665 |
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 663 def to_s to_json end |