Module: Liquid::Utils

Defined in:
lib/liquid/utils.rb

Constant Summary collapse

DECIMAL_REGEX =
/\A-?\d+\.\d+\z/
UNIX_TIMESTAMP_REGEX =
/\A\d+\z/

Class Method Summary collapse

Class Method Details

.array_inspect(arr, seen = {}) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/liquid/utils.rb', line 203

def self.array_inspect(arr, seen = {})
  if seen[arr.object_id]
    return "[...]"
  end

  seen[arr.object_id] = true
  str = +"["
  cursor = 0
  len = arr.length

  while cursor < len
    if cursor > 0
      str << ", "
    end

    item_str = inspect(arr[cursor], seen)
    str << item_str
    cursor += 1
  end

  str << "]"
  str
ensure
  seen.delete(arr.object_id)
end

.hash_inspect(hash, seen = {}) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/liquid/utils.rb', line 229

def self.hash_inspect(hash, seen = {})
  if seen[hash.object_id]
    return "{...}"
  end
  seen[hash.object_id] = true

  str = +"{"
  first = true
  hash.each do |key, value|
    if first
      first = false
    else
      str << ", "
    end

    key_str = inspect(key, seen)
    str << key_str
    str << "=>"

    value_str = inspect(value, seen)
    str << value_str
  end
  str << "}"
  str
ensure
  seen.delete(hash.object_id)
end

.inspect(obj, seen = {}) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/liquid/utils.rb', line 185

def self.inspect(obj, seen = {})
  case obj
  when Hash
    # If the custom hash implementation overrides `#inspect`, use their
    # custom implementation. Otherwise we use Liquid's default
    # implementation.
    if obj.class.instance_method(:inspect) == HASH_INSPECT_METHOD
      hash_inspect(obj, seen)
    else
      obj.inspect
    end
  when Array
    array_inspect(obj, seen)
  else
    obj.inspect
  end
end

.slice_collection(collection, from, to) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/liquid/utils.rb', line 8

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_for_iteration(collection, from, to, resource_limits, allow_endless: false, use_range_to_a: false) ⇒ Object

This is intentionally separate from slice_collection, whose Array-returning behavior is used outside of the iteration tags.



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

def self.slice_collection_for_iteration(
  collection, from, to, resource_limits, allow_endless: false, use_range_to_a: false
)
  if integer_range?(collection)
    RangeSlice.new(collection, from, to, resource_limits)
  elsif collection.is_a?(Range)
    if use_range_to_a && range_method_overridden?(collection, :to_a)
      # For historically honored custom Range#to_a. Charge the resulting
      # selection before buffering it, just as for a custom #each.
      slice_collection_for_iteration_using_each(collection.to_a, from, to, resource_limits)
    else
      slice_range_using_each(collection, from, to, resource_limits, allow_endless: allow_endless)
    end
  else
    slice_collection(collection, from, to)
  end
end

.slice_collection_using_each(collection, from, to) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/liquid/utils.rb', line 36

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) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/liquid/utils.rb', line 137

def self.to_date(obj)
  return obj if obj.respond_to?(:strftime)

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

  case obj
  when 'now', 'today'
    Time.now
  when UNIX_TIMESTAMP_REGEX, Integer
    Time.at(obj.to_i)
  when String
    Time.parse(obj)
  end
rescue ::ArgumentError
  nil
end

.to_integer(num) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/liquid/utils.rb', line 110

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) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/liquid/utils.rb', line 157

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) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/liquid/utils.rb', line 120

def self.to_number(obj)
  case obj
  when Float
    BigDecimal(obj.to_s)
  when Numeric
    obj
  when String
    DECIMAL_REGEX.match?(obj.strip) ? BigDecimal(obj) : obj.to_i
  else
    if obj.respond_to?(:to_number)
      obj.to_number
    else
      0
    end
  end
end

.to_s(obj, seen = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/liquid/utils.rb', line 165

def self.to_s(obj, seen = {})
  case obj
  when BigDecimal
    obj.to_s("F")
  when Hash
    # If the custom hash implementation overrides `#to_s`, use their
    # custom implementation. Otherwise we use Liquid's default
    # implementation.
    if obj.class.instance_method(:to_s) == HASH_TO_S_METHOD
      hash_inspect(obj, seen)
    else
      obj.to_s
    end
  when Array
    array_inspect(obj, seen)
  else
    obj.to_s
  end
end