Method: Liquid::Utils.slice_collection_using_each

Defined in:
lib/liquid/utils.rb

.slice_collection_using_each(collection, from, to) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/liquid/utils.rb', line 13

def self.slice_collection_using_each(collection, from, to)
  segments = []
  index    = 0

  # Maintains Ruby 1.8.7 String#each behaviour on 1.9
  if collection.is_a?(String)
    return collection.empty? ? [] : [collection]
  end
  return [] unless collection.respond_to?(:each)

  collection.each do |item|
    if to && to <= index
      break
    end

    if from <= index
      segments << item
    end

    index += 1
  end

  segments
end