Module: Liquid::Utils

Defined in:
lib/liquid/utils.rb

Class Method Summary collapse

Class Method Details

.non_blank_string?(collection) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/liquid/utils.rb', line 12

def self.non_blank_string?(collection)
  collection.is_a?(String) && collection != ''.freeze
end

.slice_collection(collection, from, to) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/liquid/utils.rb', line 4

def self.slice_collection(collection, from, to)
  if (from != 0 || to != nil) && collection.respond_to?(:load_slice)
    collection.load_slice(from, to)
  else
    slice_collection_using_each(collection, from, to)
  end
end

.slice_collection_using_each(collection, from, to) ⇒ Object



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

def self.slice_collection_using_each(collection, from, to)
  segments = []
  index = 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