Class: ConfStack

Inherits:
Object
  • Object
show all
Defined in:
lib/conf_stack.rb,
lib/conf_stack/version.rb

Overview

Main configuration object. Walks up the file tree looking for configuration files and loading them to build a the configuration.

These configuarion files are loaded starting from the current working directory and traversing up until a file with a at_project_root directive or the directory specified by a project_root directive is reached.

Configuration options set with configure are latched once set to something non-nil. This, along with the aforementioned load order of configuration files, means that configuration files closest to the source of your invokation will “beat” other configuration files.

A global configuration located at $HOME/.confstack is loaded last. You can use this to specify plans you want accessible everywhere or global configuration that should apply everywhere (unless overridden by more proximal files).

Additionally, there is a directive (see_other) that allows for configuration files outside of the lookup tree to be loaded.

See DSL for a full list of the commands provided by ConfStack.

Defined Under Namespace

Classes: DSL, Error, InvalidDirectoryError, MissingConfigurationError

Constant Summary collapse

VERSION =
"1.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename: '.confstack') ⇒ ConfStack



77
78
79
80
81
82
83
# File 'lib/conf_stack.rb', line 77

def initialize(filename: '.confstack')
  @filename = filename
  @loaded_conf_files = Set.new

  lookup_and_load_configuration_files
  load_configuration_file File.join(Dir.home, @filename)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object (private)

Override the default NoMethodError with a more useful MissingConfigurationError.

I

Since the configuration object is used directly by plans for configuration information, accessing non-existant configuration can lead to unhelpful NoMethodErrors. This replaces those errors with more helpful errors.



107
108
109
110
111
112
113
# File 'lib/conf_stack.rb', line 107

def method_missing(symbol, *args)
  return false if symbol.to_s.end_with? '?'

  super
rescue NoMethodError
  raise MissingConfigurationError.new(symbol, @filename)
end

Instance Attribute Details

#project_rootObject

Specifies the directory that is the root of your project. This directory is where ConfStack will stop looking for more files, so it’s important that it be set.



74
# File 'lib/conf_stack.rb', line 74

add_attribute :project_root

Class Method Details

.add_attribute(attribute) ⇒ Object

Adds an arbitrary attribute given by attribute to the configuration class



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/conf_stack.rb', line 48

def self.add_attribute(attribute)
  return if self.method_defined? attribute

  define_method "#{attribute}=" do |new_value=nil, &block|
    self.instance_variable_set("@#{attribute}", new_value.nil? ? block : new_value) if self.instance_variable_get("@#{attribute}").nil?
  end

  define_method attribute do
    value = self.instance_variable_get("@#{attribute}")
    return value unless value.respond_to?(:call)

    # Cache the value returned by the block so we're not doing potentially
    # expensive operations mutliple times.
    self.instance_variable_set("@#{attribute}", self.instance_eval(&value))
  end

  define_method "#{attribute}?" do
    true
  end

  nil
end

Instance Method Details

#load_configuration_file(filename) ⇒ Void

Loads a config file using the DSL, if it exists and hasn’t been loaded already



89
90
91
92
93
94
95
96
# File 'lib/conf_stack.rb', line 89

def load_configuration_file filename
  if File.exists? filename and !@loaded_conf_files.include? filename
    @loaded_conf_files << filename
    DSL.new(self, filename)
  end

  nil
end