Class: Capistrano::Configuration::Variables

Inherits:
Object
  • Object
show all
Includes:
ProcHelpers
Defined in:
lib/capistrano/configuration/variables.rb

Overview

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.

Instance Method Summary collapse

Methods included from ProcHelpers

callable_without_parameters?

Constructor Details

#initialize(values = {}) ⇒ Variables

Returns a new instance of Variables.



22
23
24
25
26
27
28
# File 'lib/capistrano/configuration/variables.rb', line 22

def initialize(values={})
  @trusted_keys = []
  @fetched_keys = []
  @locations = {}
  @values = values
  @trusted = true
end

Instance Method Details

#delete(key) ⇒ Object



63
64
65
# File 'lib/capistrano/configuration/variables.rb', line 63

def delete(key)
  values.delete(key)
end

#fetch(key, default = nil, &block) ⇒ Object



45
46
47
48
# 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) ⇒ Object



59
60
61
# File 'lib/capistrano/configuration/variables.rb', line 59

def fetch_for(key, default, &block)
  block ? values.fetch(key, &block) : values.fetch(key, default)
end

#keysObject



75
76
77
# File 'lib/capistrano/configuration/variables.rb', line 75

def keys
  values.keys
end

#peek(key, default = nil, &block) ⇒ Object

Internal use only.



51
52
53
54
55
56
57
# 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) ⇒ Object



37
38
39
40
41
42
43
# 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) ⇒ Object

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`.



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

def source_locations(key)
  locations[key]
end

#trusted_keysObject



67
68
69
# File 'lib/capistrano/configuration/variables.rb', line 67

def trusted_keys
  @trusted_keys.dup
end

#untrusted!Object



30
31
32
33
34
35
# File 'lib/capistrano/configuration/variables.rb', line 30

def untrusted!
  @trusted = false
  yield
ensure
  @trusted = true
end

#untrusted_keysObject



71
72
73
# File 'lib/capistrano/configuration/variables.rb', line 71

def untrusted_keys
  keys - @trusted_keys
end

#unused_keysObject

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



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

def unused_keys
  keys - fetched_keys
end