Skip to content

Commit

Permalink
Added forloop magic vars
Browse files Browse the repository at this point in the history
  • Loading branch information
rubinix committed Apr 6, 2011
1 parent a97e89c commit bf10a39
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/context.rb
Expand Up @@ -11,3 +11,7 @@ def put(key, value)
end

end

class ForLoopContext
attr_accessor :counter, :counter0, :revcounter, :revcounter0, :first, :last, :parentloop
end
18 changes: 17 additions & 1 deletion lib/parser.rb
Expand Up @@ -152,8 +152,24 @@ def initialize(var, enumerable, node_list)

def render(context)
compiled_string = StringIO.new
context.send(self.enumerable.to_sym).each do |obj|
context_enumerable = context.send(self.enumerable.to_sym)
first = context_enumerable[0]
last = context_enumerable[-1]
size = context_enumerable.size
context_enumerable.each_with_index do |obj, i|
c = Context.new(var.to_sym => obj)

loop_context = ForLoopContext.new
loop_context.first = first
loop_context.last = last
loop_context.counter0 = i
loop_context.counter = i+1
loop_context.revcounter = size
loop_context.revcounter0 = size - 1
size -= 1

c.put(:forloop, loop_context)

compiled_string << node_list.render(c)
end

Expand Down
36 changes: 35 additions & 1 deletion spec/hiromi_spec.rb
Expand Up @@ -49,11 +49,45 @@
end

context "when handling for block tags" do
let :context do
Context.new(:names => ['Foo', 'Bar'])
end
it "renders a block for each item in the collection" do
context.put(:names, ['Foo', 'Bar'])
hiromi = Hiromi.new("{% for name in names %}Name is: {{ name }}, {% endfor %}")
hiromi.render(context).should == "Name is: Foo, Name is: Bar, "
end

context "that contain magic forloop variables" do
it "can render a counter" do
hiromi = Hiromi.new("{% for name in names %}The counter is {{ forloop.counter }}, {% endfor %}")
hiromi.render(context).should == "The counter is 1, The counter is 2, "
end

it "can render a counter0" do
hiromi = Hiromi.new("{% for name in names %}The counter is {{ forloop.counter0 }}, {% endfor %}")
hiromi.render(context).should == "The counter is 0, The counter is 1, "
end

it "can render a revcounter" do
hiromi = Hiromi.new("{% for name in names %}The revcounter is {{ forloop.revcounter }}, {% endfor %}")
hiromi.render(context).should == "The revcounter is 2, The revcounter is 1, "
end

it "can render a revcounter0" do
hiromi = Hiromi.new("{% for name in names %}The revcounter0 is {{ forloop.revcounter0 }}, {% endfor %}")
hiromi.render(context).should == "The revcounter0 is 1, The revcounter0 is 0, "
end

it "can render first" do
hiromi = Hiromi.new("{% for name in names %}The first is {{ forloop.first }}, {% endfor %}")
hiromi.render(context).should == "The first is Foo, The first is Foo, "
end

it "can render last" do
hiromi = Hiromi.new("{% for name in names %}The last is {{ forloop.last }}, {% endfor %}")
hiromi.render(context).should == "The last is Bar, The last is Bar, "
end
end
end


Expand Down

0 comments on commit bf10a39

Please sign in to comment.