Skip to content

Commit

Permalink
Porting StaticMerger over to :lines, oh and:
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
judofyr committed Dec 21, 2009
1 parent 597c012 commit 5db6f9e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
30 changes: 17 additions & 13 deletions lib/temple/filters/static_merger.rb
Expand Up @@ -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

Expand Down
46 changes: 30 additions & 16 deletions spec/static_merger_spec.rb
Expand Up @@ -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

0 comments on commit 5db6f9e

Please sign in to comment.