Skip to content

Commit

Permalink
support for conditional logic in erb templates
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitri-d committed Feb 6, 2013
1 parent fa9508f commit 01af658
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/safemode/core_jails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def core_jail_methods(klass)
end

# these methods are allowed in all classes if they are present
@@default_methods = %w( % & * ** + +@ - -@ / < << <= <=> == === > >= >> ^ | ~
@@default_methods = %w( % & * ** + +@ - -@ / < << <= <=> != == === > >= >> ^ | ~
eql? equal? new methods is_a? kind_of? nil?
[] []= to_a to_jail to_s inspect to_param )

Expand Down
4 changes: 2 additions & 2 deletions lib/safemode/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def process_call(exp)
receiver = jail process_call_receiver(exp)
name = exp.shift
args = process_call_args(exp)

process_call_code(receiver, name, args)
end

Expand Down Expand Up @@ -149,7 +150,7 @@ def process_call_args(exp)

def process_call_code(receiver, name, args)
case name
when :<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :** then
when :<=>, :==, "!=".to_sym, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :** then
"(#{receiver} #{name} #{args})"
when :[] then
"#{receiver}[#{args}]"
Expand Down Expand Up @@ -195,7 +196,6 @@ def process_if(exp)
r = "if #{c} then\n#{indent(t)}\n"
r << "else\n#{indent(f)}\n" if f
r << "end"

r
else
# unless expand then
Expand Down
4 changes: 3 additions & 1 deletion test/test_erb_eval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def setup
end

def test_some_stuff_that_should_work
['"test".upcase', '10.succ', '10.times{}', '[1,2,3].each{|a| a + 1}', 'true ? 1 : 0', 'a = 1'].each do |code|
['"test".upcase', '10.succ', '10.times{}', '[1,2,3].each{|a| a + 1}',
'true ? 1 : 0', 'a = 1', 'unless "a" == "b"; "false"; end',
'if "a" != "b"; "true"; end'].each do |code|
code = ERB.new("<%= #{code} %>").src
assert_nothing_raised{ @box.eval code }
end
Expand Down
4 changes: 3 additions & 1 deletion test/test_safemode_eval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def setup
end

def test_some_stuff_that_should_work
['"test".upcase', '10.succ', '10.times{}', '[1,2,3].each{|a| a + 1}', 'true ? 1 : 0', 'a = 1'].each do |code|
['"test".upcase', '10.succ', '10.times{}', '[1,2,3].each{|a| a + 1}',
'true ? 1 : 0', 'a = 1', 'if "a" != "b"; "true"; end',
'if "a" == "b"; "true"; end'].each do |code|
assert_nothing_raised{ @box.eval code }
end
end
Expand Down

0 comments on commit 01af658

Please sign in to comment.