Class: UU::DataTable::DataTableModel

Inherits:
Object
  • Object
show all
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)

Methods included from UU::DataTable

#attributes

Constructor Details

- (DataTableModel) initialize(data = nil)

Creates new instance of DataTableModel.

Examples:

# init empty DataTableModel, with no columns and no rows
dtm = UU::DataTable::DataTableModel.new()
# init DataTableModel as copy of another model
data_table_model = ... # get existing DataTableModel
dtm_copy = UU::DataTable::DataTableModel.new(data_table_model)
# init DataTableModel from String containing data in DataTable Format

str_data = '{
              "cols": [
                {"label":"Name"},
                {"label":"Date","type":"datetime"},
                {"label":"Number","type":"number"}
                      ],
              "rows": [
                {"c": [{"v": "Peter"},{"v": "2013-04-13T13:58:22+02:00"},{"v": 154.17}]},
                {"c": [{"v": "John"},{"v": "2013-04-13T13:58:22+02:00"},{"v": 184.25}]},
                {"c": [{"v": "Elizabeth"},{"v": "2013-04-13T13:58:22+02:00"},{"v": 0.875}]},
                {"c": [{"v": "Emma"},{"v": "2013-04-13T13:58:22+02:00"},{"v": 1859}]}
                      ] 
            }'

dtm = UU::DataTable::DataTableModel.new(str_data)
# init DataTableModel from String containing data in Array Format

str_data = '[
              ["Name", {"label":"Date","type":"datetime"}, {"label":"Number","type":"number"}],
              ["Peter", "2013-04-13T13:58:22+02:00", 154.17],
              ["John", "2013-04-13T13:58:22+02:00", 184.25],
              ["Elizabeth", "2013-04-13T13:58:22+02:00", 184.25],
              ["Emma", "2013-04-13T13:58:22+02:00", 1859]
            ]'

dtm = UU::DataTable::DataTableModel.new(str_data)
# init DataTableModel from Hash containing data in DataTable Format

hash =  {
         :cols=> [
             {:label=>"Name"},
             {:label=>"Date",:type=>"datetime"},
             {:label=>"Number",:type=>"number"}
                 ],
         :rows=> [
             {:c=> [{:v=> "Peter"},{:v=> "2013-04-13T13:58:22+02:00"},{:v=> 154.17}]},
             {:c=> [{:v=> "John"},{:v=> "2013-04-13T13:58:22+02:00"},{:v=> 184.25}]},
             {:c=> [{:v=> "Elizabeth"},{:v=> "2013-04-13T13:58:22+02:00"},{:v=> 0.875}]},
             {:c=> [{:v=> "Emma"},{:v=> "2013-04-13T13:58:22+02:00"},{:v=> 1859}]}
                 ]
        }
dtm = UU::DataTable::DataTableModel.new(hash)
# init DataTableModel from Array containing data in Array Format

array = [
         ["Name", {:label => "Date",:type => "datetime"}, {:label => "Number",:type => "number"}],
         ["Peter", "2013-04-13T13:58:22+02:00", 154.17],
         ["John", "2013-04-13T13:58:22+02:00", 184.25],
         ["Elizabeth", "2013-04-13T13:58:22+02:00", 184.25],
         ["Emma", "2013-04-13T13:58:22+02:00", 1859]
       ]
dtm = UU::DataTable::DataTableModel.new(array)

Parameters:

  • data (String, Array Hash, UU::DataTable::DataTableModel) (defaults to: nil)

    Initialization data in form of JSON String, Array, Hash or DataTableModel.

    • String - String with valid JSON containing data in DataTable Format or Array Format

    • Array - Array containing data in Array Format, it is recommend to use Ruby Symbols for Hash keys.

    • Hash - Hash containing data in DataTable Format, it is recommend to use Ruby Symbols for Hash keys.

    • UU::DataTable::DataTableModel - Another DataTableModel, new model will be copy of passed data.

Raises:

  • (ArgumentError)

    If data is not one from JSON String, Array, Hash or 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.

Examples:

# add column with 'Surname' label, without type
dtm.add_column("Surname")

# add column with 'Date' label and 'datetime' as column type
dtm.add_column("Date", UU::DataTable::DataTableModel::ColumnType::DATE_TIME)

Parameters:

  • label (String)

    String with label of a new column. Label is displayed in header row of DataTable.

  • type (String) (defaults to: nil)

    String with type of a new column. The value of type can be chosen from the list specified by ColumnType class.

Returns:

  • (Numeric)

    Index of the new column.



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.

