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
57
58
59
# File 'lib/cli/mastermind/configuration.rb', line 50

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

  # 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

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

  define_method "#{attribute}=" do |new_value=nil,&block|
    self.instance_variable_set("@#{attribute}", new_value||block)  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



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

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

#define_alias(alias_from, alias_to) ⇒ Object



108
109
110
111
112
# File 'lib/cli/mastermind/configuration.rb', line 108

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

#execute_plan(*plan_stack, arguments: nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cli/mastermind/configuration.rb', line 81

def execute_plan(*plan_stack, arguments: nil)
  if plan_stack.size == 1
    case plan_stack.first
    when Array
      plan_stack = plan_stack.first
    when String
      plan_stack = plan_stack.first.split(' ')
    end
  end

  plan = @plans

  plan_stack.each do |plan_name|
    plan = plan[plan_name]
  end

  plan.call(arguments)
end

#load_masterplan(filename) ⇒ Object

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



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

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:



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

def load_plans
  @plans = {}

  top_level_plan = Plan.new('temporary_plan')

  @plan_files.each do |file|
    plans = Plan.load file
    top_level_plan.add_children plans
  end

  @plans = top_level_plan.children
end

#map_alias(input) ⇒ Object



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

def map_alias(input)
  @aliases[input]
end