Class: AppEnv::Environment

Inherits:
ActiveSupport::OrderedOptions
  • Object
show all
Defined in:
lib/appenv/environment.rb

Instance Method Summary collapse

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
  expand(&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, options = {})
  if options.kind_of?(String) || options.kind_of?(Symbol)
    options = { :from => options }
  end
  val = @source[options[:from] || property]
  self[property] = block_given? ? yield(val) : val
  assert(property)  if options[: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.expand { |env, src|
  env.var2 = src.var_two
}


23
24
25
26
# File 'lib/appenv/environment.rb', line 23

def expand(&blk)
  @source = _compile_source_env
  blk.call(self, @source)
end

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.

Yields:

  • (hash)


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_aObject

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