Class: Trooper::Configuration

Inherits:
Hash
  • Object
show all
Includes:
Trooper::Config::Defaults, Trooper::Config::Environment, Trooper::Config::Strategy
Defined in:
lib/trooper/configuration.rb

Constant Summary

Constants included from Trooper::Config::Defaults

Trooper::Config::Defaults::FILE_NAME

Constants included from Trooper::Config::Action

Trooper::Config::Action::DEFAULT_ACTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trooper::Config::Defaults

#load_defaults!

Methods included from Trooper::Config::Action

#action, #load_default_actions!

Methods included from Trooper::Config::Strategy

#strategy

Methods included from Trooper::Config::Environment

#env, #hosts, #load_environment!, #path, #repository, #ruby_bin_path, #user

Constructor Details

#initialize(options = {}) ⇒ Configuration

Public: initialize a new configuration object, will parse the given troopfile

options - Default options to combine with troopfile

Examples

Configuration.new({:my_override => 'settings'}) # => <Configuration>

Returns a configuration object.



46
47
48
49
50
51
# File 'lib/trooper/configuration.rb', line 46

def initialize(options = {})
  @loaded = false

  load_defaults! options
  load_troopfile! options
end

Class Method Details

.initObject

Public: Copies the template troopfile to current dir

Examples

Configuration.init # => nil

Returns nil.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/trooper/configuration.rb', line 25

def self.init
  gem_dir = File.dirname(__FILE__)

  if RUBY_VERSION == /1.8/
    require 'ftools'
    File.copy("#{gem_dir}/template/troopfile.rb", "./Troopfile")
  else
    require 'fileutils'
    ::FileUtils.copy("#{gem_dir}/template/troopfile.rb", "./Troopfile")
  end
end

Instance Method Details

#import(name) ⇒ Object

Public: Will allow user in import another troopfile.

Examples

@config.import('filename') # => nil

Returns nil.



109
110
111
112
113
114
115
116
117
118
# File 'lib/trooper/configuration.rb', line 109

def import(name)
  extname = File.extname(name).empty? ? ".rb" : nil
  filename =  File.join(troopfile_dir, "#{name}#{extname}")
  if File.exists?(filename)
    eval File.open(filename).read
    nil
  else
    raise Trooper::NoConfigurationFileError, "No Import Configuration file (#{self[:file_name]}) can be found!"
  end
end

#loaded?Boolean

Public: Check to see if troopfile is loaded.

Examples

@config.loaded? # => true

Returns boolean.

Returns:

  • (Boolean)


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

def loaded?
  @loaded
end

#runner(strategy_name) ⇒ Object

Public: Finds a Strategy and returns a runner for that strategy.

strategy_name - The name of the strategy as a symbol.

Examples

@config.runner(:my_strategy_name) # => <Runner>

Returns a runner.



73
74
75
76
# File 'lib/trooper/configuration.rb', line 73

def runner(strategy_name)
  strategy = Arsenal.strategies[strategy_name]
  Runner.new(strategy, self)
end

#set(hash) ⇒ Object

Public: Set variables that will be available to all actions.

hash - A key value hash to merge with config

Examples

@config.set(:my_variable => 'sdsd') # => available as method in an action

Returns self.



87
88
89
# File 'lib/trooper/configuration.rb', line 87

def set(hash)
  config.merge! hash
end

#to_sObject

Public: Terminal Friendly version of the configuration.

Examples

@configuration.to_s # => '...'

Returns a String.



60
61
62
# File 'lib/trooper/configuration.rb', line 60

def to_s
  config.map {|k,v| "#{k}: #{v}" }.join("\n")
end