class Capistrano::Configuration::Variables

Holds the variables assigned at Capistrano runtime via ‘set` and retrieved with `fetch`. Does internal bookkeeping to help identify user mistakes like spelling errors or unused variables that may lead to unexpected behavior.

Constants

CAPISTRANO_LOCATION
IGNORED_LOCATIONS

Attributes

fetched_keys[R]
locations[R]
values[R]

Public Class Methods

new(values={}) click to toggle source
# File lib/capistrano/configuration/variables.rb, line 22
def initialize(values={})
  @trusted_keys = []
  @fetched_keys = []
  @locations = {}
  @values = values
  @trusted = true
end

Public Instance Methods

delete(key) click to toggle source
# File lib/capistrano/configuration/variables.rb, line 63
def delete(key)
  values.delete(key)
end
fetch(key, default=nil, &block) click to toggle source
# File lib/capistrano/configuration/variables.rb, line 45
def fetch(key, default=nil, &block)
  fetched_keys << key unless fetched_keys.include?(key)
  peek(key, default, &block)
end
fetch_for(key, default, &block) click to toggle source
# File lib/capistrano/configuration/variables.rb, line 59
def fetch_for(key, default, &block)
  block ? values.fetch(key, &block) : values.fetch(key, default)
end
keys() click to toggle source
# File lib/capistrano/configuration/variables.rb, line 75
def keys
  values.keys
end
peek(key, default=nil, &block) click to toggle source

Internal use only.

# File lib/capistrano/configuration/variables.rb, line 51
def peek(key, default=nil, &block)
  value = fetch_for(key, default, &block)
  while callable_without_parameters?(value)
    value = (values[key] = value.call)
  end
  value
end
set(key, value=nil, &block) click to toggle source
# File lib/capistrano/configuration/variables.rb, line 37
def set(key, value=nil, &block)
  @trusted_keys << key if trusted? && !@trusted_keys.include?(key)
  remember_location(key)
  values[key] = block || value
  trace_set(key)
  values[key]
end
source_locations(key) click to toggle source

Returns an array of source file location(s) where the given key was assigned (i.e. where ‘set` was called). If the key was never assigned, returns `nil`.

# File lib/capistrano/configuration/variables.rb, line 87
def source_locations(key)
  locations[key]
end
trusted_keys() click to toggle source
# File lib/capistrano/configuration/variables.rb, line 67
def trusted_keys
  @trusted_keys.dup
end
untrusted!() { || ... } click to toggle source
# File lib/capistrano/configuration/variables.rb, line 30
def untrusted!
  @trusted = false
  yield
ensure
  @trusted = true
end
untrusted_keys() click to toggle source
# File lib/capistrano/configuration/variables.rb, line 71
def untrusted_keys
  keys - @trusted_keys
end
unused_keys() click to toggle source

Keys that have been set, but which have never been fetched.

# File lib/capistrano/configuration/variables.rb, line 80
def unused_keys
  keys - fetched_keys
end

Private Instance Methods

remember_location(key) click to toggle source
# File lib/capistrano/configuration/variables.rb, line 99
def remember_location(key)
  location = caller.find do |line|
    IGNORED_LOCATIONS.none? { |i| line.include?(i) }
  end
  (locations[key] ||= []) << location
end
trace_set(key) click to toggle source
# File lib/capistrano/configuration/variables.rb, line 106
def trace_set(key)
  return unless fetch(:print_config_variables, false)
  puts "Config variable set: #{key.inspect} => #{values[key].inspect}"
end
trusted?() click to toggle source
# File lib/capistrano/configuration/variables.rb, line 95
def trusted?
  @trusted
end