Class: Factorix::MODList

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(mods = {}) ⇒ void

Initialize the MOD list

Parameters:



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

Parameters:

  • path (Pathname) (defaults to: Container[:runtime].mod_list_path)

    the path to the file to load the MOD list from (default: runtime.mod_list_path)

Returns:

Raises:



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

Parameters:

  • mod (Factorix::MOD)

    the MOD to add

  • enabled (Boolean) (defaults to: true)

    the enabled status. Default to true

  • version (Factorix::MODVersion, nil) (defaults to: nil)

    the version of the MOD. Default to nil

Raises:

  • (MODSettingsError)

    if the MOD is the base MOD and the enabled status is false



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

Parameters:

Raises:



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

Yield Parameters:

Returns:



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

Yield Parameters:

Returns:



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

Parameters:

Raises:



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

Parameters:

Returns:

  • (Boolean)

    true if the MOD is enabled, false otherwise

Raises:



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

Parameters:

Returns:

  • (Boolean)

    true if the MOD is in the list, false otherwise



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

Parameters:

Raises:



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

Parameters:

  • path (Pathname) (defaults to: Container[:runtime].mod_list_path)

    the path to the file to save the MOD list to (default: runtime.mod_list_path)



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

Parameters:

Returns:

Raises:



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