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
+1
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
#
+4 -5
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
+5 -33
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
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