Class: CLI::Mastermind::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/mastermind/configuration.rb

Overview

Main configuration object. Walks up the file tree looking for masterplans and loading them into to build a the configuration used by the CLI.

Masterplans are loaded such that configuration specified closest to the point of invocation override configuration from farther masterplans. This allows you to add folder specific configuration while still falling back to more and more general configuration options.

A global masterplan located at $HOME/.masterplan (or equivalent) 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 specific masterplans).

Defined Under Namespace

Classes: DSL

Constant Summary collapse

PLANFILE =

Filename of masterplan files

'.masterplan'
MASTER_PLAN =

Path to the top-level masterplan

File.join(Dir.home, PLANFILE)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



50
51
52
53
54
55
56
# File 'lib/cli/mastermind/configuration.rb', line 50

def initialize
  @loaded_masterplans = Set.new
  @plan_files = Set.new

  lookup_and_load_masterplans
  load_masterplan MASTER_PLAN
end

Instance Attribute Details

#plansObject (readonly)

Returns the value of attribute plans.



25
26
27
# File 'lib/cli/mastermind/configuration.rb', line 25

def plans
  @plans
end

Class Method Details

.add_attribute(attribute) ⇒ Object

Adds an arbitrary attribute given by attribute to the configuration class



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cli/mastermind/configuration.rb', line 28

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

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

  self.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
end

Instance Method Details

#add_plans(planfiles) ⇒ Object

Adds a set of filenames for plans into the set of @plan_files



59
60
61
# File 'lib/cli/mastermind/configuration.rb', line 59

def add_plans(planfiles)
  @plan_files.merge(planfiles)
end

#load_masterplan(filename) ⇒ Object

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



74
75
76
77
78
79
# File 'lib/cli/mastermind/configuration.rb', line 74

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

#load_plansObject

Loads all plan files added using add_plans

See Also:



65
66
67
68
69
70
71
# File 'lib/cli/mastermind/configuration.rb', line 65

def load_plans
  @plans ||= @plan_files.reduce({}) do |hash, file|
    plans = Plan.load file
    plans.each { |plan| hash[plan.name] = plan }
    hash
  end
end