Class: UU::DataTable::DataTableModel

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

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

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.



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.

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.



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.

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


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.

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


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.

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:



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.

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.



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.

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.



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.

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


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.

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.



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.

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.



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.

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.



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.

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.



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

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.



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.

Examples:

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

Returns:

  • (Numeric)

    Number of columns.



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.

Examples:

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

Returns:

  • (Numeric)

    Number of rows.



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

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.



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

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.



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.

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.



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.

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.



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.

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


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.

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.



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.

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.



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.

Examples:

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

Returns:

  • (Numeric)

    Number of rows.



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.

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.



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.

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.



691
692
693
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 691

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



663
664
665
# File 'uu_datatable-0.4.6/lib/uu/datatable/datatable_model.rb', line 663

def to_s
  to_json
end