From ca2fa587cf224e59f46bbeda0de538fba1cf6217 Mon Sep 17 00:00:00 2001 From: Michael Richardson Date: Tue, 31 Aug 2010 16:17:12 -0400 Subject: [PATCH 1/6] added tag to increment a variable each time it is referenced --- lib/liquid/tags/inc.rb | 37 +++++++++++++++++++++++++++++++++++++ test/inc_tag_test.rb | 16 ++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 lib/liquid/tags/inc.rb create mode 100644 test/inc_tag_test.rb diff --git a/lib/liquid/tags/inc.rb b/lib/liquid/tags/inc.rb new file mode 100644 index 0000000..be140c0 --- /dev/null +++ b/lib/liquid/tags/inc.rb @@ -0,0 +1,37 @@ +#require 'rubygems' +#require 'ruby-debug' + +module Liquid + + # inc 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. + # + # if the variable does not exist, it is created with value 0. + + # Hello: {% inc variable %} + # + # gives you: + # + # Hello: 0 + # Hello: 1 + # Hello: 2 + # + class Inc < Tag + def initialize(tag_name, markup, tokens) + @variable = markup.strip + + super + end + + def render(context) + value = context.registers[@variable] ||= 0 + context.registers[@variable] = value + 1 + value.to_s + end + + private + end + + Template.register_tag('inc', Inc) +end diff --git a/test/inc_tag_test.rb b/test/inc_tag_test.rb new file mode 100644 index 0000000..cb4daa7 --- /dev/null +++ b/test/inc_tag_test.rb @@ -0,0 +1,16 @@ +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 From 97f18112b225dfe3c0348a3a450dbcd6fb4926a9 Mon Sep 17 00:00:00 2001 From: Michael Richardson Date: Tue, 31 Aug 2010 17:26:41 -0400 Subject: [PATCH 2/6] the variables should be stored in the normal environment, not in the register --- lib/liquid/tags/inc.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/liquid/tags/inc.rb b/lib/liquid/tags/inc.rb index be140c0..6c277cb 100644 --- a/lib/liquid/tags/inc.rb +++ b/lib/liquid/tags/inc.rb @@ -25,8 +25,8 @@ module Liquid end def render(context) - value = context.registers[@variable] ||= 0 - context.registers[@variable] = value + 1 + value = context.environments.first[@variable] ||= 0 + context.environments.first[@variable] = value + 1 value.to_s end From 381b4f526801a032b0f87ae488d49d846e12a8a8 Mon Sep 17 00:00:00 2001 From: Michael Richardson Date: Tue, 31 Aug 2010 19:42:13 -0400 Subject: [PATCH 3/6] adjusted test case to have third argument --- test/inc_tag_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/inc_tag_test.rb b/test/inc_tag_test.rb index cb4daa7..1c93f74 100644 --- a/test/inc_tag_test.rb +++ b/test/inc_tag_test.rb @@ -5,12 +5,12 @@ 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','{%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 %}') + '{%inc starboard %}', {}) end end From 3919ff6bc387bd690dcf0784128b640d699e5a32 Mon Sep 17 00:00:00 2001 From: Michael Richardson Date: Thu, 2 Sep 2010 09:41:43 -0400 Subject: [PATCH 4/6] 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 From bb035d96e1c16c27be802c712bc3a12d0cfacff7 Mon Sep 17 00:00:00 2001 From: Michael Richardson Date: Thu, 2 Sep 2010 09:46:21 -0400 Subject: [PATCH 5/6] ignore backups --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 2763bff..a1b3044 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*~ *.gem *.swp pkg From 58e5820e7a956641a68f9077b058aaa2a6d71005 Mon Sep 17 00:00:00 2001 From: Michael Richardson Date: Fri, 3 Sep 2010 10:54:29 -0400 Subject: [PATCH 6/6] thought that these were turned off, removed --- lib/liquid/tags/decrement.rb | 3 --- lib/liquid/tags/increment.rb | 3 --- 2 files changed, 6 deletions(-) diff --git a/lib/liquid/tags/decrement.rb b/lib/liquid/tags/decrement.rb index 285d88e..c22318e 100644 --- a/lib/liquid/tags/decrement.rb +++ b/lib/liquid/tags/decrement.rb @@ -1,6 +1,3 @@ -require 'rubygems' -require 'ruby-debug' - module Liquid # decrement is used in a place where one needs to insert a counter diff --git a/lib/liquid/tags/increment.rb b/lib/liquid/tags/increment.rb index 1519ff1..e6a30ca 100644 --- a/lib/liquid/tags/increment.rb +++ b/lib/liquid/tags/increment.rb @@ -1,6 +1,3 @@ -require 'rubygems' -require 'ruby-debug' - module Liquid # increment is used in a place where one needs to insert a counter