Class: RgssDb::DataFileHash

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

Overview

RPG Maker hash data file

This class expects the object to be a hash

Direct Known Subclasses

DataFileHashNumber

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

#objectHash (readonly)

Data file object (not processed)

Returns:

  • (Hash)


399
400
401
# File 'lib/rgss_db/model/data_file.rb', line 399

def object
  @object
end

Instance Method Details

#all_objects?Boolean

Checks whether all objects are included in the serialization process

Returns:

  • (Boolean)


406
407
408
# File 'lib/rgss_db/model/data_file.rb', line 406

def all_objects?
  object_ids.empty? || object.all? { |key, value| object_ids.include?(key) }
end

#convert_list_to_ids(list) ⇒ Array<Object>

Converts the given list to object IDs for this data file

The list must be an array of this data file hash values or keys

Parameters:

  • list (Array)

    List of objects

Returns:

  • (Array<Object>)


454
455
456
# File 'lib/rgss_db/model/data_file.rb', line 454

def convert_list_to_ids(list)
  list.map { |i| object.key?(i) ? i : object.key(i) }
end

#merge(data_file) ⇒ Object

Merges the given data file contents with this data file

Parameters:

Raises:

  • (StandardError)

    Data file cannot be merged



417
418
419
420
421
422
423
# File 'lib/rgss_db/model/data_file.rb', line 417

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

  data_file.to_merge.each_pair do |key, value|
    object.store(key, value)
  end
end

#serializeHash

Serializes the data file’s object

This method prepares the object as a hash

Object IDs list is used to filter the correct

Returns:

  • (Hash)


434
435
436
437
438
439
440
441
442
443
# File 'lib/rgss_db/model/data_file.rb', line 434

def serialize
  # Dups the original object
  processed_object = object.dup

  # Applies the selected object IDs (if any)
  processed_object = processed_object.filter { |key, value| object_ids.include?(key) } unless all_objects?

  # Returns the formatted object
  processed_object
end

#to_listArray<Object>

Gets a list of objects to perform a selection

Returns:

  • (Array<Object>)


472
473
474
475
476
477
# File 'lib/rgss_db/model/data_file.rb', line 472

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 auto-selecting objects
  object.values
end

#to_mergeHash

Gets a list of objects prepared to be merged

Returns:

  • (Hash)


463
464
465
# File 'lib/rgss_db/model/data_file.rb', line 463

def to_merge
  object
end