Class: DaemonKit::Config
- Inherits:
-
Object
- Object
- DaemonKit::Config
- 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
-
.hash(config, symbolize = false) ⇒ Object
Return the
config
.yml file as a raw hash. -
.load(config) ⇒ Object
Load the
config
.yml file from DAEMON_ROOT/config.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Pick out a config by name.
- #data=(hash) ⇒ Object
- #extend_hash(hash) ⇒ Object
-
#initialize(config_data) ⇒ Config
constructor
Expects a hash, looks for DAEMON_ENV key.
-
#method_missing(method_name, *args) ⇒ Object
:nodoc:.
-
#to_h(symbolize = false) ⇒ Object
Return the internal hash structure used, optionally symbolizing the first level of keys in the hash.
Constructor Details
#initialize(config_data) ⇒ Config
Expects a hash, looks for DAEMON_ENV key
46 47 48 49 50 51 52 |
# File 'lib/daemon_kit/config.rb', line 46 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:
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/daemon_kit/config.rb', line 65 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.
39 40 41 |
# File 'lib/daemon_kit/config.rb', line 39 def hash( config, symbolize = false ) self.load( config ).to_h( symbolize ) end |
.load(config) ⇒ Object
Load the config
.yml file from DAEMON_ROOT/config
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/daemon_kit/config.rb', line 25 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 ) config_content = ERB.new(File.read(path)).result(binding) new( YAML.load( config_content ) ) end |
Instance Method Details
#[](key) ⇒ Object
Pick out a config by name
55 56 57 |
# File 'lib/daemon_kit/config.rb', line 55 def []( key ) @data[ key.to_s ] end |
#data=(hash) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/daemon_kit/config.rb', line 80 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/daemon_kit/config.rb', line 102 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
61 62 63 |
# File 'lib/daemon_kit/config.rb', line 61 def to_h( symbolize = false ) symbolize ? @data.symbolize_keys : @data end |