Class: AppEnv::Environment
- Inherits:
-
ActiveSupport::OrderedOptions
- Object
- ActiveSupport::OrderedOptions
- AppEnv::Environment
- Defined in:
- lib/appenv/environment.rb
Instance Method Summary collapse
-
#apply(property, options = {}) ⇒ Object
Creates a property in the env, taking the value from the source hash.
-
#assert(*properties) ⇒ Object
After setting all the environment properties, assert which properties should exist.
-
#expand(&blk) ⇒ Object
You can use
expandto add more values to the environment after initialization. -
#initialize(file = '.env', &blk) ⇒ Environment
constructor
A new instance of Environment.
-
#print(pad = '') ⇒ Object
Print a line-by-line list of env values to STDOUT.
-
#set(property, value = nil) ⇒ Object
If you are computing a complex value (say, a conditional one), you might like to set the value via a block.
-
#splat(pattern) {|hash| ... } ⇒ Object
Collect all the source properties that match the pattern as a hash.
-
#to_a ⇒ Object
Collect a line-by-line list of strings in the form: ‘property = value’.
Constructor Details
#initialize(file = '.env', &blk) ⇒ Environment
Returns a new instance of Environment.
3 4 5 6 7 |
# File 'lib/appenv/environment.rb', line 3 def initialize(file = '.env', &blk) super(&nil) @file = file (&blk) end |
Instance Method Details
#apply(property, options = {}) ⇒ Object
Creates a property in the env, taking the value from the source hash. If you want to modify the value from the source before setting it in the env, supply a block that takes the src value and returns the env value.
Options:
-
:from – if the source property name differs, supply it
-
:required – raise an error if the property is not assigned a value
57 58 59 60 61 62 63 64 65 |
# File 'lib/appenv/environment.rb', line 57 def apply(property, = {}) if .kind_of?(String) || .kind_of?(Symbol) = { :from => } end val = @source[[:from] || property] self[property] = block_given? ? yield(val) : val assert(property) if [:required] self[property] end |
#assert(*properties) ⇒ Object
After setting all the environment properties, assert which properties should exist. The Configurator will raise an exception listing the missing properties, if any.
84 85 86 87 88 89 |
# File 'lib/appenv/environment.rb', line 84 def assert(*properties) missing = properties.collect { |prop| prop.to_s.upcase unless self[prop] }.compact raise("AppEnv requires #{missing.join(', ')}") if missing.any? end |
#expand(&blk) ⇒ Object
You can use expand to add more values to the environment after initialization. eg:
# Create the environment:
@env = AppEnv::Environment.new('test/data/env') { |env, src|
env.var1 = src.var_one
}
# Later, expand it:
@env. { |env, src|
env.var2 = src.var_two
}
23 24 25 26 |
# File 'lib/appenv/environment.rb', line 23 def (&blk) @source = _compile_source_env blk.call(self, @source) end |
#print(pad = '') ⇒ Object
Print a line-by-line list of env values to STDOUT.
101 102 103 |
# File 'lib/appenv/environment.rb', line 101 def print(pad = '') puts pad+to_a.join("\n"+pad) end |
#set(property, value = nil) ⇒ Object
If you are computing a complex value (say, a conditional one), you might like to set the value via a block. eg:
# Simple variable assignment:
env.var1 = src.var_one
# Variable assignment with a block:
env.set(:var2) {
if abcd?
'xyz'
else
'foo'
end
}
44 45 46 |
# File 'lib/appenv/environment.rb', line 44 def set(property, value = nil) self[property] = block_given? ? yield : value end |
#splat(pattern) {|hash| ... } ⇒ Object
Collect all the source properties that match the pattern as a hash. If a block is given, yield the hash to the block, which can modify values. Then merge the hash into the env.
72 73 74 75 76 77 |
# File 'lib/appenv/environment.rb', line 72 def splat(pattern) hash = ActiveSupport::OrderedOptions.new @source.each_pair { |k, v| hash[k] ||= v if k.to_s.match(pattern) } yield(hash) if block_given? self.update(hash) end |
#to_a ⇒ Object
Collect a line-by-line list of strings in the form: ‘property = value’.
94 95 96 |
# File 'lib/appenv/environment.rb', line 94 def to_a collect { |k, v| "#{k} = #{v.inspect}" } end |