Class: Rscons::VarSet
- Inherits:
-
Object
- Object
- Rscons::VarSet
- Defined in:
- lib/rscons/varset.rb
Overview
This class represents a collection of variables which supports efficient deep cloning.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access the value of variable.
-
#[]=(key, val) ⇒ Object
Assign a value to a variable.
-
#append(values) ⇒ VarSet
Add or overwrite a set of variables.
-
#expand_varref(varref, lambda_args) ⇒ nil, ...
Replace “${var}” variable references in varref with the expanded variables’ values, recursively.
-
#include?(key) ⇒ Boolean
Check if the VarSet contains a variable.
-
#initialize(vars = {}) ⇒ VarSet
constructor
Create a VarSet.
-
#inspect ⇒ String
Return a String representing the VarSet.
-
#merge(other = {}) ⇒ VarSet
(also: #clone)
Create a new VarSet object based on the first merged with other.
-
#to_h ⇒ Hash
Return a Hash containing all variables in the VarSet.
-
#values_at(*keys) ⇒ Array
Return an array containing the values associated with the given keys.
Constructor Details
#initialize(vars = {}) ⇒ VarSet
Create a VarSet.
8 9 10 11 12 |
# File 'lib/rscons/varset.rb', line 8 def initialize(vars = {}) @my_vars = {} @coa_vars = [] append(vars) end |
Instance Method Details
#[](key) ⇒ Object
Access the value of variable.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rscons/varset.rb', line 19 def [](key) if @my_vars.include?(key) @my_vars[key] else @coa_vars.each do |coa_vars| if coa_vars.include?(key) @my_vars[key] = deep_dup(coa_vars[key]) return @my_vars[key] end end nil end end |
#[]=(key, val) ⇒ Object
Assign a value to a variable.
38 39 40 |
# File 'lib/rscons/varset.rb', line 38 def []=(key, val) @my_vars[key] = val end |
#append(values) ⇒ VarSet
Add or overwrite a set of variables.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rscons/varset.rb', line 62 def append(values) coa! if values.is_a?(VarSet) values.send(:coa!) @coa_vars = values.instance_variable_get(:@coa_vars) + @coa_vars else @my_vars = deep_dup(values) end self end |
#expand_varref(varref, lambda_args) ⇒ nil, ...
Replace “${var}” variable references in varref with the expanded variables’ values, recursively.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/rscons/varset.rb', line 96 def (varref, lambda_args) if varref.is_a?(String) if varref =~ /^(.*)\$\{([^}]+)\}(.*)$/ prefix, varname, suffix = $1, $2, $3 varval = (self[varname], lambda_args) if varval.is_a?(String) or varval.nil? ("#{prefix}#{varval}#{suffix}", lambda_args) elsif varval.is_a?(Array) varval.map {|vv| ("#{prefix}#{vv}#{suffix}", lambda_args)}.flatten else raise "I do not know how to expand a variable reference to a #{varval.class.name} (from #{varname.inspect} => #{self[varname].inspect})" end else varref end elsif varref.is_a?(Array) varref.map do |ent| (ent, lambda_args) end.flatten elsif varref.is_a?(Proc) (varref[*lambda_args], lambda_args) elsif varref.nil? nil elsif varref.is_a?(Symbol) varref elsif varref.is_a?(TrueClass) varref elsif varref.is_a?(FalseClass) varref else raise "Unknown varref type: #{varref.class} (#{varref.inspect})" end end |
#include?(key) ⇒ Boolean
Check if the VarSet contains a variable.
47 48 49 50 51 52 53 54 55 |
# File 'lib/rscons/varset.rb', line 47 def include?(key) if @my_vars.include?(key) true else @coa_vars.find do |coa_vars| coa_vars.include?(key) end end end |
#inspect ⇒ String
Return a String representing the VarSet.
153 154 155 |
# File 'lib/rscons/varset.rb', line 153 def inspect to_h.inspect end |
#merge(other = {}) ⇒ VarSet Also known as: clone
Create a new VarSet object based on the first merged with other.
78 79 80 81 82 83 |
# File 'lib/rscons/varset.rb', line 78 def merge(other = {}) coa! varset = self.class.new varset.instance_variable_set(:@coa_vars, @coa_vars.dup) varset.append(other) end |
#to_h ⇒ Hash
Return a Hash containing all variables in the VarSet.
This method is not terribly efficient. It is intended to be used only by debugging code to dump out a VarSet’s variables.
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/rscons/varset.rb', line 138 def to_h result = deep_dup(@my_vars) @coa_vars.reduce(result) do |result, coa_vars| coa_vars.each_pair do |key, value| unless result.include?(key) result[key] = deep_dup(value) end end result end end |
#values_at(*keys) ⇒ Array
Return an array containing the values associated with the given keys.
164 165 166 167 168 |
# File 'lib/rscons/varset.rb', line 164 def values_at(*keys) keys.map do |key| self[key] end end |