diff options
author | elijah <elijah@riseup.net> | 2012-11-01 21:47:36 -0700 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2012-11-01 21:47:36 -0700 |
commit | 60833da3e6d76e8cc2172ff58a79941de8b884dd (patch) | |
tree | fa7e1368b67699af759dfee80aeedbab524ce1df /lib/leap_cli/config | |
parent | 31c8c23bbb7e575be7c2231833dfec880c3d9d16 (diff) | |
download | leap_cli-60833da3e6d76e8cc2172ff58a79941de8b884dd.tar.gz leap_cli-60833da3e6d76e8cc2172ff58a79941de8b884dd.tar.bz2 |
fixed bugs in ruby 1.9 related to our use of eval.
Diffstat (limited to 'lib/leap_cli/config')
-rw-r--r-- | lib/leap_cli/config/object.rb | 78 |
1 files changed, 41 insertions, 37 deletions
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. |