Examples:

# add row with four columns as JSON String in DataTable format
row = '{"c" : [ { "v" : "Andrew" }, { "v" : 13 }, { "v" : "1415-07-06T19:20+01:00" }, { "v" : "ues:TERR[123]:ATR[12345]" } ]}' # String
dtm.add_row(row)

# add same row as JSON String in Array format
row = '[ "Andrew", 13 , "1415-07-06T19:20+01:00", "ues:TERR[123]:ATR[12345]" ]' # String
dtm.add_row(row)

# add same row as Hash in DataTable format
row = {:c => [ { :v => "Andrew" }, { :v => 13 }, { :v => "1415-07-06T19:20+01:00" }, { :v => "ues:TERR[123]:ATR[12345]" } ]} # Hash
dtm.add_row(row)

# add same row as Array in Array format
row = [ "Andrew", 13 , "1415-07-06T19:20+01:00", "ues:TERR[123]:ATR[12345]" ] # Array
dtm.add_row(row)

Parameters:

  • row (String, Array, Hash)

    Row representation as JSON String, Array or Hash, it is recommend to use Ruby Symbols for Hash keys.

Returns:

  • (Numeric)

    Index of the new column.

Raises:

  • (ArgumentError)

    If row has bad format or is not one of String, Array or Hash.



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.

Examples:

# check number of rows in DataTable
dtm.number_of_rows # 12
# check number of columns in DataTable
dtm.number_of_columns # 5

dtm.clear

# check number of rows in DataTable
dtm.number_of_rows # 0
# check number of columns in DataTable
dtm.number_of_columns # 0


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.

Examples:

# check number of rows in DataTable
dtm.number_of_rows # 12

dtm.clear_rows

# check number of rows in DataTable
dtm.number_of_rows # 0


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.

Examples:

# copy DataTableModel
dtm_copy = dtm.clone

dtm.number_of_columns # 5
dtm_copy.number_of_columns # 5

# add column to original DataTableModel
dtm.add_column("Age")

dtm.number_of_columns # 6
dtm_copy.number_of_columns # 5

Returns:



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.

Examples:

# get label of fourth column
label = dtm.get_column_label(3) # string 'Name'

# get label of last column
label = dtm.get_column_label(-1) # string 'Date'

Parameters:

  • index (Integer)

    Index of column. An index can be represented by a single positive integer between 0 (index 0 = first column) and the number of column (excluding) which is returned by #number_of_columns method. You can use negative indices as well, which start counting from the end (i.e. minus number of column), with -1 being the last element.

Returns:

  • (String)

    Column label as String

Raises:

  • (ArgumentError)

    If index is greater than or equal to number of columns.



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.

Examples:

# get type of first column
type = dtm.get_column_type(0) # string 'number'

# get type of fourth column from the end
type = dtm.get_column_type(-4) # string 'datetime'

Parameters:

  • index (Integer)

    Index of column. An index can be represented by a single positive integer between 0 (index 0 = first column) and the number of column (excluding) which is returned by #number_of_columns method. You can use negative indices as well, which start counting from the end (i.e. minus number of column), with -1 being the last element.

Returns:

  • (String)

    Column type as String, value can be one from the list specified by ColumnType class.

Raises:

  • (ArgumentError)

    If index is greater than or equal to number of columns.



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.

Examples:

# get label property of cell at third row and seventh column
label = dtm.get_property(2, 6, UU::DataTable::DataTableModel::Property::LABEL) # string 'My Artifact'

# get label property of cell at last row and second column
label = dtm.get_property(-1, 1, UU::DataTable::DataTableModel::Property::LABEL) # string 'My Artifact'

Parameters:

  • row_index (Integer)

    Index of row. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of rows (excluding) which is returned by #number_of_rows method. You can use negative indices as well, which start counting from the end (i.e. minus number of rows), with -1 being the last element.

  • column_index (Integer)

    Index of column. An index can be represented by a single positive integer between 0 (index 0 = first column) and the number of column (excluding) which is returned by #number_of_columns method. You can use negative indices as well, which start counting from the end (i.e. minus number of column), with -1 being the last element.

  • property_name (String)

    Name of property. The property name can be chosen from the list specified by Property class.

Returns:

  • (Object, nil)

    Property value or nil if property is not set.

Raises:

  • (ArgumentError)

    If row_index is greater than or equal to number of rows or column_index is greater than or equal to number of columns. Or if property_name is not String.

