Module: Minestrone::Configuration::Variables
- Included in:
- Minestrone::Configuration
- Defined in:
- lib/minestrone/configuration/variables.rb
Instance Attribute Summary collapse
-
#variables ⇒ Object
readonly
The hash of variables that have been defined in this configuration instance.
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #[](variable) ⇒ Object
- #append(variable, *values) ⇒ Object
-
#exists?(variable) ⇒ Boolean
Returns true if the variable has been defined, and false otherwise.
-
#fetch(variable, *args) ⇒ Object
Access a named variable.
-
#initialize_variables ⇒ Object
:nodoc:.
-
#method_missing_with_variables(sym, *args, &block) ⇒ Object
:nodoc:.
- #remove(variable, *values) ⇒ Object
-
#reset!(variable) ⇒ Object
If the variable was originally a proc value, it will be reset to it's original proc value.
-
#respond_to_with_variables?(sym, include_priv = false) ⇒ Boolean
:nodoc:.
-
#set(variable, *args, &block) ⇒ Object
(also: #[]=)
Set a variable to the given value.
- #set_if_empty(variable, *args, &block) ⇒ Object
-
#unset(variable) ⇒ Object
Removes any trace of the given variable.
Instance Attribute Details
#variables ⇒ Object (readonly)
The hash of variables that have been defined in this configuration instance.
16 17 18 |
# File 'lib/minestrone/configuration/variables.rb', line 16 def variables @variables end |
Class Method Details
.included(base) ⇒ Object
:nodoc:
6 7 8 9 10 11 12 13 |
# File 'lib/minestrone/configuration/variables.rb', line 6 def self.included(base) #:nodoc: %w(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
102 103 104 |
# File 'lib/minestrone/configuration/variables.rb', line 102 def [](variable) fetch(variable, nil) end |
#append(variable, *values) ⇒ Object
50 51 52 |
# File 'lib/minestrone/configuration/variables.rb', line 50 def append(variable, *values) set(variable, Array(fetch(variable, [])).concat(values)) end |
#exists?(variable) ⇒ Boolean
Returns true if the variable has been defined, and false otherwise.
59 60 61 |
# File 'lib/minestrone/configuration/variables.rb', line 59 def exists?(variable) @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.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/minestrone/configuration/variables.rb', line 81 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 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 @variables[sym] end |
#initialize_variables ⇒ Object
:nodoc:
106 107 108 109 110 111 112 |
# File 'lib/minestrone/configuration/variables.rb', line 106 def initialize_variables #:nodoc: @variables = {} @original_procs = {} set :ssh_options, {} set :logger, logger end |
#method_missing_with_variables(sym, *args, &block) ⇒ Object
:nodoc:
118 119 120 121 122 123 124 |
# File 'lib/minestrone/configuration/variables.rb', line 118 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 |
#remove(variable, *values) ⇒ Object
54 55 56 |
# File 'lib/minestrone/configuration/variables.rb', line 54 def remove(variable, *values) set(variable, Array(fetch(variable, [])) - values) 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.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/minestrone/configuration/variables.rb', line 66 def reset!(variable) sym = variable.to_sym if @original_procs.key?(sym) @variables[sym] = @original_procs.delete(sym) true else false end end |
#respond_to_with_variables?(sym, include_priv = false) ⇒ Boolean
:nodoc:
114 115 116 |
# File 'lib/minestrone/configuration/variables.rb', line 114 def respond_to_with_variables?(sym, include_priv = false) #:nodoc: @variables.has_key?(sym.to_sym) || respond_to_without_variables?(sym, include_priv) end |
#set(variable, *args, &block) ⇒ Object Also known as: []=
Set a variable to the given value.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/minestrone/configuration/variables.rb', line 19 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 @variables[sym] = value end |
#set_if_empty(variable, *args, &block) ⇒ Object
39 40 41 |
# File 'lib/minestrone/configuration/variables.rb', line 39 def set_if_empty(variable, *args, &block) set(variable, *args, &block) unless exists?(variable) end |
#unset(variable) ⇒ Object
Removes any trace of the given variable.
44 45 46 47 48 |
# File 'lib/minestrone/configuration/variables.rb', line 44 def unset(variable) sym = variable.to_sym @original_procs.delete(sym) @variables.delete(sym) end |