Module: Capistrano::Configuration::Variables

Included in:
Capistrano::Configuration
Defined in:
lib/capistrano/configuration/variables.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#variablesObject (readonly)

The hash of variables that have been defined in this configuration instance.



17
18
19
# File 'lib/capistrano/configuration/variables.rb', line 17

def variables
  @variables
end

Class Method Details

.included(base) ⇒ Object

:nodoc:



6
7
8
9
10
11
12
13
# File 'lib/capistrano/configuration/variables.rb', line 6

def self.included(base) #:nodoc:
  %w(initialize respond_to? method_missing).each do |m|
    base_name = m[/^\w+/]
    punct     = m[/\W+$/]
    base.send :alias_method, "#{base_name}_without_variables#{punct}", m
    base.send :alias_method, m, "#{base_name}_with_variables#{punct}"
  end
end

Instance Method Details

#[](variable) ⇒ Object



146
147
148
# File 'lib/capistrano/configuration/variables.rb', line 146

def [](variable)
  fetch(variable, nil)
end

#exists?(variable) ⇒ Boolean

Returns true if the variable has been defined, and false otherwise.

Returns:

  • (Boolean)


80
81
82
# File 'lib/capistrano/configuration/variables.rb', line 80

def exists?(variable)
  (@scope && @variables[@scope] && @variables[@scope].key?(variable.to_sym)) || @variables.key?(variable.to_sym)
end

#fetch(variable, *args) ⇒ Object

Access a named variable. If the value of the variable responds_to? :call, #call will be invoked (without parameters) and the return value cached and returned.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/capistrano/configuration/variables.rb', line 106

def fetch(variable, *args)
  if !args.empty? && block_given?
    raise ArgumentError, "you must specify either a default value or a block, but not both"
  end

  sym = variable.to_sym
  protect(sym) do
    if @scope
      if !exists?(variable)
        return args.first unless args.empty?
        return yield(variable) if block_given?
        raise IndexError, "`#{variable}' not found"
      end

      if @variables[@scope] && @variables[@scope].respond_to?(:call)
        @original_procs[@scope] ||= {}
        @variables[@scope] ||= {}
        @original_procs[@scope][sym] = @variables[@scope][sym]
        @variables[@scope][sym] = @variables[@scope][sym].call
      elsif @variables[sym].respond_to?(:call)
        @original_procs[sym] = @variables[sym]
        @variables[sym] = @variables[sym].call
      end
    else
      if !@variables.key?(sym)
        return args.first unless args.empty?
        return yield(variable) if block_given?
        raise IndexError, "`#{variable}' not found"
      end

      if @variables[sym].respond_to?(:call)
        @original_procs[sym] = @variables[sym]
        @variables[sym] = @variables[sym].call
      end
    end
  end

  (@scope && @variables[@scope] && @variables[@scope][sym]) || @variables[sym]
end

#method_missing_with_variables(sym, *args, &block) ⇒ Object

:nodoc:



181
182
183
184
185
186
187
# File 'lib/capistrano/configuration/variables.rb', line 181

def method_missing_with_variables(sym, *args, &block) #:nodoc:
  if args.length == 0 && block.nil? && variables_has_key?(sym)
    self[sym]
  else
    method_missing_without_variables(sym, *args, &block)
  end
end

#reset!(variable) ⇒ Object

If the variable was originally a proc value, it will be reset to it’s original proc value. Otherwise, this method does nothing. It returns true if the variable was actually reset.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/capistrano/configuration/variables.rb', line 87

def reset!(variable)
  sym = variable.to_sym
  protect(sym) do
    if @scope && @original_procs[@scope] && @original_procs[@scope].key?(sym)
      @variables[@scope] ||= {}
      @variables[@scope][sym] = @original_procs[@scope].delete(sym)
      true
    elsif @original_procs.key?(sym)
      @variables[sym] = @original_procs.delete(sym)
      true
    else
      false
    end
  end
end

#respond_to_with_variables?(sym, include_priv = false) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


177
178
179
# File 'lib/capistrano/configuration/variables.rb', line 177

def respond_to_with_variables?(sym, include_priv=false) #:nodoc:
  variables_has_key?(sym) || respond_to_without_variables?(sym, include_priv)
end

#scope(scope_name, &block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/capistrano/configuration/variables.rb', line 19

def scope(scope_name, &block)
  old_scope = @scope
  @scope = scope_name
  result = yield
  @scope = old_scope
  result
end

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

Set a variable to the given value.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/capistrano/configuration/variables.rb', line 28

def set(variable, *args, &block)
  if variable.to_s !~ /^[_a-z]/
    raise ArgumentError, "invalid variable `#{variable}' (variables must begin with an underscore, or a lower-case letter)"
  end

  if !block_given? && args.empty? || block_given? && !args.empty?
    raise ArgumentError, "you must specify exactly one of either a value or a block"
  end

  if args.length > 1
    raise ArgumentError, "wrong number of arguments (#{args.length} for 1)"
  end

  value = args.empty? ? block : args.first
  sym = variable.to_sym
  protect(sym) {
    if @scope
      @variables[@scope] ||= {}
      @variables[@scope][sym] = value
    else
      @variables[sym] = value
    end
  }
end

#unset(variable) ⇒ Object

Removes any trace of the given variable.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/capistrano/configuration/variables.rb', line 56

def unset(variable)
  sym = variable.to_sym
  protect(sym) do
    if @scope
      if @original_procs[@scope] && @original_procs[@scope][sym]
        @original_procs[@scope].delete(sym)
        @original_procs.delete(@scope) if @original_procs[@scope].empty?
      else
        @original_procs.delete(sym)
      end
      if @variables[@scope] && @variables[@scope][sym]
        @variables[@scope].delete(sym)
        @variables.delete(@scope) if @variables[@scope].empty?
      else
        @variables.delete(sym)
      end
    else
      @original_procs.delete(sym)
      @variables.delete(sym)
    end
  end
end

#variables_has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/capistrano/configuration/variables.rb', line 150

def variables_has_key?(key)
  (@scope && @variables[@scope] && @variables[@scope].has_key?(key)) || @variables.has_key?(key)
end