See Also:

  • UU::DataTable::DataTableModel#UU#UU::DataTable#UU::DataTable::DataTableModel#UU::DataTable::DataTableModel::Property


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.

Examples:

# get third row
row = dtm.get_row(2) # hash in format {:c => [{:v => "First cell value"},{:v => "2013-04-13T13:58:22+02:00"},{:v => 13}]}

# get last row
row = dtm.get_row(-1)

Parameters:

  • index (Integer)

    Index of row. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of rows (excluding) which is returned by #number_of_rows method. You can use negative indices as well, which start counting from the end (i.e. minus number of rows), with -1 being the last element.

Returns:

  • (Hash)

    Hash with Ruby Symbols used as keys.

Raises:

  • (ArgumentError)

    If index is greater than or equal to number of rows.



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.

Examples:

# get values of first row
row = dtm.get_row_values(0) # array in format ["First cell value", "2013-04-13T13:58:22+02:00", 13]

# get values of third row from the end
row = dtm.get_row_values(-3)

Parameters:

  • index (Integer)

    Index of row. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of rows (excluding) which is returned by #number_of_rows method. You can use negative indices as well, which start counting from the end (i.e. minus number of rows), with -1 being the last element.

Returns:

  • (Array)

    Row values in simple Array.

Raises:

  • (ArgumentError)

    If index is greater than or equal to number of rows.



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.

Examples:

# get value of cell at third row and seventh column
value = dtm.get_value(2, 6)

# get value of cell at last row and second column
value = dtm.get_value(-1, 1)

Parameters:

  • row_index (Integer)

    Index of row. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of rows (excluding) which is returned by #number_of_rows method. You can use negative indices as well, which start counting from the end (i.e. minus number of rows), with -1 being the last element.

  • column_index (Integer)

    Index of column. An index can be represented by a single positive integer between 0 (index 0 = first column) and the number of column (excluding) which is returned by #number_of_columns method. You can use negative indices as well, which start counting from the end (i.e. minus number of column), with -1 being the last element.

Returns:

  • (Object)

    Value of cell.

Raises:

  • (ArgumentError)

    If row_index is greater than or equal to number of rows or column_index is greater than or equal to number of columns.



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.

Examples:

# insert column at second position, with 'Surname' label, without type
dtm.insert_column(1,"Surname")

# insert column at third position from the end, with 'Date' label and 'number' as column type
dtm.insert_column(-3 ,"Number", UU::DataTable::DataTableModel::ColumnType::NUMBER)

Parameters:

  • index (Integer)

    Index where new column will be inserted. An index can be represented by a single positive integer between 0 (index 0 = first column) and the number of column (excluding) which is returned by #number_of_columns method. You can use negative indices as well, which start counting from the end (i.e. minus number of column), with -1 being the last element.

  • label (String)

    String with label of a new column. Label is displayed in header row of DataTable.

  • type (String) (defaults to: nil)

    String with type of a new column. The value of type can be chosen from the list specified by ColumnType class.

Raises:

  • (ArgumentError)

    If index is greater than or equal to number of columns.



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).

Examples:

# insert row at fifth position with four columns as JSON String in DataTable format
row = '{"c" : [ { "v" : "Andrew" }, { "v" : 13 }, { "v" : "1415-07-06T19:20+01:00" }, { "v" : "ues:TERR[123]:ATR[12345]" } ]}' # String
dtm.insert_row(4 ,row)

# insert same row as JSON String in Array format
row = '[ "Andrew", 13 , "1415-07-06T19:20+01:00", "ues:TERR[123]:ATR[12345]" ]' # String
dtm.insert_row(4 ,row)

# insert same row as Hash in DataTable format
row = {:c => [ { :v => "Andrew" }, { :v => 13 }, { :v => "1415-07-06T19:20+01:00" }, { :v => "ues:TERR[123]:ATR[12345]" } ]} # Hash
dtm.insert_row(4 ,row)

# insert same row as Array in Array format
row = [ "Andrew", 13 , "1415-07-06T19:20+01:00", "ues:TERR[123]:ATR[12345]" ] # Array
dtm.insert_row(4 ,row)

Parameters:

  • index (Integer)

    Index where new row will be inserted. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of rows (excluding) which is returned by #number_of_rows method. You can use negative indices as well, which start counting from the end (i.e. minus number of rows), with -1 being the last element.

  • row (String, Array, Hash)

    Row representation as JSON String, Array or Hash, it is recommend to use Ruby Symbols for Hash keys.

Raises:

  • (ArgumentError)

    If row has bad format or is not one of String, Array or Hash. If index is greater than or equal to number of rows.



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.

