Class: Rscons::VarSet

Inherits:
Object
  • Object
show all
Defined in:
lib/rscons/varset.rb

Overview

This class represents a collection of variables which supports efficient deep cloning.

Instance Method Summary collapse

Constructor Details

#initialize(vars = {}) ⇒ VarSet

Create a VarSet.

Parameters:

  • vars (Hash) (defaults to: {})

    Optional initial variables.



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 a variable.

Parameters:

  • key (String, Symbol)

    The variable name.

Returns:

  • (Object)

    The variable’s value.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rscons/varset.rb', line 21

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.

Parameters:

  • key (String, Symbol)

    The variable name.

  • val (Object)

    The value to set.



67
68
69
# File 'lib/rscons/varset.rb', line 67

def []=(key, val)
  @my_vars[key] = val
end

#append(values) ⇒ VarSet

Add or overwrite a set of variables.

Parameters:

  • values (VarSet, Hash)

    New set of variables.

Returns:



91
92
93
94
95
96
97
98
99
100
# File 'lib/rscons/varset.rb', line 91

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.

Parameters:

  • varref (nil, String, Array, Proc, Symbol, TrueClass, FalseClass)

    Value containing references to variables.

  • lambda_args (Array)

    Arguments to pass to any lambda variable values to be expanded.

Returns:

  • (nil, String, Array, Symbol, TrueClass, FalseClass)

    Expanded value with “${var}” variable references replaced.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rscons/varset.rb', line 125

def expand_varref(varref, lambda_args)
  case varref
  when String
    if varref =~ /^(.*)\$\{([^}]+)\}(.*)$/
      prefix, varname, suffix = $1, $2, $3
      prefix = expand_varref(prefix, lambda_args) unless prefix.empty?
      varval = expand_varref(get_var(varname), lambda_args)
      # suffix needs no expansion since the regex matches the last occurence
      case varval
      when Array
        if prefix.is_a?(Array)
          varval.map {|vv| prefix.map {|p| "#{p}#{vv}#{suffix}"}}.flatten
        else
          varval.map {|vv| "#{prefix}#{vv}#{suffix}"}
        end
      when String, Symbol, true, false, nil
        if prefix.is_a?(Array)
          prefix.map {|p| "#{p}#{varval}#{suffix}"}
        else
          "#{prefix}#{varval}#{suffix}"
        end
      else
        raise "Unknown construction variable type: #{varval.class} (from #{varname.inspect} => #{get_var(varname).inspect})"
      end
    else
      varref
    end
  when Array
    varref.map do |ent|
      expand_varref(ent, lambda_args)
    end.flatten
  when Symbol, true, false, nil
    varref
  when Proc
    expand_varref(varref[*lambda_args], lambda_args)
  else
    raise "Unknown construction variable type: #{varref.class} (#{varref.inspect})"
  end
end

#get_var(key) ⇒ Object

Access the value of a variable.

This method is similar to #[] but does not make a copy-on-access copy of the variable accessed. This means that the returned value is NOT safe to be modified by the caller. Thus the caller must guarantee that it does not modify the returned value.

Parameters:

  • key (String, Symbol)

    The variable name.

Returns:

  • (Object)

    The variable’s value.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rscons/varset.rb', line 47

def get_var(key)
  if @my_vars.include?(key)
    @my_vars[key]
  else
    @coa_vars.each do |coa_vars|
      if coa_vars.include?(key)
        return coa_vars[key]
      end
    end
    nil
  end
end

#include?(key) ⇒ Boolean

Check if the VarSet contains a variable.

Parameters:

  • key (String, Symbol)

    The variable name.

Returns:

  • (Boolean)

    Whether the VarSet contains the variable.



76
77
78
79
80
81
82
83
84
# File 'lib/rscons/varset.rb', line 76

def include?(key)
  if @my_vars.include?(key)
    true
  else
    @coa_vars.find do |coa_vars|
      coa_vars.include?(key)
    end
  end
end

#inspectString

Return a String representing the VarSet.

Returns:

  • (String)

    Representation of the VarSet.



186
187
188
# File 'lib/rscons/varset.rb', line 186

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.

Parameters:

  • other (VarSet, Hash) (defaults to: {})

    Other variables to add or overwrite.

Returns:

  • (VarSet)

    The newly created VarSet.



107
108
109
110
111
112
# File 'lib/rscons/varset.rb', line 107

def merge(other = {})
  coa!
  varset = self.class.new
  varset.instance_variable_set(:@coa_vars, @coa_vars.dup)
  varset.append(other)
end

#to_hHash

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.

Returns:

  • (Hash)

    All variables in the VarSet.



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rscons/varset.rb', line 171

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.

Parameters:

  • keys (Array<String, Symbol>)

    Keys to look up in the VarSet.

Returns:

  • (Array)

    An array containing the values associated with the given keys.



197
198
199
200
201
# File 'lib/rscons/varset.rb', line 197

def values_at(*keys)
  keys.map do |key|
    self[key]
  end
end