Class: Backup::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/configuration.rb

Overview

Represents a specific Backup configuration. A Configuration instance may be used to load multiple recipe files, define and describe tasks, define roles, create an actor, and set configuration variables.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actor_class = Actor) ⇒ Configuration

:nodoc:



21
22
23
24
25
26
# File 'lib/backup/configuration.rb', line 21

def initialize(actor_class=Actor) #:nodoc:
  @actor = actor_class.new(self)
  #@logger = Logger.new
  @load_paths = [".", File.join(File.dirname(__FILE__), "recipes")]
  @variables = {}
end

Instance Attribute Details

#actorObject (readonly)

The actor created for this configuration instance.



10
11
12
# File 'lib/backup/configuration.rb', line 10

def actor
  @actor
end

#load_pathsObject (readonly)

The load paths used for locating recipe files.



16
17
18
# File 'lib/backup/configuration.rb', line 16

def load_paths
  @load_paths
end

#loggerObject (readonly)

The logger instance defined for this configuration.



13
14
15
# File 'lib/backup/configuration.rb', line 13

def logger
  @logger
end

#variablesObject (readonly)

The hash of variables currently known by the configuration



19
20
21
# File 'lib/backup/configuration.rb', line 19

def variables
  @variables
end

Instance Method Details

#[](variable) ⇒ Object



45
46
47
48
# File 'lib/backup/configuration.rb', line 45

def [](variable)
  # TODO have it raise if it doesn exist
  @variables[variable]
end

#action(name, options = {}, &block) ⇒ Object

Define a new task. If a description is active (see #desc), it is added to the options under the :desc key. This method ultimately delegates to Actor#define_task.



127
128
129
130
131
132
133
134
# File 'lib/backup/configuration.rb', line 127

def action(name, options={}, &block)
  # raise ArgumentError, "expected a block or method" unless block or options[:method]  # ?? 
  if @next_description
    options = options.merge(:desc => @next_description)
    @next_description = nil
  end
  actor.define_action(name, options, &block)
end

#desc(text) ⇒ Object

Describe the next task to be defined. The given text will be attached to the next task that is defined and used as its description.



120
121
122
# File 'lib/backup/configuration.rb', line 120

def desc(text)
  @next_description = text
end

#load(*args, &block) ⇒ Object

Disclaimer: This method written by Jamis Buck. Taken directly from his excellent code Capistrano.

Load a configuration file or string into this configuration.

Usage:

load("recipe"):
  Look for and load the contents of 'recipe.rb' into this
  configuration.

load(:file => "recipe"):
  same as above

load(:string => "set :scm, :subversion"):
  Load the given string as a configuration specification.

load { ... }
  Load the block in the context of the configuration.


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/backup/configuration.rb', line 81

def load(*args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  args.each { |arg| load options.merge(:file => arg) }
  return unless args.empty?

  if block
    raise "loading a block requires 0 parameters" unless args.empty?
    load(options.merge(:proc => block))

  elsif options[:file]
    file = options[:file]
    unless file[0] == ?/
      load_paths.each do |path|
        if File.file?(File.join(path, file))
          file = File.join(path, file)
          break
        elsif File.file?(File.join(path, file) + ".rb")
          file = File.join(path, file + ".rb")
          break
        end
      end
    end
    load :string => File.read(file), :name => options[:name] || file

  elsif options[:string]
    #logger.trace "loading configuration #{options[:name] || "<eval>"}"
    instance_eval(options[:string], options[:name] || "<eval>")

  elsif options[:proc]
    #logger.trace "loading configuration #{options[:proc].inspect}"
    instance_eval(&options[:proc])

  else
    raise ArgumentError, "don't know how to load #{options.inspect}"
  end
end

#require(*args) ⇒ Object

Require another file. This is identical to the standard require method, with the exception that it sets the reciever as the “current” configuration so that third-party task bundles can include themselves relative to that configuration.



54
55
56
57
58
59
60
# File 'lib/backup/configuration.rb', line 54

def require(*args) #:nodoc:
  original, Backup.configuration = Backup.configuration, self
  super
ensure
  # restore the original, so that require's can be nested
  Backup.configuration = original
end

#set(variable, value = nil, &block) ⇒ Object Also known as: []=

Set a variable to the given value.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/backup/configuration.rb', line 29

def set(variable, value=nil, &block)
  # if the variable is uppercase, then we add it as a constant to the
  # actor. This is to allow uppercase "variables" to be set and referenced
  # in recipes.
  if variable.to_s[0].between?(?A, ?Z)
    klass = @actor.metaclass
    klass.send(:remove_const, variable) if klass.const_defined?(variable)
    klass.const_set(variable, value)
  end

  value = block if value.nil? && block_given?
  @variables[variable] = value
end