class Infrastructure::Configuration

Attributes

attributes[R]

Public Class Methods

new(base, config = {}) click to toggle source
# File lib/infrastructure/configuration.rb, line 5
def initialize(base, config = {})
  @base = base
  @attributes = Hash.new
  assign config
end

Public Instance Methods

after_assignment(*params) click to toggle source
# File lib/infrastructure/configuration.rb, line 65
def after_assignment(*params)
  @base.after_assignment *params
end
after_configure(*params) click to toggle source
# File lib/infrastructure/configuration.rb, line 57
def after_configure(*params)
  @base.after_configure *params
end
assign(key, value = nil) click to toggle source
# File lib/infrastructure/configuration.rb, line 69
def assign(key, value = nil)
  if key.is_a?(Hash)
    key.each { |k,v| assign k, v }
  else
    key = key.to_sym
    before_assignment key, value
    @attributes[key] = value.is_a?(Hash) ? Configuration.new(self, value) : value
    after_assignment key, value
  end
end
before_assignment(*params) click to toggle source
# File lib/infrastructure/configuration.rb, line 61
def before_assignment(*params)
  @base.before_assignment *params
end
before_configure(*params) click to toggle source
# File lib/infrastructure/configuration.rb, line 53
def before_configure(*params)
  @base.before_configure *params
end
config() click to toggle source
# File lib/infrastructure/configuration.rb, line 18
def config
  self
end
configure(&block) click to toggle source
# File lib/infrastructure/configuration.rb, line 11
def configure(&block)
  raise "configure expects a block" unless block
  before_configure
  instance_eval &block
  after_configure
end
fetch(key) click to toggle source
# File lib/infrastructure/configuration.rb, line 49
def fetch(key)
  @attributes[key.to_sym]
end
load_config(path_to_config_file, &block) click to toggle source
# File lib/infrastructure/configuration.rb, line 28
def load_config(path_to_config_file, &block)
  assign YAML.load(ERB.new(File.read(path_to_config_file)).result)
  block.call if block
end
load_config_if_exists(path_to_config_file, &block) click to toggle source
# File lib/infrastructure/configuration.rb, line 22
def load_config_if_exists(path_to_config_file, &block)
  if File.exists? path_to_config_file
   load_config path_to_config_file, &block
  end
end
method_missing(method, *params, &block) click to toggle source
Calls superclass method
# File lib/infrastructure/configuration.rb, line 80
def method_missing(method, *params, &block)
  if @attributes.keys.include? method
    fetch method
  elsif method.to_s.end_with? '='
    assign method.to_s[0..-2], params.first
  elsif @attributes.respond_to? method
    @attributes.send method, *params, &block
  else
    super
  end
end
save_config(path_to_config_file) click to toggle source
# File lib/infrastructure/configuration.rb, line 33
def save_config(path_to_config_file)
  File.open(path_to_config_file, 'w') do |file| 
    file.write to_h(stringify_keys: true).to_yaml
  end
end
to_h(params = {}) click to toggle source
# File lib/infrastructure/configuration.rb, line 39
def to_h(params = {})
  attributes = @attributes.dup
  if params[:stringify_keys]
    attributes.keys.each { |key| attributes[key.to_s] = attributes.delete(key) unless key.is_a?(String) }
    attributes.each { |key, value| attributes[key] = value.to_h(params) if value.is_a? Configuration }
  else
    attributes.each { |key, value| attributes[key] = value.to_h(params) if value.is_a? Configuration }
  end
end