Class: RgssDb::DataFile

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rgss_db/model/data_file.rb

Overview

RPG Maker base data file

This class saves the object as is, without any treatment

Direct Known Subclasses

DataFileArray, DataFileHash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, file_path, object) ⇒ DataFile

Constructor

Parameters:

  • type (String)

    Data file type

  • file_path (String)

    Data file path

  • object (Object)

    Data file object



103
104
105
106
107
108
# File 'lib/rgss_db/model/data_file.rb', line 103

def initialize(type, file_path, object)
  @type = type
  @file_path = file_path
  @object = object
  @object_ids = []
end

Instance Attribute Details

#file_pathString (readonly)

Data file path

Returns:

  • (String)


86
87
88
# File 'lib/rgss_db/model/data_file.rb', line 86

def file_path
  @file_path
end

#objectObject (readonly)

Data file object (not processed)

Returns:

  • (Object)


94
95
96
# File 'lib/rgss_db/model/data_file.rb', line 94

def object
  @object
end

#object_idsArray<Integer> (readonly)

Data file object IDs list

Returns:

  • (Array<Integer>)


90
91
92
# File 'lib/rgss_db/model/data_file.rb', line 90

def object_ids
  @object_ids
end

#typeString (readonly)

Data file type

Returns:

  • (String)


82
83
84
# File 'lib/rgss_db/model/data_file.rb', line 82

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Integer

Comparable operator (case insensitive)

Parameters:

  • other (Object)

    Other

Returns:

  • (Integer)


298
299
300
301
302
# File 'lib/rgss_db/model/data_file.rb', line 298

def <=>(other)
  return @file_path.downcase <=> other.file_path.downcase if other.is_a?(DataFile)

  @file_path.downcase <=> other.downcase if other.is_a?(String)
end

#all_objects?Boolean

Checks whether all objects are included in the serialization process

Returns:

  • (Boolean)


172
173
174
# File 'lib/rgss_db/model/data_file.rb', line 172

def all_objects?
  true
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 contents

By default it returns an empty array

Parameters:

  • list (Array)

    List of objects

Returns:

  • (Array<Integer>)


254
255
256
# File 'lib/rgss_db/model/data_file.rb', line 254

def convert_list_to_ids(list)
  []
end

#customizable?Boolean

Checks whether this data file instance supports object selection or not

Returns:

  • (Boolean)


154
155
156
# File 'lib/rgss_db/model/data_file.rb', line 154

def customizable?
  !to_list.nil?
end

#fileString

Gets the data file name

Returns:

  • (String)


199
200
201
# File 'lib/rgss_db/model/data_file.rb', line 199

def file
  File.basename(@file_path)
end

#file?(file) ⇒ Boolean

Checks if the given file base name matches this data file base name

The check is case insensitive

Parameters:

  • file (String)

    File path

Returns:

  • (Boolean)


132
133
134
# File 'lib/rgss_db/model/data_file.rb', line 132

def file?(file)
  File.basename(@file_path).casecmp?(File.basename(file))
end

#file_path?(file) ⇒ Boolean

Checks if the given file matches this data file path

The check is case insensitive

Parameters:

  • file (String)

    File path

Returns:

  • (Boolean)


119
120
121
# File 'lib/rgss_db/model/data_file.rb', line 119

def file_path?(file)
  @file_path.casecmp?(file)
end

#merge(data_file) ⇒ Object

Merges the given data file contents with this data file

Parameters:

Raises:

  • (StandardError)

    Data file cannot be merged



226
227
228
# File 'lib/rgss_db/model/data_file.rb', line 226

def merge(data_file)
  raise "cannot merge data file: '#{data_file}' because it is not supported!"
end

#mergeable?Boolean

Checks whether this data file instance is mergeable or not

Returns:

  • (Boolean)


163
164
165
# File 'lib/rgss_db/model/data_file.rb', line 163

def mergeable?
  !to_merge.nil?
end

#object_ids_clearObject

Clears the current list of object IDs



190
191
192
# File 'lib/rgss_db/model/data_file.rb', line 190

def object_ids_clear
  @object_ids.clear
end

#object_ids_update(*object_ids) ⇒ Object

Updates the list of object IDs for this data file

Any duped ID is auto. removed from the list

Parameters:

  • object_ids (Array<Integer>)


183
184
185
# File 'lib/rgss_db/model/data_file.rb', line 183

def object_ids_update(*object_ids)
  @object_ids = object_ids.flatten.uniq
end

#serializeObject

Serializes the data file’s object

This method performs the necessary logic to the object for serialization

By default, it returns the object as is

Returns:

  • (Object)


239
240
241
# File 'lib/rgss_db/model/data_file.rb', line 239

def serialize
  @object
end

#serialize_file_nameString

Process the data file’s file name for serialization

The extension is automatically removed

Returns:

  • (String)


210
211
212
213
214
215
216
217
# File 'lib/rgss_db/model/data_file.rb', line 210

def serialize_file_name
  base_name = File.basename(@file_path, ".*")
  if all_objects?
    base_name
  else
    base_name.concat(DATA_FILE_CUSTOM_LABEL)
  end
end

#to_listArray<Object>

Gets a list of objects to perform a selection

If the data file does not allow this behavior it returns “nil“

Returns “nil“ by default

Returns:

  • (Array<Object>)


278
279
280
# File 'lib/rgss_db/model/data_file.rb', line 278

def to_list
  nil
end

#to_mergeArray<Object>

Gets a list of objects prepared to be merged

Returns “nil“ by default

Returns:

  • (Array<Object>)


265
266
267
# File 'lib/rgss_db/model/data_file.rb', line 265

def to_merge
  nil
end

#to_sString

Converts this instance to a string

Returns:

  • (String)


287
288
289
# File 'lib/rgss_db/model/data_file.rb', line 287

def to_s
  File.basename(@file_path)
end

#type?(data_type) ⇒ Boolean

Checks if the given data type matches this data file type

The check is case insensitive

Parameters:

  • data_type (String)

Returns:

  • (Boolean)


145
146
147
# File 'lib/rgss_db/model/data_file.rb', line 145

def type?(data_type)
  File.fnmatch(@type, data_type, File::FNM_CASEFOLD)
end