class Trooper::Configuration

Public Class Methods

init() click to toggle source

Public: Copies the template troopfile to current dir

Examples

Configuration.init # => nil

Returns nil.

# 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
new(options = {}) click to toggle source

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.

# File lib/trooper/configuration.rb, line 46
def initialize(options = {})
  @loaded = false

  load_defaults! options
  load_troopfile! options
end

Public Instance Methods

import(name) click to toggle source

Public: Will allow user in import another troopfile.

Examples

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

Returns nil.

# 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?() click to toggle source

Public: Check to see if troopfile is loaded.

Examples

@config.loaded? # => true

Returns boolean.

# File lib/trooper/configuration.rb, line 98
def loaded?
  @loaded
end
runner(strategy_name) click to toggle source

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.

# File lib/trooper/configuration.rb, line 73
def runner(strategy_name)
  strategy = Arsenal.strategies[strategy_name]
  Runner.new(strategy, self)
end
set(hash) click to toggle source

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.

# File lib/trooper/configuration.rb, line 87
def set(hash)
  config.merge! hash
end
to_s() click to toggle source

Public: Terminal Friendly version of the configuration.

Examples

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

Returns a String.

# File lib/trooper/configuration.rb, line 60
def to_s
  config.map {|k,v| "#{k}: #{v}" }.join("\n")
end

Private Instance Methods

load_troopfile!(options) click to toggle source

loads the troopfile and sets the environment up

# File lib/trooper/configuration.rb, line 127
def load_troopfile!(options)
  if troopfile?
    eval troopfile.read
    @loaded = true
    
    load_environment!
    set options
  else
    raise Trooper::NoConfigurationFileError, "No Configuration file (#{self[:file_name]}) can be found!"
  end
end
troopfile() click to toggle source

returns the troopfile file object

# File lib/trooper/configuration.rb, line 140
def troopfile
  File.open(config[:file_name])
end
troopfile?() click to toggle source

returns true if the troopfile exists

# File lib/trooper/configuration.rb, line 145
def troopfile?
  File.exists?(config[:file_name])
end
troopfile_dir() click to toggle source
# File lib/trooper/configuration.rb, line 149
def troopfile_dir
  File.dirname(File.realpath(config[:file_name]))
end