Class: Factorix::MODList
- Inherits:
-
Object
- Object
- Factorix::MODList
- Includes:
- Enumerable
- Defined in:
- lib/factorix/mod_list.rb
Overview
Represents a list of MODs and their enabled status
This class manages the mod-list.json file, which contains the list of MODs and their enabled/disabled states.
Defined Under Namespace
Classes: MODNotInListError
Class Method Summary collapse
-
.load(path = Container[:runtime].mod_list_path) ⇒ Factorix::MODList
Load the MOD list from the given file.
Instance Method Summary collapse
-
#add(mod, enabled: true, version: nil) ⇒ void
Add the MOD to the list.
-
#disable(mod) ⇒ void
Disable the MOD.
-
#each {|mod, state| ... } ⇒ Enumerator, Factorix::MODList
Iterate through all MOD-state pairs.
-
#each_mod {|mod| ... } ⇒ Enumerator, Factorix::MODList
(also: #each_key)
Iterate through all MODs.
-
#enable(mod) ⇒ void
Enable the MOD.
-
#enabled?(mod) ⇒ Boolean
Check if the MOD is enabled.
-
#exist?(mod) ⇒ Boolean
Check if the MOD is in the list.
-
#initialize(mods = {}) ⇒ void
constructor
Initialize the MOD list.
-
#remove(mod) ⇒ void
Remove the MOD from the list.
-
#save(path = Container[:runtime].mod_list_path) ⇒ void
Save the MOD list to the given file.
-
#version(mod) ⇒ Factorix::MODVersion?
Get the version of the MOD.
Constructor Details
#initialize(mods = {}) ⇒ void
Initialize the MOD list
42 43 44 45 46 47 |
# File 'lib/factorix/mod_list.rb', line 42 def initialize(mods={}) @mods = {} mods.each do |mod, state| @mods[mod] = state end end |
Class Method Details
.load(path = Container[:runtime].mod_list_path) ⇒ Factorix::MODList
Load the MOD list from the given file
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/factorix/mod_list.rb', line 21 def self.load(path=Container[:runtime].mod_list_path) raw_data = JSON.parse(path.read, symbolize_names: true) mods_hash = raw_data[:mods].to_h {|entry| mod = MOD[name: entry[:name]] version = entry[:version] ? MODVersion.from_string(entry[:version]) : nil state = MODState[enabled: entry[:enabled], version:] # Validate that base MOD is not disabled if mod.base? && !entry[:enabled] raise MODSettingsError, "base MOD cannot be disabled" end [mod, state] } new(mods_hash) end |
Instance Method Details
#add(mod, enabled: true, version: nil) ⇒ void
This method returns an undefined value.
Add the MOD to the list
102 103 104 105 106 |
# File 'lib/factorix/mod_list.rb', line 102 def add(mod, enabled: true, version: nil) raise MODSettingsError, "can't disable the base MOD" if mod.base? && enabled == false @mods[mod] = MODState[enabled:, version:] end |
#disable(mod) ⇒ void
This method returns an undefined value.
Disable the MOD
166 167 168 169 170 171 172 |
# File 'lib/factorix/mod_list.rb', line 166 def disable(mod) raise MODSettingsError, "can't disable the base MOD" if mod.base? raise MODNotInListError, "MOD not in the list: #{mod}" unless exist?(mod) current_state = @mods[mod] @mods[mod] = MODState[enabled: false, version: current_state.version] end |
#each {|mod, state| ... } ⇒ Enumerator, Factorix::MODList
Iterate through all MOD-state pairs
69 70 71 72 73 74 |
# File 'lib/factorix/mod_list.rb', line 69 def each(&block) return @mods.to_enum unless block @mods.each(&block) self end |
#each_mod {|mod| ... } ⇒ Enumerator, Factorix::MODList Also known as: each_key
Iterate through all MODs
81 82 83 84 85 86 |
# File 'lib/factorix/mod_list.rb', line 81 def each_mod(&block) return @mods.keys.to_enum unless block @mods.each_key(&block) self end |
#enable(mod) ⇒ void
This method returns an undefined value.
Enable the MOD
153 154 155 156 157 158 |
# File 'lib/factorix/mod_list.rb', line 153 def enable(mod) raise MODNotInListError, "MOD not in the list: #{mod}" unless exist?(mod) current_state = @mods[mod] @mods[mod] = MODState[enabled: true, version: current_state.version] end |
#enabled?(mod) ⇒ Boolean
Check if the MOD is enabled
131 132 133 134 135 |
# File 'lib/factorix/mod_list.rb', line 131 def enabled?(mod) raise MODNotInListError, "MOD not in the list: #{mod}" unless exist?(mod) @mods[mod].enabled? end |
#exist?(mod) ⇒ Boolean
Check if the MOD is in the list
124 |
# File 'lib/factorix/mod_list.rb', line 124 def exist?(mod) = @mods.key?(mod) |
#remove(mod) ⇒ void
This method returns an undefined value.
Remove the MOD from the list
113 114 115 116 117 118 |
# File 'lib/factorix/mod_list.rb', line 113 def remove(mod) raise MODSettingsError, "can't remove the base MOD" if mod.base? raise MODSettingsError, "can't remove expansion MOD: #{mod}" if mod.expansion? @mods.delete(mod) end |
#save(path = Container[:runtime].mod_list_path) ⇒ void
This method returns an undefined value.
Save the MOD list to the given file
53 54 55 56 57 58 59 60 61 |
# File 'lib/factorix/mod_list.rb', line 53 def save(path=Container[:runtime].mod_list_path) mods_data = @mods.map {|mod, state| data = {name: mod.name, enabled: state.enabled?} # Only include version in the output if it exists data[:version] = state.version.to_s if state.version data } path.write(JSON.pretty_generate({mods: mods_data})) end |
#version(mod) ⇒ Factorix::MODVersion?
Get the version of the MOD
142 143 144 145 146 |
# File 'lib/factorix/mod_list.rb', line 142 def version(mod) raise MODNotInListError, "MOD not in the list: #{mod}" unless exist?(mod) @mods[mod].version end |