From 3919ff6bc387bd690dcf0784128b640d699e5a32 Mon Sep 17 00:00:00 2001 From: Michael Richardson Date: Thu, 2 Sep 2010 09:41:43 -0400 Subject: [PATCH] renamed inc -> increment added decrement as well (pre-decremented) --- lib/liquid/tags/decrement.rb | 42 ++++++++++++++++++++++++ lib/liquid/tags/{inc.rb => increment.rb} | 13 ++++---- test/inc_tag_test.rb | 16 --------- test/lib/liquid/tags/inc_tag_test.rb | 25 ++++++++++++++ 4 files changed, 74 insertions(+), 22 deletions(-) create mode 100644 lib/liquid/tags/decrement.rb rename lib/liquid/tags/{inc.rb => increment.rb} (66%) delete mode 100644 test/inc_tag_test.rb create mode 100644 test/lib/liquid/tags/inc_tag_test.rb diff --git a/lib/liquid/tags/decrement.rb b/lib/liquid/tags/decrement.rb new file mode 100644 index 0000000..285d88e --- /dev/null +++ b/lib/liquid/tags/decrement.rb @@ -0,0 +1,42 @@ +require 'rubygems' +require 'ruby-debug' + +module Liquid + + # decrement is used in a place where one needs to insert a counter + # into a template, and needs the counter to survive across + # multiple instantiations of the template. + # NOTE: decrement is a pre-decrement, --i, + # while increment is post: i++. + # + # (To achieve the survival, the application must keep the context) + # + # if the variable does not exist, it is created with value 0. + + # Hello: {% decrement variable %} + # + # gives you: + # + # Hello: -1 + # Hello: -2 + # Hello: -3 + # + class Decrement < Tag + def initialize(tag_name, markup, tokens) + @variable = markup.strip + + super + end + + def render(context) + value = context.environments.first[@variable] ||= 0 + value = value - 1 + context.environments.first[@variable] = value + value.to_s + end + + private + end + + Template.register_tag('decrement', Decrement) +end diff --git a/lib/liquid/tags/inc.rb b/lib/liquid/tags/increment.rb similarity index 66% rename from lib/liquid/tags/inc.rb rename to lib/liquid/tags/increment.rb index 6c277cb..1519ff1 100644 --- a/lib/liquid/tags/inc.rb +++ b/lib/liquid/tags/increment.rb @@ -1,15 +1,16 @@ -#require 'rubygems' -#require 'ruby-debug' +require 'rubygems' +require 'ruby-debug' module Liquid - # inc is used in a place where one needs to insert a counter + # increment is used in a place where one needs to insert a counter # into a template, and needs the counter to survive across # multiple instantiations of the template. + # (To achieve the survival, the application must keep the context) # # if the variable does not exist, it is created with value 0. - # Hello: {% inc variable %} + # Hello: {% increment variable %} # # gives you: # @@ -17,7 +18,7 @@ module Liquid # Hello: 1 # Hello: 2 # - class Inc < Tag + class Increment < Tag def initialize(tag_name, markup, tokens) @variable = markup.strip @@ -33,5 +34,5 @@ module Liquid private end - Template.register_tag('inc', Inc) + Template.register_tag('increment', Increment) end diff --git a/test/inc_tag_test.rb b/test/inc_tag_test.rb deleted file mode 100644 index 1c93f74..0000000 --- a/test/inc_tag_test.rb +++ /dev/null @@ -1,16 +0,0 @@ -require File.dirname(__FILE__) + '/helper' - - -class IncTagTest < Test::Unit::TestCase - include Liquid - - def test_inc - assert_template_result('0','{%inc port %}', {}) - assert_template_result('0 1','{%inc port %} {%inc port%}', {}) - assert_template_result('0 0 1 2 1', - '{%inc port %} {%inc starboard%} ' + - '{%inc port %} {%inc port%} ' + - '{%inc starboard %}', {}) - end - -end diff --git a/test/lib/liquid/tags/inc_tag_test.rb b/test/lib/liquid/tags/inc_tag_test.rb new file mode 100644 index 0000000..eabf495 --- /dev/null +++ b/test/lib/liquid/tags/inc_tag_test.rb @@ -0,0 +1,25 @@ +require 'test_helper' + + +class IncTagTest < Test::Unit::TestCase + include Liquid + + def test_inc + assert_template_result('0','{%increment port %}', {}) + assert_template_result('0 1','{%increment port %} {%increment port%}', {}) + assert_template_result('0 0 1 2 1', + '{%increment port %} {%increment starboard%} ' + + '{%increment port %} {%increment port%} ' + + '{%increment starboard %}', {}) + end + + def test_dec + assert_template_result('9','{%decrement port %}', { 'port' => 10}) + assert_template_result('-1 -2','{%decrement port %} {%decrement port%}', {}) + assert_template_result('1 5 2 2 5', + '{%increment port %} {%increment starboard%} ' + + '{%increment port %} {%decrement port%} ' + + '{%decrement starboard %}', { 'port' => 1, 'starboard' => 5 }) + end + +end