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 masterplan files and loading them to build a the configuration used by the CLI.

These masterplan files are loaded starting from the current working directory and traversing up until a masterplan with a ‘at_project_root` directive or 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 masterplan files, means that masterplan files closest to the source of your invokation will “beat” other masterplan files.

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

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

See DSL for a full list of the commands provided by Mastermind and a sample masterplan file.

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.

Parameters:

  • base_path (String, nil) (defaults to: nil)

    plans outside of the base path will be ignored



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

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)

Override the default NoMethodError with a more useful MissingConfigurationError.

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.



145
146
147
148
149
# File 'lib/cli/mastermind/configuration.rb', line 145

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

Instance Attribute Details

#plan_filesObject (readonly)

The set of planfiles to load



36
37
38
# File 'lib/cli/mastermind/configuration.rb', line 36

def plan_files
  @plan_files
end

#project_rootObject

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



64
# File 'lib/cli/mastermind/configuration.rb', line 64

add_attribute :project_root

Class Method Details

.add_attribute(attribute) ⇒ Object

Adds an arbitrary attribute given by attribute to the configuration class

Parameters:

  • attribute (String, Symbol)

    the attribute to define



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cli/mastermind/configuration.rb', line 44

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) ⇒ Void

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

Plans with paths outside the @base_path, if set, will be ignored.

Parameters:

  • planfiles (Array<String>)

    new planfiles to add to the set of planfiles

Returns:

  • (Void)


86
87
88
89
90
91
92
93
94
# File 'lib/cli/mastermind/configuration.rb', line 86

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 the user’s ask_for_confirmation setting.

Returns:

  • (Boolean)

    the user’s ask_for_confirmation setting



127
128
129
# File 'lib/cli/mastermind/configuration.rb', line 127

def ask?
  @ask_for_confirmation
end

#define_alias(alias_from, alias_to) ⇒ Void

Defines a user alias

Parameters:

  • alias_from (String)

    the string to be replaced during expansion

  • alias_to (String, Array<String>)

    the expanded argument

Returns:

  • (Void)


112
113
114
115
116
# File 'lib/cli/mastermind/configuration.rb', line 112

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) ⇒ Void

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

Parameters:

  • filename (String)

    the path to the masterplan to load

Returns:

  • (Void)


100
101
102
103
104
105
# File 'lib/cli/mastermind/configuration.rb', line 100

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) ⇒ String+

Maps an input string to an alias.

Parameters:

  • input (String)

    the value to be replaced

Returns:

  • (String, Array<String>)

    the replacement alias or the input, if no replacement exists



122
123
124
# File 'lib/cli/mastermind/configuration.rb', line 122

def map_alias(input)
  @aliases[input]
end

#skip_confirmation!false

Sets @ask_for_confirmation to ‘false`.

Returns:

  • (false)


134
135
136
# File 'lib/cli/mastermind/configuration.rb', line 134

def skip_confirmation!
  @ask_for_confirmation = false
end