Class: Envolve::Config
- Inherits:
-
Object
- Object
- Envolve::Config
- Defined in:
- lib/envolve/config.rb
Overview
Public: A Configuration class to hold your application configuration
Feed it ENV or some other Hash-like object and it will allow you to access the elements in that hash via methods.
You can also tell it to only pull those items from the initial has that have a particular prefix, and that prefix will be stripped off of the elements for access.
Instance Attribute Summary collapse
-
#_env ⇒ Object
readonly
Internal: The internal hash holding all the keys and values.
-
#_key_separator ⇒ Object
readonly
Internal: The character to use as the key separator.
-
#_prefix ⇒ Object
readonly
Internal: The prefix to strip off all the keys.
Class Method Summary collapse
-
.environment_source(*args, &block) ⇒ Object
Public: Return the default environment.
-
.key_separator(*args, &block) ⇒ Object
Public: Return the key_separator to be used by the class.
-
.prefix(*args) ⇒ Object
Public: Return the prefix to be used by the class.
-
.properties ⇒ Object
Internal: Return the hash holding the properties.
-
.property(property, key: nil, value: nil, default: nil) ⇒ Object
Public: Set a property, with possible transformations.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Public: return the value for the give key.
-
#apply_transformation(input, transformer) ⇒ Object
Internal: Apply the given transformation to the input.
-
#config_with_prefix(prefix) ⇒ Object
Public: Return a subset of the config with just those items that have a prefix on them.
-
#initialize(env: self.class.environment_source, prefix: self.class.prefix, key_separator: self.class.key_separator) ⇒ Config
constructor
Public: Create a new Config.
-
#keys ⇒ Object
Public: Just the keys, only the keys, as Strings.
-
#method_missing(method, *args, &block) ⇒ Object
Internal: This is how we convert method calls into key lookups in the internal hash.
-
#prefix_regex(prefix, key_separator) ⇒ Object
Internal: The prefix regular expression used for stripping leading prefix.
-
#process_env(env) ⇒ Object
Internal: Process and Transform the keys and values from the environment into the final hash.
-
#respond_to_missing?(symbol, include_all = false) ⇒ Boolean
Internal: Respond to missing should always be implemented if you implement method_missing.
-
#size ⇒ Object
Public: The number of elements in the config.
-
#to_h ⇒ Object
(also: #to_hash)
Public: Return as hash of the keys and values.
-
#to_symbolized_h ⇒ Object
(also: #to_symbolized_hash)
Public: Return a hash of the keys and values with the keys as symbols.
-
#transform_properties(env, properties) ⇒ Object
Internal: Transform the environment variables to propreties.
Constructor Details
#initialize(env: self.class.environment_source, prefix: self.class.prefix, key_separator: self.class.key_separator) ⇒ Config
Public: Create a new Config
88 89 90 91 92 93 94 |
# File 'lib/envolve/config.rb', line 88 def initialize( env: self.class.environment_source, prefix: self.class.prefix, key_separator: self.class.key_separator ) @_key_separator = key_separator @_prefix = prefix.nil? ? nil : prefix.to_s.downcase.strip @_env = process_env( env ) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Internal: This is how we convert method calls into key lookups in the internal hash.
191 192 193 194 195 196 197 198 |
# File 'lib/envolve/config.rb', line 191 def method_missing( method, *args, &block ) s_method = method.to_s if _env.has_key?( s_method ) then _env[ s_method ] else super end end |
Instance Attribute Details
#_env ⇒ Object (readonly)
Internal: The internal hash holding all the keys and values
79 80 81 |
# File 'lib/envolve/config.rb', line 79 def _env @_env end |
#_key_separator ⇒ Object (readonly)
Internal: The character to use as the key separator
85 86 87 |
# File 'lib/envolve/config.rb', line 85 def _key_separator @_key_separator end |
#_prefix ⇒ Object (readonly)
Internal: The prefix to strip off all the keys
82 83 84 |
# File 'lib/envolve/config.rb', line 82 def _prefix @_prefix end |
Class Method Details
.environment_source(*args, &block) ⇒ Object
Public: Return the default environment.
Override this to return a different environment source
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/envolve/config.rb', line 14 def self.environment_source( *args, &block ) if args.size > 0 then @_default_env = args.first elsif block_given? then @_default_env = block.call else @_default_env = ENV unless defined?( @_default_env ) end @_default_env.to_h end |
.key_separator(*args, &block) ⇒ Object
Public: Return the key_separator to be used by the class
Override this to return a different key_separator, or meta program it in an inherited class.
42 43 44 45 46 47 48 49 |
# File 'lib/envolve/config.rb', line 42 def self.key_separator( *args, &block ) if args.size > 0 then @_key_separator = args.first else @_key_separator = '_'.freeze unless defined?( @_key_separator ) end @_key_separator end |
.prefix(*args) ⇒ Object
Public: Return the prefix to be used by the class
Override this to return a different prefix, or meta program it in an inherited class.
29 30 31 32 33 34 35 36 |
# File 'lib/envolve/config.rb', line 29 def self.prefix( *args ) if args.size > 0 then @_prefix = args.first else @_prefix = nil unless defined?( @_prefix ) end @_prefix end |
.properties ⇒ Object
Internal: Return the hash holding the properties
Returns Hash
74 75 76 |
# File 'lib/envolve/config.rb', line 74 def self.properties @_properties ||= Hash.new end |
.property(property, key: nil, value: nil, default: nil) ⇒ Object
Public: Set a property, with possible transformations
In the conversion of a the environment to the configuration properties sometimes the keys and/or values need to be converted to a new name.
All property transformations take place AFTER the initial keys have been downcased and prefix stripped.
property - the name of the property we want to appear in the configuration key - the source key from the environment where this property comes from value - the new value for this property default - setting a default for this property should it not exist
value may also be a lambda, in which ase the lambda is given the original value and the return value from the labmda is used for the new value.
67 68 69 |
# File 'lib/envolve/config.rb', line 67 def self.property( property, key: nil, value: nil, default: nil ) properties[property] = { :key => key, :value => value, :default => default } end |
Instance Method Details
#[](key) ⇒ Object
Public: return the value for the give key
key - the String key to use for fetching
Returns value or nil if no value is found
156 157 158 |
# File 'lib/envolve/config.rb', line 156 def []( key ) _env[key] end |
#apply_transformation(input, transformer) ⇒ Object
Internal: Apply the given transformation to the input.
Returns the result of the transformation
131 132 133 134 135 |
# File 'lib/envolve/config.rb', line 131 def apply_transformation( input, transformer ) return input if transformer.nil? return transformer.call( input ) if transformer.respond_to?( :call ) return transformer end |
#config_with_prefix(prefix) ⇒ Object
Public: Return a subset of the config with just those items that have a prefix on them
The resulting Config only has those keys, and all with the prefixes stripped
165 166 167 |
# File 'lib/envolve/config.rb', line 165 def config_with_prefix( prefix ) self.class.new( env: _env, prefix: prefix ) end |
#keys ⇒ Object
Public: Just the keys, only the keys, as Strings
Returns an Array of the keys as Strings
147 148 149 |
# File 'lib/envolve/config.rb', line 147 def keys _env.keys end |
#prefix_regex(prefix, key_separator) ⇒ Object
Internal: The prefix regular expression used for stripping leading prefix
This matches Beginning of String followed by the prefix followed by 0 or more key_separator characters.
This will use named captures. The prefix will be under key ‘prefix’ and the following will be under ‘rest’
Returns the regex
215 216 217 |
# File 'lib/envolve/config.rb', line 215 def prefix_regex( prefix, key_separator ) /\A(?<prefix>#{prefix}[#{key_separator}]*)(?<rest>.*)\Z/i end |
#process_env(env) ⇒ Object
Internal: Process and Transform the keys and values from the environment into the final hash
100 101 102 103 104 105 106 |
# File 'lib/envolve/config.rb', line 100 def process_env( env ) env = downcase_keys( env ) if _prefix then env = filter_by_prefix( env, _prefix ) end env = transform_properties( env, self.class.properties ) end |
#respond_to_missing?(symbol, include_all = false) ⇒ Boolean
Internal: Respond to missing should always be implemented if you implement method_missing
202 203 204 |
# File 'lib/envolve/config.rb', line 202 def respond_to_missing?( symbol, include_all = false ) _env.has_key?( symbol.to_s ) || super end |
#size ⇒ Object
Public: The number of elements in the config
Returns the number of elements
140 141 142 |
# File 'lib/envolve/config.rb', line 140 def size _env.size end |
#to_h ⇒ Object Also known as: to_hash
Public: Return as hash of the keys and values
Returns a Hash
172 173 174 |
# File 'lib/envolve/config.rb', line 172 def to_h _env.to_h.dup end |
#to_symbolized_h ⇒ Object Also known as: to_symbolized_hash
Public: Return a hash of the keys and values with the keys as symbols.
Returns a Hash
180 181 182 183 184 185 186 |
# File 'lib/envolve/config.rb', line 180 def to_symbolized_h h = {} _env.each do |key, value| h[key.to_sym] = value end return h end |
#transform_properties(env, properties) ⇒ Object
Internal: Transform the environment variables to propreties
Returns the transformed hash
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/envolve/config.rb', line 111 def transform_properties( env, properties ) transformed = env.to_h.dup properties.each do |dest_key, trans| src_key = trans[:key] src_key ||= dest_key if value = transformed.delete(src_key) then value = apply_transformation(value, trans[:value]) if trans[:value] elsif value.nil? then value = trans[:default] if trans[:default] end transformed[dest_key] = value end return transformed end |