Module: ZTK::Config

Defined in:
lib/ztk/config.rb

Overview

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:

Class Method Summary (collapse)

Instance Method Summary (collapse)

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method_symbol, *method_args)

Handles method calls for our configuration keys.



127
128
129
130
131
132
133
# 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

Class Method Details

+ (Object) extended(base)

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.



54
55
56
57
58
59
60
# File 'lib/ztk/config.rb', line 54

def self.extended(base)
  class << base
    attr_accessor :configuration
  end

  base.configuration = OpenStruct.new
end

Instance Method Details

- (Object) [](key)

Get the value of a configuration key.

Parameters:

  • key (Symbol, String)

    A symbol or string of the configuration key to return.

Returns:

  • The value currently assigned to the configuration key.



84
85
86
# File 'lib/ztk/config.rb', line 84

def [](key)
  _get(key)
end

- (Object) []=(key, value)

Set the value of a configuration key.

Parameters:

  • key (Symbol, String)

    A symbol or string of the configuration key to set.

  • value

    The value which you want to assign to the configuration key.

Returns:

  • The value assigned to the configuration key.



97
98
99
# File 'lib/ztk/config.rb', line 97

def []=(key, value)
  _set(key, value)
end

- (Object) config {|configuration| ... }

Yields the configuration OpenStruct object to a block.

Yields:

  • (configuration)

    Pass the configuration OpenStruct object to the specified block.



73
74
75
# File 'lib/ztk/config.rb', line 73

def config(&block)
  block and block.call(self.configuration)
end

- (Object) from_file(filename)

Loads a configuration from a file.



64
65
66
# File 'lib/ztk/config.rb', line 64

def from_file(filename)
  self.instance_eval(IO.read(filename), filename)
end

- (Boolean) has_key?(key)

Returns:

  • (Boolean)

See Also:

  • Hash#has_key?


109
110
111
# File 'lib/ztk/config.rb', line 109

def has_key?(key)
  self.configuration.send(:table).has_key?(key)
end

- (Object) keys

See Also:

  • Hash#keys


103
104
105
# File 'lib/ztk/config.rb', line 103

def keys
  self.configuration.send(:table).keys
end

- (Object) merge(hash)

See Also:

  • Hash#merge


115
116
117
# File 'lib/ztk/config.rb', line 115

def merge(hash)
  self.configuration.send(:table).merge(hash)
end

- (Object) merge!(hash)

See Also:

  • Hash#merge!


121
122
123
# File 'lib/ztk/config.rb', line 121

def merge!(hash)
  self.configuration.send(:table).merge!(hash)
end