Class: Sabrina::Monster
- Inherits:
-
Object
- Object
- Sabrina::Monster
- Includes:
- ChildrenManager
- Defined in:
- lib/sabrina/monster.rb
Overview
An abstract class for dealing with further abstractions of data related to a specific, numbered monster.
See Plugins for extensions that might enhance this class with added functionality.
Instance Attribute Summary collapse
-
#dex_number ⇒ Object
The effective dex number of the monster, depending on any blanks in the ROM file.
-
#filename ⇒ String
readonly
The filename for saving data to a file, sans the path and extension.
-
#index ⇒ Integer
The real index of the monster.
-
#rom ⇒ Rom
The current working ROM file.
-
#work_dir ⇒ String
The directory for reading and saving file data.
Class Method Summary collapse
-
.parse_index(index, rom) ⇒ Integer
Calculates the real index by parsing the provided index either as a dex number (when integer) or as a string.
Instance Method Summary collapse
- #children ⇒ Array
-
#close ⇒ 0
Closes the ROM file.
-
#initialize(rom, index, dir = nil) ⇒ Monster
constructor
Generates a new instance of Monster.
-
#name ⇒ String
Reads the monster name from the ROM.
-
#to_s ⇒ String
Prints a blurb consisting of the monster’s dex number and name.
Methods included from ChildrenManager
Constructor Details
#initialize(rom, index, dir = nil) ⇒ Monster
Generates a new instance of Monster.
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/sabrina/monster.rb', line 86 def initialize(rom, index, dir = nil) @plugins = {} @rom = rom @index = self.class.parse_index(index, @rom) @dex_number = index @filename = format('%03d', @index) @work_dir = dir || @rom.filename.dup << '_files/' load_plugins end |
Instance Attribute Details
#dex_number ⇒ Object
The effective dex number of the monster, depending on any blanks in the ROM file.
42 43 44 |
# File 'lib/sabrina/monster.rb', line 42 def dex_number @dex_number end |
#filename ⇒ String (readonly)
The filename for saving data to a file, sans the path and extension. Refer to #work_dir= for setting the path, and child #save
methods should take care of adding the right extension.
31 32 33 |
# File 'lib/sabrina/monster.rb', line 31 def filename @filename end |
#index ⇒ Integer
The real index of the monster.
24 |
# File 'lib/sabrina/monster.rb', line 24 attr_children :rom, :index |
#rom ⇒ Rom
The current working ROM file.
24 |
# File 'lib/sabrina/monster.rb', line 24 attr_children :rom, :index |
#work_dir ⇒ String
The directory for reading and saving file data. Defaults to the current ROM’s file name affixed with _files
.
38 39 40 |
# File 'lib/sabrina/monster.rb', line 38 def work_dir @work_dir end |
Class Method Details
.parse_index(index, rom) ⇒ Integer
Calculates the real index by parsing the provided index either as a dex number (when integer) or as a string. A number string prefixed with an exclamation mark “!” will be interpreted as the real index.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sabrina/monster.rb', line 55 def parse_index(index, rom) in_index = index.to_s if /[^0-9\!]/ =~ in_index fail "\'#{in_index}\' does not look like a valid index." end out_index = if in_index['!'] in_index.rpartition('!').last.to_i else i = in_index.to_i i < rom.dex_blank_start ? i : i + rom.dex_blank_length end unless out_index < rom.dex_length fail "Real index #{out_index} out of bounds;" \ " #{rom.id} has #{rom.dex_length} monsters." end out_index end |
Instance Method Details
#children ⇒ Array
101 102 103 |
# File 'lib/sabrina/monster.rb', line 101 def children @plugins.values end |
#close ⇒ 0
Closes the ROM file.
135 136 137 138 |
# File 'lib/sabrina/monster.rb', line 135 def close @rom.close 0 end |
#name ⇒ String
Reads the monster name from the ROM.
128 129 130 |
# File 'lib/sabrina/monster.rb', line 128 def name @rom.monster_name(@index) end |
#to_s ⇒ String
Prints a blurb consisting of the monster’s dex number and name.
143 144 145 |
# File 'lib/sabrina/monster.rb', line 143 def to_s "#{@index}. #{name}" end |