Class: Factorix::MODSettings::Section

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Enumerable[[
Defined in:
lib/factorix/mod_settings.rb,
sig/factorix/mod_settings.rbs

Overview

Represents a section in MOD settings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Section

Initialize a new section with the given name

Parameters:

  • name (String) —

    The section name

Raises:



21
22
23
24
25
26
27
28
# File 'lib/factorix/mod_settings.rb', line 21

def initialize(name)
  unless VALID_SECTIONS.include?(name)
    raise MODSettingsError, "Invalid MOD section name: #{name}"
  end

  @name = name
  @settings = {}
end

Instance Attribute Details

#name ⇒ String (readonly)

Get the section name

Returns:

  • (String) —

    The section name



33
34
35
# File 'lib/factorix/mod_settings.rb', line 33

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object?

Get a setting value from this section

Parameters:

  • key (String) —

    The setting key

Returns:

  • (Object, nil) —

    The setting value or nil if not found



48
# File 'lib/factorix/mod_settings.rb', line 48

def [](key) = @settings[key]

#[]=(key, value) ⇒ Object

Set a setting value in this section

Parameters:

  • key (String) —

    The setting key

  • value (Object) —

    The setting value

Returns:

  • (Object) —

    The setting value



40
41
42
# File 'lib/factorix/mod_settings.rb', line 40

def []=(key, value)
  @settings[key] = value
end

#each ⇒ Enumerator[[String, untyped], self] #each ⇒ self

Iterate over all settings in this section

Overloads:

  • #each ⇒ Enumerator[[String, untyped], self]

    Returns:

    • (Enumerator[[String, untyped], self])
  • #each ⇒ self

    Returns:

    • (self)

Yields:

  • (key, value) —

    Block to be called for each setting

Yield Parameters:

  • key (String) —

    The setting key

  • value (Object) —

    The setting value

Returns:

  • (Enumerator) —

    If no block is given



56
57
58
59
60
# File 'lib/factorix/mod_settings.rb', line 56

def each(&)
  return @settings.to_enum(:each) unless block_given?

  @settings.each(&)
end

#empty? ⇒ Boolean

Check if this section has any settings

Returns:

  • (Boolean) —

    True if the section has no settings



65
# File 'lib/factorix/mod_settings.rb', line 65

def empty? = @settings.empty?

#fetch(key) ⇒ Object #fetch(key, default) ⇒ Object #fetch(key) ⇒ Object

Fetch a setting value with optional default or block

Overloads:

  • #fetch(key) ⇒ Object

    Parameters:

    • key (String)

    Returns:

    • (Object)
  • #fetch(key, default) ⇒ Object

    Parameters:

    • key (String)
    • default (Object)

    Returns:

    • (Object)
  • #fetch(key) ⇒ Object

    Parameters:

    • key (String)

    Returns:

    • (Object)

Parameters:

  • key (String) —

    The setting key

  • default (Object) —

    Default value if key doesn't exist (optional)

Yields:

  • (key) —

    Block to compute default value if key doesn't exist

Yield Parameters:

  • key (String) —

    The missing key

Returns:

  • (Object) —

    The setting value, default, or block result

Raises:

  • (KeyError) —

    If key doesn't exist and no default/block provided



99
# File 'lib/factorix/mod_settings.rb', line 99

def fetch(key, *, &) = @settings.fetch(key, *, &)

#key?(key) ⇒ Boolean Also known as: has_key?, include?

Check if a key exists in this section

Parameters:

  • key (String) —

    The setting key

Returns:

  • (Boolean) —

    True if the key exists



71
# File 'lib/factorix/mod_settings.rb', line 71

def key?(key) = @settings.key?(key)

#keys ⇒ Array<String>

Get all keys in this section

Returns:

  • (Array<String>) —

    Array of all setting keys



78
# File 'lib/factorix/mod_settings.rb', line 78

def keys = @settings.keys

#size ⇒ Integer Also known as: length

Get the number of settings in this section

Returns:

  • (Integer) —

    Number of settings



88
# File 'lib/factorix/mod_settings.rb', line 88

def size = @settings.size

#to_h ⇒ Hash<String, Object>

Convert this section to a Hash

Returns:

  • (Hash<String, Object>) —

    Hash of all settings



104
# File 'lib/factorix/mod_settings.rb', line 104

def to_h = @settings.dup

#values ⇒ Array<Object>

Get all values in this section

Returns:

  • (Array<Object>) —

    Array of all setting values



83
# File 'lib/factorix/mod_settings.rb', line 83

def values = @settings.values