module ZTK::Config

Configuration Module

Extend an existing class with this module to turn it into a singleton configuration class.

Given we have some code like:

class C
  extend(ZTK::Config)
end

We can then do things like:

C.thing = "something"

or we can reference keys this way:

C[:thing] = "something"

Accessing the value is just as simple:

puts C.thing

We can also load configurations from disk. Assuming we have a file (i.e. config.rb) like:

message  "Hello World"
thing    (1+1)

We can load it like so:

C.from_file("config.rb")

Then we can reference the configuration defined in the file as easily as:

puts C.message

@author Zachary Patten <zpatten AT jovelabs DOT io>

Public Class Methods

extended(base) click to toggle source

Extend base class with this module.

This will add the configuration attribute to the base class and create a new OpenStruct object assigning it to the configuration attribute.

# File lib/ztk/config.rb, line 54
def self.extended(base)
  class << base
    attr_accessor :configuration
  end

  base.configuration = OpenStruct.new
end

Public Instance Methods

[](key) click to toggle source

Get the value of a configuration key.

@param [Symbol, String] key A symbol or string of the configuration

key to return.

@return The value currently assigned to the configuration key.

# File lib/ztk/config.rb, line 84
def [](key)
  _get(key)
end
[]=(key, value) click to toggle source

Set the value of a configuration key.

@param [Symbol, String] key A symbol or string of the configuration

key to set.

@param value The value which you want to assign to the configuration

key.

@return The value assigned to the configuration key.

# File lib/ztk/config.rb, line 97
def []=(key, value)
  _set(key, value)
end
config(&block) click to toggle source

Yields the configuration OpenStruct object to a block.

@yield [configuration] Pass the configuration OpenStruct object to the

specified block.
# File lib/ztk/config.rb, line 73
def config(&block)
  block and block.call(self.configuration)
end
from_file(filename) click to toggle source

Loads a configuration from a file.

# File lib/ztk/config.rb, line 64
def from_file(filename)
  self.instance_eval(IO.read(filename), filename)
end
has_key?(key) click to toggle source

@see Hash#has_key?

# File lib/ztk/config.rb, line 109
def has_key?(key)
  self.configuration.send(:table).has_key?(key)
end
keys() click to toggle source

@see Hash#keys

# File lib/ztk/config.rb, line 103
def keys
  self.configuration.send(:table).keys
end
merge(hash) click to toggle source

@see Hash#merge

# File lib/ztk/config.rb, line 115
def merge(hash)
  self.configuration.send(:table).merge(hash)
end
merge!(hash) click to toggle source

@see Hash#merge!

# File lib/ztk/config.rb, line 121
def merge!(hash)
  self.configuration.send(:table).merge!(hash)
end
method_missing(method_symbol, *method_args) click to toggle source

Handles method calls for our configuration keys.

# File lib/ztk/config.rb, line 127
def method_missing(method_symbol, *method_args)
  if method_args.length > 0
    _set(method_symbol, method_args.first)
  end

  _get(method_symbol)
end

Private Instance Methods

_get(key) click to toggle source
# File lib/ztk/config.rb, line 145
def _get(key)
  key = key.to_s
  (key !~ /=/) or key = key[0..-2]

  self.configuration.send(key.to_sym)
end
_set(key, value) click to toggle source
# File lib/ztk/config.rb, line 138
def _set(key, value)
  key = key.to_s
  (key =~ /=/) or key += '='

  self.configuration.send(key.to_sym, value)
end