From 5db6f9e09813703d866e5660450ee1bb39514dde Mon Sep 17 00:00:00 2001 From: Magnus Holm Date: Mon, 21 Dec 2009 23:13:37 +0100 Subject: [PATCH] Porting StaticMerger over to :lines, oh and: HOLY FUCK! I re-wrote the tests, renamed on_multi to on_lines, added another loop. After a few tweaks the first test passed. Then I tried it with the other tests: 0 failures. WTF? I don't know how, but at least it works. It probably makes sense, but I had a major mindfuck and now I'm to tired to understand it. --- lib/temple/filters/static_merger.rb | 30 +++++++++++-------- spec/static_merger_spec.rb | 46 +++++++++++++++++++---------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/lib/temple/filters/static_merger.rb b/lib/temple/filters/static_merger.rb index 9a9de97..7395e57 100644 --- a/lib/temple/filters/static_merger.rb +++ b/lib/temple/filters/static_merger.rb @@ -12,25 +12,29 @@ module Filters # [:static, "Hello World!"]] class StaticMerger def compile(exp) - exp.first == :multi ? on_multi(*exp[1..-1]) : exp + exp.first == :lines ? on_lines(*exp[1..-1]) : exp end - - def on_multi(*exps) - res = [:multi] + + def on_lines(*lines) + res = [:lines] + curr_line = nil curr = nil state = :looking - exps.each do |exp| - if exp.first == :static - if state == :looking - res << [:static, (curr = exp[1].dup)] - state = :static + lines.each do |line| + res << (curr_line = []) + line.each do |exp| + if exp.first == :static + if state == :looking + curr_line << [:static, (curr = exp[1].dup)] + state = :static + else + curr << exp[1] + end else - curr << exp[1] + curr_line << compile(exp) + state = :looking end - else - res << compile(exp) - state = :looking end end diff --git a/spec/static_merger_spec.rb b/spec/static_merger_spec.rb index d169062..1af1494 100644 --- a/spec/static_merger_spec.rb +++ b/spec/static_merger_spec.rb @@ -2,26 +2,40 @@ describe_filter :StaticMerger do it "should merge several statics" do - @filter.compile([:multi, - [:static, "Hello "], - [:static, "World, "], - [:static, "Good night"] - ]).should == [:multi, - [:static, "Hello World, Good night"] + @filter.compile([:lines, + [[:static, "Hello "], + [:static, "World, "], + [:static, "Good night"]] + ]).should == [:lines, + [[:static, "Hello World, Good night"]] ] end it "should merge several statics around block" do - @filter.compile([:multi, - [:static, "Hello "], - [:static, "World!"], - [:block, "123"], - [:static, "Good night, "], - [:static, "everybody"] - ]).should == [:multi, - [:static, "Hello World!"], - [:block, "123"], - [:static, "Good night, everybody"] + @filter.compile([:lines, + [[:static, "Hello "], + [:static, "World!"], + [:block, "123"], + [:static, "Good night, "], + [:static, "everybody"]] + ]).should == [:lines, + [[:static, "Hello World!"], + [:block, "123"], + [:static, "Good night, everybody"]] + ] + end + + it "should merge several statics across lines" do + @filter.compile([:lines, + [[:static, "Hello "]], + [[:static, "World!"], [:block, "123"]], + [[:block, "456"], [:static, "Good "]], + [[:static, "night, "], [:static, "everybody"]] + ]).should == [:lines, + [[:static, "Hello World!"]], + [[:block, "123"]], + [[:block, "456"], [:static, "Good night, everybody"]], + [] ] end end