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).

See Also:

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

#initialize(base_path = nil) ⇒ Configuration

Returns a new instance of Configuration.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cli/mastermind/configuration.rb', line 52

def initialize(base_path=nil)
  @base_path = base_path
  @loaded_masterplans = Set.new
  @plan_files = Set.new
  @ask_for_confirmation = true

  # If no alias exists for a particular value, return that value
  @aliases = Hash.new { |_,k| k }

  lookup_and_load_masterplans
  load_masterplan MASTER_PLAN
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



104
105
106
107
108
# File 'lib/cli/mastermind/configuration.rb', line 104

def method_missing(symbol, *args)
  super
rescue NoMethodError
  raise MissingConfigurationError, symbol
end

Instance Attribute Details

#plan_filesObject (readonly)

Returns the value of attribute plan_files.



27
28
29
# File 'lib/cli/mastermind/configuration.rb', line 27

def plan_files
  @plan_files
end

Class Method Details

.add_attribute(attribute) ⇒ Object

Adds an arbitrary attribute given by attribute to the configuration class



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

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
end

Instance Method Details

#add_plans(planfiles) ⇒ Object

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



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

def add_plans(planfiles)
  allowed_plans = if @base_path.nil?
                    planfiles
                  else
                    planfiles.select { |file| file.start_with? @base_path }
                  end

  @plan_files.merge(allowed_plans)
end

#ask?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/cli/mastermind/configuration.rb', line 94

def ask?
  @ask_for_confirmation
end

#define_alias(alias_from, alias_to) ⇒ Object



84
85
86
87
88
# File 'lib/cli/mastermind/configuration.rb', line 84

def define_alias(alias_from, alias_to)
  arguments = alias_to.split(' ') if alias_to.is_a? String

  @aliases[alias_from] = arguments unless @aliases.has_key? alias_from
end

#load_masterplan(filename) ⇒ Object

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



77
78
79
80
81
82
# File 'lib/cli/mastermind/configuration.rb', line 77

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

#map_alias(input) ⇒ Object



90
91
92
# File 'lib/cli/mastermind/configuration.rb', line 90

def map_alias(input)
  @aliases[input]
end

#skip_confirmation!Object



98
99
100
# File 'lib/cli/mastermind/configuration.rb', line 98

def skip_confirmation!
  @ask_for_confirmation = false
end