renamed inc -> increment

added decrement as well (pre-decremented)
This commit is contained in:
Michael Richardson
2010-09-02 09:41:43 -04:00
parent 381b4f5268
commit 3919ff6bc3
4 changed files with 74 additions and 22 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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