From ff822155ea2e2c3b9404f71680f87a889c50d026 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 10 Jul 2013 17:18:33 -0400 Subject: [PATCH] add VarSet class to keep track of construction variables --- lib/rscons.rb | 1 + lib/rscons/environment.rb | 17 +++++------------ lib/rscons/varset.rb | 29 +++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 lib/rscons/varset.rb diff --git a/lib/rscons.rb b/lib/rscons.rb index bc97101..e34fb97 100644 --- a/lib/rscons.rb +++ b/lib/rscons.rb @@ -1,6 +1,7 @@ require "rscons/builder" require "rscons/cache" require "rscons/environment" +require "rscons/varset" require "rscons/version" require "rscons/monkey/module" diff --git a/lib/rscons/environment.rb b/lib/rscons/environment.rb index 938e781..d4a0d3f 100644 --- a/lib/rscons/environment.rb +++ b/lib/rscons/environment.rb @@ -11,7 +11,7 @@ module Rscons # uppercase strings (such as "CC" or "LDFLAGS"), and rscons options, # which are lowercase symbols (such as :echo). def initialize(variables = {}) - @variables = variables + @variables = VarSet.new(variables) @targets = {} @builders = {} @variables[:exclude_builders] ||= [] @@ -44,19 +44,12 @@ module Rscons end end - def [](key, type = nil) - val = @variables[key] - if type == :array and val.is_a?(String) - [val] - elsif type == :string and val.is_a?(Array) - val.first - else - val - end + def [](*args) + @variables.send(:[], *args) end - def []=(key, val) - @variables[key] = val + def []=(*args) + @variables.send(:[]=, *args) end def process diff --git a/lib/rscons/varset.rb b/lib/rscons/varset.rb new file mode 100644 index 0000000..8ce60a1 --- /dev/null +++ b/lib/rscons/varset.rb @@ -0,0 +1,29 @@ +module Rscons + class VarSet + attr_reader :vars + + def initialize(vars = {}) + @vars = vars + end + + def [](key, type = nil) + val = @vars[key] + if type == :array and val.is_a?(String) + [val] + elsif type == :string and val.is_a?(Array) + val.first + else + val + end + end + + def []=(key, val) + @vars[key] = val + end + + def merge(other) + other = other.vars if other.is_a?(VarSet) + VarSet.new(@vars.merge(other)) + end + end +end