Examples:

# get number of columns in DataTable
cols_num = dtm.number_of_columns # Number 5

Returns:

  • (Numeric)

    Number of columns.



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.

Examples:

# get number of rows in DataTable
rows_num = dtm.number_of_rows # Number 12

Returns:

  • (Numeric)

    Number of rows.



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).

Examples:

# remove fifth column
dtm.remove_column(4)

# remove last column
dtm.remove_column(-1)

Parameters:

  • index (Integer)

    Index of column to remove. An index can be represented by a single positive integer between 0 (index 0 = first column) and the number of column (excluding) which is returned by #number_of_columns method. You can use negative indices as well, which start counting from the end (i.e. minus number of column), with -1 being the last element.

Raises:

  • (ArgumentError)

    If index is greater than or equal to number of columns.



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).

Examples:

# remove second row
dtm.remove_row(1)

# remove fifth row from the end
dtm.remove_row(-4)

Parameters:

  • index (Integer)

    Index where new row will be inserted. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of rows (excluding) which is returned by #number_of_rows method. You can use negative indices as well, which start counting from the end (i.e. minus number of rows), with -1 being the last element.

Raises:

  • (ArgumentError)

    If index is greater than or equal to number of rows.



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.

Examples:

# set label 'Profession' to third column
dtm.set_column_label(2, "Profession")

# set label 'Age' to third column from the end
dtm.set_column_label(-3, "Age")

Parameters:

  • index (Integer)

    Index of column. An index can be represented by a single positive integer between 0 (index 0 = first column) and the number of column (excluding) which is returned by #number_of_columns method. You can use negative indices as well, which start counting from the end (i.e. minus number of column), with -1 being the last element.

  • label (String)

    String to set the label of column. Label is displayed in header row of DataTable.

Raises:

  • (ArgumentError)

    If index is greater than or equal to number of columns.



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.

Examples:

# set type 'uesuri' to second column
dtm.set_column_type(1, UU::DataTable::DataTableModel::ColumnType::UESURI)

# unset type of third column
dtm.set_column_type(2)

# same as above
dtm.set_column_type(2, nil)

Parameters:

  • index (Integer)

    Index of column. An index can be represented by a single positive integer between 0 (index 0 = first column) and the number of column (excluding) which is returned by #number_of_columns method. You can use negative indices as well, which start counting from the end (i.e. minus number of column), with -1 being the last element.

  • type (String) (defaults to: nil)

    String with type of a new column. The value of type can be chosen from the list specified by ColumnType class.

Raises:

  • (ArgumentError)

    If index is greater than or equal to number of columns.



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.

Examples:

# set label property to cell at second row and third column from the end
dtm.set_property(1, -3, UU::DataTable::DataTableModel::Property::LABEL, "My Artifact")

# set label property to cell at sixth row and third column
dtm.set_property(5, 2, UU::DataTable::DataTableModel::Property::LABEL, "My Artifact")

Parameters:

  • row_index (Integer)

    Index of row. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of rows (excluding) which is returned by #number_of_rows method. You can use negative indices as well, which start counting from the end (i.e. minus number of rows), with -1 being the last element.

  • column_index (Integer)

    Index of column. An index can be represented by a single positive integer between 0 (index 0 = first column) and the number of column (excluding) which is returned by #number_of_columns method. You can use negative indices as well, which start counting from the end (i.e. minus number of column), with -1 being the last element.

  • property_name (String)

    Name of property. The property name can be chosen from the list specified by Property class.

  • value (Object)

    New value of property.

Raises:

  • (ArgumentError)

    If row_index is greater than or equal to number of rows or column_index is greater than or equal to number of columns. Or if property_name is not String.

See Also:

  • UU::DataTable::DataTableModel#UU#UU::DataTable#UU::DataTable::DataTableModel#UU::DataTable::DataTableModel::Property


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.

Examples:

# set row at second position with four columns as JSON String in DataTable format
row = '{"c" : [ { "v" : "Andrew" }, { "v" : 13 }, { "v" : "1415-07-06T19:20+01:00" }, { "v" : "ues:TERR[123]:ATR[12345]" } ]}' # String
dtm.set_row(1 ,row)

# set same row as JSON String in Array format
row = '[ "Andrew", 13 , "1415-07-06T19:20+01:00", "ues:TERR[123]:ATR[12345]" ]' # String
dtm.set_row(1 ,row)

