Class: Rodbot::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/rodbot/config.rb

Overview

Simple yet flexible configuration module

The configuration is defined in Ruby as follows:

name 'Bot'
country 'Sweden'
country nil
log do
  level 3
end
plugin :matrix do
  version 1
  ssl true
end
plugin :slack do
  version 2
end

Within Rodbot, you should use the Rodbot.config shortcut to access the configuration.

source = Pathname('config/rodbot.rb')
rc = Rodbot::Config.new(source)
rc.config(:name)        # => 'Bot'
rc.config(:country)     # => nil
rc.config(:undefined)   # => nil
rc.config(:log)         # => { level: 3 }
rc.config(:plugin, :matrix, :version)
# => 1
rc.config(:plugin, :matrix)
# => { version: 1, ssl: true }
rc.config(:plugin)
# => { matrix: { version: 1, ssl: true }, slack: { version: 2 } }
rc.config
# => { name: 'Bot', country: nil, plugin: { matrix: { version: 1, ssl: true }, slack: { version: 2 } } }

There are two types configuration items:

  1. Object values without block like name ‘Bot’:
    The config key :name gets the object ‘Bot’ assigned. Subsequent assignments with the same config key overwrite previous assignments.

  2. Unspecified value with a block like log do:
    The config key :log is assigned a hash defined by the block. Subsequent assignments with the same config key are merged into the hash.

  3. Object values with a block like plugin :matrix do:
    The config key :plugin is assigned an empty hash which is then populated with the object ‘:matrix` (usually a Symbol) as key and the subtree defined by the block. Subsequent assignments with the same config key add more keys to this hash.

Please note: You can force a config key to always be treated as if it had a block (type 3) by adding it to the KEYS_WITH_IMPLICIT_BLOCK array.

Defaults set by the DEFAULTS constant are read first and therefore may be overwritten or extend as mentioned above.

Defined Under Namespace

Classes: Reader

Constant Summary collapse

KEYS_WITH_IMPLICIT_BLOCK =

Keys which are always treated as if they had a block even if they don’t

%i(plugin).freeze
DEFAULTS =

Default configuration

<<~END
  name 'Rodbot'
  port 7200
  time_zone ENV['TZ'] || 'Etc/UTC'
  db 'hash'
  app do
    threads Rodbot.env.development? ? (1..1) : (2..4)
  end
  log do
    to STDOUT
    level Rodbot.env.development? ? Logger::INFO : Logger::ERROR
  end
END

Instance Method Summary collapse

Constructor Details

#initialize(source, defaults: true) ⇒ self

Read configuration

Parameters:

  • source (String, IO)

    config source as raw string or file e.g. config/rodbot.rb

  • defaults (Boolean) (defaults to: true)

    whether to load the defaults or not



85
86
87
88
89
90
# File 'lib/rodbot/config.rb', line 85

def initialize(source, defaults: true)
  @config = Reader.new.eval_strings(
    (DEFAULTS if defaults),
    (source.respond_to?(:read) ? (source.read if source.readable?) : source)
  ).to_h
end

Instance Method Details

#config(*keys) ⇒ Object

Note:

Use the Rodbot.config shortcut to access this method!

Get config values and subtrees

Parameters:

  • keys (Array)

    key path to config subtree or value

Returns:

  • (Object)

    config subtree or value



98
99
100
101
102
103
104
105
106
# File 'lib/rodbot/config.rb', line 98

def config(*keys)
  return @config if keys.none?
  value = @config.dig(*keys)
  if value.instance_of?(Array) && value.count == 1
    value.first
  else
    value
  end
end