Allow tablerow to work with any Enumerable. Closes #132

This commit is contained in:
Dylan Smith
2012-06-20 11:07:11 -04:00
parent 6ebdded8f2
commit d0184555d9
5 changed files with 59 additions and 38 deletions

View File

@@ -60,6 +60,7 @@ require 'liquid/htmltags'
require 'liquid/standardfilters'
require 'liquid/condition'
require 'liquid/module_ex'
require 'liquid/utils'
# Load all the tags of the standard library
#

View File

@@ -20,11 +20,10 @@ module Liquid
def render(context)
collection = context[@collection_name] or return ''
if @attributes['limit'] or @attributes['offset']
limit = context[@attributes['limit']] || -1
offset = context[@attributes['offset']] || 0
collection = collection[offset.to_i..(limit.to_i + offset.to_i - 1)]
end
from = @attributes['offset'] ? context[@attributes['offset']].to_i : 0
to = @attributes['limit'] ? from + context[@attributes['limit']].to_i - 1 : nil
collection = Utils.slice_collection_using_each(collection, from, to)
length = collection.length

View File

@@ -86,10 +86,10 @@ module Liquid
limit = context[@attributes['limit']]
to = limit ? limit.to_i + from : nil
segment = slice_collection_using_each(collection, from, to)
segment = Utils.slice_collection_using_each(collection, from, to)
return render_else(context) if segment.empty?
segment.reverse! if @reversed
@@ -119,30 +119,6 @@ module Liquid
end
result
end
def slice_collection_using_each(collection, from, to)
segments = []
index = 0
yielded = 0
# Maintains Ruby 1.8.7 String#each behaviour on 1.9
return [collection] if non_blank_string?(collection)
collection.each do |item|
if to && to <= index
break
end
if from <= index
segments << item
end
index += 1
end
segments
end
private
@@ -151,11 +127,7 @@ module Liquid
end
def iterable?(collection)
collection.respond_to?(:each) || non_blank_string?(collection)
end
def non_blank_string?(collection)
collection.is_a?(String) && collection != ''
collection.respond_to?(:each) || Utils.non_blank_string?(collection)
end
end

31
lib/liquid/utils.rb Normal file
View File

@@ -0,0 +1,31 @@
module Liquid
module Utils
def self.slice_collection_using_each(collection, from, to)
segments = []
index = 0
yielded = 0
# Maintains Ruby 1.8.7 String#each behaviour on 1.9
return [collection] if non_blank_string?(collection)
collection.each do |item|
if to && to <= index
break
end
if from <= index
segments << item
end
index += 1
end
segments
end
def self.non_blank_string?(collection)
collection.is_a?(String) && collection != ''
end
end
end

View File

@@ -3,6 +3,18 @@ require 'test_helper'
class HtmlTagTest < Test::Unit::TestCase
include Liquid
class ArrayDrop < Liquid::Drop
include Enumerable
def initialize(array)
@array = array
end
def each(&block)
@array.each(&block)
end
end
def test_html_table
assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 4 </td><td class=\"col2\"> 5 </td><td class=\"col3\"> 6 </td></tr>\n",
@@ -36,4 +48,10 @@ class HtmlTagTest < Test::Unit::TestCase
'collections' => {'frontpage' => [1,2,3,4,5,6]})
end
def test_enumerable_drop
assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 4 </td><td class=\"col2\"> 5 </td><td class=\"col3\"> 6 </td></tr>\n",
'{% tablerow n in numbers cols:3%} {{n}} {% endtablerow %}',
'numbers' => ArrayDrop.new([1,2,3,4,5,6]))
end
end # HtmlTagTest