Skip to content

Commit

Permalink
added pp2r method to Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ptomulik committed Jan 28, 2014
1 parent 865b7b5 commit a5d052b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
2014-01-28 Pawel Tomulik <ptomulik@meil.pw.edu.pl>
* added pp2r method to Scope
2014-01-27 Pawel Tomulik <ptomulik@meil.pw.edu.pl>
* release 0.0.15
* added call_macro to scope and fixed lambda binding issues
Expand Down
23 changes: 20 additions & 3 deletions lib/puppet/parser/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@
# Monkey patches to Scope, so we have convenient access to some methods from
# macros.
class Puppet::Parser::Scope
# FIXME: I believe I should use environment that is bound to scope, not the
# Macros.default_environment, but at the moment I have no idea where to find
# it
# Call macro registered with {Puppet::Parser::Macros.newmacro}
#
# @param name [String] macro name, e.g. `'foo::bar'`,
# @param args [Array] arguments to be provided to the macro,
# @param options [Hash] additional options, see {Puppet::Parser::Macros.call_macro}
# @param env [Puppet::Node::Environment] puppet environment,
# @return the result of macro evaluation
def call_macro(name, args = [], options = {}, env = Puppet::Parser::Macros.default_environment)
# FIXME: I believe I should use environment that is bound to scope, not the
# Macros.default_environment, but at the moment I have no idea where to
# find it
Puppet::Parser::Macros.call_macro(self,name,args,options,env)
end

# Convert puppet value to ruby.
#
# This converts empty strings and :undefs to nil.
#
# @param v [String|Symbol] value to be converted
# @return converted value
def pp2r(v)
(v.equal?(:undef) or v == '') ? nil : v
end
end

module Puppet::Parser::Macros; end
Expand Down
18 changes: 18 additions & 0 deletions spec/unit/puppet/parser/macros_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@

describe Puppet::Parser::Scope do
it { should respond_to :call_macro }
it { should respond_to :pp2r }
let(:subject) { PuppetlabsSpec::PuppetInternals.scope }
describe 'call_macro' do
context 'call_macro("foo")' do
Expand All @@ -651,5 +652,22 @@
subject.call_macro("foo",[],:opts,:env1)
end
end
describe 'pp2r' do
context 'pp2r(:foo)' do
it { subject.pp2r(:foo).should be :foo }
end
context 'pp2r("foo")' do
it { subject.pp2r("foo").should == "foo" }
end
context 'pp2r(nil)' do
it { subject.pp2r(nil).should be_nil }
end
context 'pp2r("")' do
it { subject.pp2r("").should be_nil }
end
context 'pp2r(:undef)' do
it { subject.pp2r(:undef).should be_nil }
end
end
end
end

0 comments on commit a5d052b

Please sign in to comment.