From 60833da3e6d76e8cc2172ff58a79941de8b884dd Mon Sep 17 00:00:00 2001 From: elijah Date: Thu, 1 Nov 2012 21:47:36 -0700 Subject: fixed bugs in ruby 1.9 related to our use of eval. --- lib/leap_cli/config/object.rb | 78 +++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 37 deletions(-) (limited to 'lib/leap_cli/config/object.rb') diff --git a/lib/leap_cli/config/object.rb b/lib/leap_cli/config/object.rb index 06a4fef..8b14c49 100644 --- a/lib/leap_cli/config/object.rb +++ b/lib/leap_cli/config/object.rb @@ -43,11 +43,6 @@ module LeapCli ## FETCHING VALUES ## - # - # like a normal hash [], except: - # * lazily eval dynamic values when we encounter them. - # * support for nested hashes (e.g. ['a.b'] is the same as ['a']['b']) - # def [](key) get(key) end @@ -67,13 +62,22 @@ module LeapCli end end + # + # Like a normal Hash#[], except: + # + # (1) lazily eval dynamic values when we encounter them. (i.e. strings that start with "= ") + # + # (2) support for nested references in a single string (e.g. ['a.b'] is the same as ['a']['b']) + # the dot path is always absolute, starting at the top-most object. + # def get!(key) key = key.to_s if key =~ /\./ + # for keys with with '.' in them, we start from the root object (@node). keys = key.split('.') - value = get!(keys.first) + value = @node.get!(keys.first) if value.is_a? Config::Object - value.get!(keys[1..-1]) + value.get!(keys[1..-1].join('.')) else value end @@ -136,6 +140,35 @@ module LeapCli self end + ## + ## MACROS + ## these are methods used when eval'ing a value in the .json configuration + ## + + # + # the list of all the nodes + # + def nodes + global.nodes + end + + # + # inserts the contents of a file + # + def file(filename) + filepath = Path.find_file(@node.name, filename) + if filepath + if filepath =~ /\.erb$/ + ERB.new(File.read(filepath), nil, '%<>').result(binding) + else + File.read(filepath) + end + else + log0('no such file, "%s"' % filename) + "" + end + end + private # @@ -150,7 +183,7 @@ module LeapCli else if value =~ /^= (.*)$/ begin - value = eval($1, @node.send(:binding)) + value = @node.instance_eval($1) #, @node.send(:binding)) self[key] = value rescue SystemStackError => exc puts "STACK OVERFLOW, BAILING OUT" @@ -169,35 +202,6 @@ module LeapCli end end - ## - ## MACROS - ## these are methods used when eval'ing a value in the .json configuration - ## - - # - # the list of all the nodes - # - def nodes - global.nodes - end - - # - # inserts the contents of a file - # - def file(filename) - filepath = Path.find_file(@node.name, filename) - if filepath - if filepath =~ /\.erb$/ - ERB.new(File.read(filepath), nil, '%<>').result(binding) - else - File.read(filepath) - end - else - log0('no such file, "%s"' % filename) - "" - end - end - # # Output json from ruby objects in such a manner that all the hashes and arrays are output in alphanumeric sorted order. # This is required so that our generated configs don't throw puppet or git for a tizzy fit. -- cgit v1.2.3