# set same row as Hash in DataTable format
row = {:c => [ { :v => "Andrew" }, { :v => 13 }, { :v => "1415-07-06T19:20+01:00" }, { :v => "ues:TERR[123]:ATR[12345]" } ]} # Hash
dtm.set_row(1 ,row)

# set same row as Array in Array format
row = [ "Andrew", 13 , "1415-07-06T19:20+01:00", "ues:TERR[123]:ATR[12345]" ] # Array
dtm.set_row(1 ,row)

Parameters:

  • index (Integer)

    Index of row. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of rows (excluding) which is returned by #number_of_rows method. You can use negative indices as well, which start counting from the end (i.e. minus number of rows), with -1 being the last element.

  • row (String, Array, Hash)

    Row representation as JSON String, Array or Hash, it is recommend to use Ruby Symbols for Hash keys.

Raises:

  • (ArgumentError)

    If row has bad format or is not one of String, Array or Hash. If index is greater than or equal to number of rows.



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.

Examples:

# set string value 'Unicorn' to cell at second row and third column from the end
dtm.set_value(1, -3, "Unicorn")

# set number value '13' to cell at sixth row and third column
dtm.set_value(5, 2, 13)

Parameters:

  • row_index (Integer)

    Index of row. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of rows (excluding) which is returned by #number_of_rows method. You can use negative indices as well, which start counting from the end (i.e. minus number of rows), with -1 being the last element.

  • column_index (Integer)

    Index of column. An index can be represented by a single positive integer between 0 (index 0 = first column) and the number of column (excluding) which is returned by #number_of_columns method. You can use negative indices as well, which start counting from the end (i.e. minus number of column), with -1 being the last element.

Raises:

  • (ArgumentError)

    If row_index is greater than or equal to number of rows or column_index is greater than or equal to number of columns.



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.

Examples:

# get number of rows in DataTable
size = dtm.size # Number 12

Returns:

  • (Numeric)

    Number of rows.



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.

Examples:

# get Array in Array Format

array = dtm.to_a # Array

# output:

[
  ["Name", {"label":"Date","type":"datetime"}, {"label":"Number","type":"number"}],
  ["Peter", "2013-04-13T13:58:22+02:00", 154.17],
  ["John", "2013-04-13T13:58:22+02:00", 184.25],
  ["Elizabeth", "2013-04-13T13:58:22+02:00", 184.25],
  ["Emma", "2013-04-13T13:58:22+02:00", 1859]
]

Returns:

  • (Array)

    Data as Array in Array Format where first row is Header row.



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.

Examples:

# get JSON String in DataTable format

data_str = dtm.to_json # String

# output:
# {
# "cols": [
#     {"label":"Name"},
#     {"label":"Date","type":"datetime"},
#     {"label":"Number","type":"number"}
#         ],
# "rows": [
#     {"c": [{"v": "Peter"},{"v": "2013-04-13T13:58:22+02:00"},{"v": 154.17}]},
#     {"c": [{"v": "John"},{"v": "2013-04-13T13:58:22+02:00"},{"v": 184.25}]},
#     {"c": [{"v": "Elizabeth"},{"v": "2013-04-13T13:58:22+02:00"},{"v": 0.875}]},
#     {"c": [{"v": "Emma"},{"v": "2013-04-13T13:58:22+02:00"},{"v": 1859}]}
#         ]
# }

Returns:

  • (String)

    Data in JSON String.



706
707
708
709
710
711
712
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 706

def to_json(options = {})
  #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(options)
end

- (String) to_s

Returns data representation as JSON String in DataTable format.

Examples:

# get JSON String in DataTable format

data_str = dtm.to_s # String

# output:
# {
# "cols": [
#     {"label":"Name"},
#     {"label":"Date","type":"datetime"},
#     {"label":"Number","type":"number"}
#         ],
# "rows": [
#     {"c": [{"v": "Peter"},{"v": "2013-04-13T13:58:22+02:00"},{"v": 154.17}]},
#     {"c": [{"v": "John"},{"v": "2013-04-13T13:58:22+02:00"},{"v": 184.25}]},
#     {"c": [{"v": "Elizabeth"},{"v": "2013-04-13T13:58:22+02:00"},{"v": 0.875}]},
#     {"c": [{"v": "Emma"},{"v": "2013-04-13T13:58:22+02:00"},{"v": 1859}]}
#         ]
# }

Returns:

  • (String)

    Data in JSON String.



678
679
680
# File 'uu_datatable-0.11.6/lib/uu/datatable/datatable_model.rb', line 678

def to_s
  to_json
end