Class: DaemonKit::Config

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

Overview

Simplify simple config file loading for daemons. Assumes the config files all live in DAEMON_ROOT/config and are YAML files. Loaded configs are accessed like a hash with string keys.

Config files can either be keyed by environment (default behavior) or be a normal hash.

Load a config by passing the filename (with or without the .yml extension) to #load.

At this stage the configs are read-only.

Any of the keys can be called as methods as well.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_data) ⇒ Config

Expects a hash, looks for DAEMON_ENV key



41
42
43
44
45
46
47
# File 'lib/daemon_kit/config.rb', line 41

def initialize( config_data ) #:nodoc:
  if config_data.has_key?( DAEMON_ENV )
    self.data = config_data[ DAEMON_ENV ]
  else
    self.data = config_data
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

:nodoc:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/daemon_kit/config.rb', line 60

def method_missing( method_name, *args ) #:nodoc:
  unless method_name.to_s =~ /[\w_]+=$/
    #if @data.keys.include?( method_name.to_s )
    #  return @data.send( method_name.to_s )
    #end
    if @data.respond_to?( method_name.to_s )
      return @data.send( method_name.to_s )
    elsif @data.respond_to?( method_name.to_s.gsub(/\-/, '_') )
      return @data.send( method_name.to_s.gsub(/\-/, '_') )
    end
  end

  super
end

Class Method Details

.hash(config, symbolize = false) ⇒ Object

Return the config.yml file as a raw hash.



34
35
36
# File 'lib/daemon_kit/config.rb', line 34

def hash( config, symbolize = false )
  self.load( config ).to_h( symbolize )
end

.load(config) ⇒ Object

Load the config.yml file from DAEMON_ROOT/config

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
# File 'lib/daemon_kit/config.rb', line 22

def load( config )
  config = config.to_s
  config += '.yml' unless config =~ /\.yml$/

  path = File.join( DAEMON_ROOT, 'config', config )

  raise ArgumentError, "Can't find #{path}" unless File.exists?( path )

  new( YAML.load_file( path ) )
end

Instance Method Details

#[](key) ⇒ Object

Pick out a config by name



50
51
52
# File 'lib/daemon_kit/config.rb', line 50

def []( key )
  @data[ key.to_s ]
end

#data=(hash) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/daemon_kit/config.rb', line 75

def data=( hash )
  @data = hash
  class << @data
    def symbolize_keys( hash = self )
      hash.inject({}) { |result, (key, value)|
        new_key = case key
                when String then key.to_sym
                else key
                end
        new_value = case value
                when Hash then symbolize_keys(value)
                else value
                end
        result[new_key] = new_value
        result
      }
    end
  end

  extend_hash( @data )
end

#extend_hash(hash) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/daemon_kit/config.rb', line 97

def extend_hash( hash )
  hash.keys.each do |k|
    hash.instance_eval <<-KEY
      def #{k.gsub(/\-/, '_')}
        fetch("#{k}")
      end
    KEY
  end

  hash.each do |(key, value)|
    case value
      when Hash then extend_hash( value )
    end
  end
end

#to_h(symbolize = false) ⇒ Object

Return the internal hash structure used, optionally symbolizing the first level of keys in the hash



56
57
58
# File 'lib/daemon_kit/config.rb', line 56

def to_h( symbolize = false )
  symbolize ? @data.symbolize_keys : @data
end