Class: RgssDb::DataFileArray

Inherits:
DataFile show all
Defined in:
lib/rgss_db/model/data_file.rb

Overview

RPG Maker array data file

This class expects the object to be an array

Instance Attribute Summary collapse

Attributes inherited from DataFile

#file_path, #object_ids, #type

Instance Method Summary collapse

Methods inherited from DataFile

#<=>, #customizable?, #file, #file?, #file_path?, #initialize, #mergeable?, #object_ids_clear, #object_ids_update, #serialize_file_name, #to_s, #type?

Constructor Details

This class inherits a constructor from RgssDb::DataFile

Instance Attribute Details

#objectArray (readonly)

Data file object (not processed)

Returns:

  • (Array)


313
314
315
# File 'lib/rgss_db/model/data_file.rb', line 313

def object
  @object
end

Instance Method Details

#all_objects?Boolean

Checks whether all objects are included in the serialization process

Returns:

  • (Boolean)


320
321
322
# File 'lib/rgss_db/model/data_file.rb', line 320

def all_objects?
  object_ids.empty? || object_ids.size == object.compact.size
end

#convert_list_to_ids(list) ⇒ Array<Integer>

Converts the given object list to object IDs for this data file

The given list must be instances of this data file’s array

Parameters:

  • list (Array)

    List of objects

Returns:

  • (Array<Integer>)


365
366
367
# File 'lib/rgss_db/model/data_file.rb', line 365

def convert_list_to_ids(list)
  list.map { |i| object.index(i) }
end

#merge(data_file) ⇒ Object

Merges the given data file contents with this data file

Parameters:

Raises:

  • (StandardError)

    Data file cannot be merged



331
332
333
334
335
# File 'lib/rgss_db/model/data_file.rb', line 331

def merge(data_file)
  raise "cannot merge data file: '#{data_file}' because it is not supported!" unless data_file.is_a?(DataFileArray)

  object.push(*data_file.to_merge)
end

#serializeArray<Object>

Serializes the data file’s object

This method prepares the object as an array

The first element is always “nil“ (required for RPG Maker)

Object IDs list is used to filter the selected items on the data file

Returns:

  • (Array<Object>)


348
349
350
351
352
353
354
# File 'lib/rgss_db/model/data_file.rb', line 348

def serialize
  # Applies the selected object IDs (if any)
  processed_object = all_objects? ? object.dup : object.dup.values_at(*object_ids)

  # Returns the formatted object (with a safe-check)
  processed_object.first.nil? ? processed_object : processed_object.unshift(nil)
end

#to_listArray<Object>

Gets a list of objects to perform a selection

Returns:

  • (Array<Object>)


383
384
385
386
387
388
# File 'lib/rgss_db/model/data_file.rb', line 383

def to_list
  # Data files needs to be compacted because TTY does not allow to create zero index based lists
  # for some data files, the first element is nil because they are created in RPG Maker database starting at 1
  # If this array is not compacted, it will cause an index mismatch later when selecting objects
  object.compact
end

#to_mergeArray<Object>

Gets a list of objects prepared to be merged

Returns:

  • (Array<Object>)


374
375
376
# File 'lib/rgss_db/model/data_file.rb', line 374

def to_merge
  object.compact
end