Class: UU::DataTable::DataTableModel
- Inherits:
-
Object
- Object
- UU::DataTable::DataTableModel
- Includes:
- UU::DataTable
- Defined in:
- uu_datatable-0.11.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.
Methods included from UU::DataTable
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 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'uu_datatable-0.11.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 @row_ids = {}; if (!@json.nil? && !@json[:rows].nil?) @json[:rows].each_index{ |index| row = @json[:rows][index]; if (!row[:id].nil?) @row_ids[row[:id]] = {:index => index}; row.delete(:id); end }; end # TODO validace return @json; 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.
231 232 233 234 235 236 237 238 239 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 231 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.
396 397 398 399 400 401 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 396 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.
632 633 634 635 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 632 def clear @json = empty_data return end |
- (void) clear_rows
This method returns an undefined value.
Removes all rows.
649 650 651 652 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 649 def clear_rows @json[:rows] = [] return end |
- (DataTableModel) clone
Returns copy of data representation as DataTableModel.
754 755 756 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 754 def clone return DataTableModel.new(to_h) end |
- (String) get_column_label(index)
Returns the label of column at specific index.
283 284 285 286 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 283 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.
302 303 304 305 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 302 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.
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 515 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.
136 137 138 139 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 136 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.
156 157 158 159 160 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 156 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.
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 179 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.
258 259 260 261 262 263 264 265 266 267 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 258 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).
429 430 431 432 433 434 435 436 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 429 def insert_row(index, row) check_boundaries(index,"index",size,"size", "rows") parsed_row = parse_row(row) validate_row(parsed_row, index) update_ids_on_insert_row(index); @json[:rows].insert(index, parsed_row) return end |
- (Numeric) number_of_columns
Returns number of columns in DataTable. May be zero.
600 601 602 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 600 def number_of_columns return @json[:cols].size end |
- (Numeric) number_of_rows
Returns number of rows in DataTable. May be zero.
611 612 613 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 611 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).
367 368 369 370 371 372 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 367 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).
486 487 488 489 490 491 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 486 def remove_row(index) check_boundaries(index,"index",size,"size", "rows") update_ids_on_remove_row(index); @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.
322 323 324 325 326 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 322 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.
346 347 348 349 350 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 346 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.
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 555 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.
463 464 465 466 467 468 469 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 463 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.
210 211 212 213 214 215 216 217 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 210 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.
589 590 591 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 589 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.
733 734 735 736 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 733 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.
706 707 708 709 710 711 712 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 706 def to_json( = {}) #fix for when displaying as GVC and data are empty, do not send anything! if (@json[:cols].nil? or @json[:cols].empty?) and (@json[:rows].nil? or @json[:rows].empty?) return nil.to_json end return @json.to_json() end |
- (String) to_s
Returns data representation as JSON String in DataTable format.
678 679 680 |
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 678 def to_s to_json end |