module Liquid::Utils

Public Class Methods

slice_collection(collection, from, to) click to toggle source
# File lib/liquid/utils.rb, line 5
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) click to toggle source
# 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
to_date(obj) click to toggle source
# File lib/liquid/utils.rb, line 65
def self.to_date(obj)
  return obj if obj.respond_to?(:strftime)

  if obj.is_a?(String)
    return nil if obj.empty?
    obj = obj.downcase
  end

  case obj
  when 'now', 'today'
    Time.now
  when /\A\d+\z/, Integer
    Time.at(obj.to_i)
  when String
    Time.parse(obj)
  end
rescue ::ArgumentError
  nil
end
to_integer(num) click to toggle source
# File lib/liquid/utils.rb, line 38
def self.to_integer(num)
  return num if num.is_a?(Integer)
  num = num.to_s
  begin
    Integer(num)
  rescue ::ArgumentError
    raise Liquid::ArgumentError, "invalid integer"
  end
end
to_liquid_value(obj) click to toggle source
# File lib/liquid/utils.rb, line 85
def self.to_liquid_value(obj)
  # Enable "obj" to represent itself as a primitive value like integer, string, or boolean
  return obj.to_liquid_value if obj.respond_to?(:to_liquid_value)

  # Otherwise return the object itself
  obj
end
to_number(obj) click to toggle source
# File lib/liquid/utils.rb, line 48
def self.to_number(obj)
  case obj
  when Float
    BigDecimal(obj.to_s)
  when Numeric
    obj
  when String
    /\A-?\d+\.\d+\z/.match?(obj.strip) ? BigDecimal(obj) : obj.to_i
  else
    if obj.respond_to?(:to_number)
      obj.to_number
    else
      0
    end
  end
end