Skip to content

Commit

Permalink
Merge pull request #333 from BallAerospace/erb_local_string_vars
Browse files Browse the repository at this point in the history
render local string vars #332
  • Loading branch information
jmthomas committed Oct 5, 2016
2 parents 95a686d + 39f92e7 commit 812bcc5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/cosmos/config/config_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,16 @@ def error(message, usage = "", url = @url)
def render(template_name, options = {})
b = binding
if options[:locals]
options[:locals].each do |key, value|
eval("#{key} = #{value}", b)
if RUBY_VERSION.split('.')[0..1].join.to_i >= 21
options[:locals].each {|key, value| b.local_variable_set(key, value) }
else
options[:locals].each do |key, value|
if value.is_a? String
b.eval("#{key} = '#{value}'")
else
b.eval("#{key} = #{value}")
end
end
end
end
# Assume the file is there. If not we raise a pretty obvious error
Expand Down
42 changes: 40 additions & 2 deletions spec/config/config_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,45 @@ module Cosmos
tf.unlink
end

it "optionallies not remove quotes" do
it "supports ERB syntax" do
tf = Tempfile.new('unittest')
tf.puts "KEYWORD <%= 5 * 2 %>"
tf.close

@cp.parse_file(tf.path) do |keyword, params|
expect(keyword).to eql "KEYWORD"
expect(params[0]).to eql "10"
end
tf.unlink
end

it "supports ERB partials via render" do
tf2 = Tempfile.new('partial.txt')
tf2.puts '<% if output %>'
tf2.puts 'KEYWORD <%= id %> <%= desc %>'
tf2.puts '<% end %>'
tf2.close

# Run the test twice to verify the KEYWORD gets rendered and then doesn't
[true, false].each do |output|
tf = Tempfile.new('unittest')
tf.puts "<%= render '#{File.basename(tf2.path)}', locals: {id: 1, desc: 'Description', output: #{output}} %>"
tf.close

yielded = false
@cp.parse_file(tf.path) do |keyword, params|
yielded = true
expect(keyword).to eql "KEYWORD"
expect(params[0]).to eql "1"
expect(params[1]).to eql "Description"
end
expect(yielded).to eql output
tf.unlink
end
tf2.unlink
end

it "optionally does not remove quotes" do
tf = Tempfile.new('unittest')
line = "KEYWORD PARAM1 PARAM2 'PARAM 3'"
tf.puts line
Expand Down Expand Up @@ -106,7 +144,7 @@ module Cosmos
tf.unlink
end

it "optionallies yield comment lines" do
it "optionally yields comment lines" do
tf = Tempfile.new('unittest')
tf.puts "KEYWORD1 PARAM1"
tf.puts "# This is a comment"
Expand Down

0 comments on commit 812bcc5

Please sign in to comment.