Class: Nauvisian::ModList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nauvisian/mod_list.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mods = {}) ⇒ ModList

Returns a new instance of ModList.



17
18
19
20
21
22
23
24
# File 'lib/nauvisian/mod_list.rb', line 17

def initialize(mods={})
  @mods = {Nauvisian::Mod[name: "base"] => true}
  mods.each do |mod, enabled|
    next if mod.base?

    @mods[mod] = enabled
  end
end

Class Method Details

.load(from = DEFAULT_MOD_LIST_PATH) ⇒ Object



12
13
14
15
# File 'lib/nauvisian/mod_list.rb', line 12

def self.load(from=DEFAULT_MOD_LIST_PATH)
  raw_data = JSON.parse(File.read(from), symbolize_names: true)
  new(raw_data[:mods].to_h {|e| [Mod[name: e[:name]], e[:enabled]] })
end

Instance Method Details

#add(mod, enabled: nil) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
# File 'lib/nauvisian/mod_list.rb', line 38

def add(mod, enabled: nil)
  raise ArgumentError, "Can't disable the base mod" if mod.base? && enabled == false

  @mods[mod] = enabled.nil? ? true : enabled
end

#disable(mod) ⇒ Object

Raises:

  • (ArgumentError)


64
65
66
67
68
69
# File 'lib/nauvisian/mod_list.rb', line 64

def disable(mod)
  raise ArgumentError, "Can't disalbe the base mod" if mod.base?
  raise Nauvisian::ModNotFound, mod unless exist?(mod)

  @mods[mod] = false
end

#eachObject



30
31
32
33
34
35
36
# File 'lib/nauvisian/mod_list.rb', line 30

def each
  return @mods.to_enum unless block_given?

  @mods.each do |mod, enabled|
    yield(mod, enabled)
  end
end

#enable(mod) ⇒ Object



58
59
60
61
62
# File 'lib/nauvisian/mod_list.rb', line 58

def enable(mod)
  raise Nauvisian::ModNotFound, mod unless exist?(mod)

  @mods[mod] = true
end

#enabled?(mod) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



52
53
54
55
56
# File 'lib/nauvisian/mod_list.rb', line 52

def enabled?(mod)
  raise Nauvisian::ModNotFound, mod unless exist?(mod)

  @mods[mod]
end

#exist?(mod) ⇒ Boolean

Returns:

  • (Boolean)


50
# File 'lib/nauvisian/mod_list.rb', line 50

def exist?(mod) = @mods.key?(mod)

#remove(mod) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
# File 'lib/nauvisian/mod_list.rb', line 44

def remove(mod)
  raise ArgumentError, "Can't remove the base mod" if mod.base?

  @mods.delete(mod)
end

#save(to = DEFAULT_MOD_LIST_PATH) ⇒ Object



26
27
28
# File 'lib/nauvisian/mod_list.rb', line 26

def save(to=DEFAULT_MOD_LIST_PATH)
  File.write(to, JSON.pretty_generate({mods: @mods.map {|mod, enabled| {name: mod.name, enabled:} }}))
end