Class: Cakewalk::Configuration

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/cakewalk/configuration.rb,
lib/cakewalk/configuration/bot.rb,
lib/cakewalk/configuration/dcc.rb,
lib/cakewalk/configuration/ssl.rb,
lib/cakewalk/configuration/sasl.rb,
lib/cakewalk/configuration/plugins.rb,
lib/cakewalk/configuration/timeouts.rb

Overview

Since:

  • 2.0.0

Defined Under Namespace

Classes: Bot, DCC, Plugins, SASL, SSL, Timeouts

Constant Summary collapse

KnownOptions =

Since:

  • 2.0.0

[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base = nil) ⇒ Configuration

Returns a new instance of Configuration.

Since:

  • 2.0.0



15
16
17
18
# File 'lib/cakewalk/configuration.rb', line 15

def initialize(base = nil)
  base ||= self.class.default_config
  super(base)
end

Class Method Details

.default_config ⇒ Hash

Generate a default configuration.

Returns:

  • (Hash)

Since:

  • 2.0.0



11
12
13
# File 'lib/cakewalk/configuration.rb', line 11

def self.default_config
  {}
end

Instance Method Details

#[](key) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 2.0.0



29
30
31
32
33
34
# File 'lib/cakewalk/configuration.rb', line 29

def [](key)
  # FIXME also adjust method_missing
  key = key.to_sym
  raise ArgumentError, "Unknown option #{key}" unless self.class::KnownOptions.include?(key)
  @table[key]
end

#[]=(key, value) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 2.0.0



36
37
38
39
40
41
# File 'lib/cakewalk/configuration.rb', line 36

def []=(key, value)
  # FIXME also adjust method_missing
  key = key.to_sym
  raise ArgumentError, "Unknown option #{key}" unless self.class::KnownOptions.include?(key)
  super(key, value)
end

#load(new_config, from_default = false)

This method returns an undefined value.

Loads a configuration from a hash by merging the hash with either the current configuration or the default configuration.

Parameters:

  • new_config (Hash) —

    The configuration to load

  • from_default (Boolean) (defaults to: false) —

    If true, the configuration won’t be merged with the currently set up configuration (by prior calls to #load or Bot#configure) but with the default configuration.

Since:

  • 2.0.0



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cakewalk/configuration.rb', line 52

def load(new_config, from_default = false)
  if from_default
    if respond_to?(:update_to_values!, true)
      send(:update_to_values!, self.class.default_config)
    else
      @table = self.class.default_config.dup
      # Ensure singleton methods exist for old OpenStruct (Ruby < 3.5)
      self.class.default_config.each_key do |k|
        if respond_to?(:new_ostruct_member!, true)
          send(:new_ostruct_member!, k)
        elsif respond_to?(:new_ostruct_member, true)
          send(:new_ostruct_member, k)
        end
      end
    end
  end

  new_config.each do |option, value|
    if value.is_a?(Hash)
      if self[option].is_a?(Configuration)
        self[option].load(value)
      else
        # recursive merging is handled by subclasses like
        # Configuration::Plugins
        self[option] = value
      end
    else
      self[option] = value
    end
  end
end

#load!(new_config)

This method returns an undefined value.

Like #load but always uses the default configuration

Parameters:

  • new_config (Hash) —

    (see #load_config)

See Also:

Since:

  • 2.0.0



89
90
91
# File 'lib/cakewalk/configuration.rb', line 89

def load!(new_config)
  load(new_config, true)
end

#to_h(&block) ⇒ Hash

Returns:

  • (Hash)

Since:

  • 2.0.0



21
22
23
24
25
26
27
# File 'lib/cakewalk/configuration.rb', line 21

def to_h(&block)
  if block
    @table.to_h(&block)
  else
    @table.dup
  end
end