Module: LiquidUtils

Defined in:
lib/liquid_nested_sort/nested_filters.rb

Class Method Summary collapse

Class Method Details

.accepts?(input, index) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/liquid_nested_sort/nested_filters.rb', line 46

def self.accepts?(input, index)
  !input[index].nil?
rescue TypeError
  false
end

.nested_respond_to?(input, property) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/liquid_nested_sort/nested_filters.rb', line 22

def self.nested_respond_to?(input, property)
  return true if property.empty?

  p = property.split('.').first
  q = property[/#{p}.?([\w.]*)/, 1]

  return nested_respond_to?(input[p], q) if input.respond_to?(:[]) && accepts?(input, p)
  return nested_respond_to?(input.send(p), q) if input.respond_to?(p)

  false
end

.nested_send(input, property) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/liquid_nested_sort/nested_filters.rb', line 34

def self.nested_send(input, property)
  return input if property.empty?

  p = property.split('.').first
  q = property[/#{p}.?([\w.]*)/, 1]

  return nested_send(input[p], q) if input.respond_to?(:[]) && accepts?(input, p)
  return nested_send(input.send(p), q) if input.respond_to?(p)

  nil
end

.nil_safe_casecmp(a, b) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/liquid_nested_sort/nested_filters.rb', line 14

def self.nil_safe_casecmp(a, b)
  if !a.nil? && !b.nil?
    a.to_s.casecmp(b.to_s)
  else
    a.nil? ? 1 : -1
  end
end

.nil_safe_compare(a, b) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/liquid_nested_sort/nested_filters.rb', line 6

def self.nil_safe_compare(a, b)
  if !a.nil? && !b.nil?
    a <=> b
  else
    a.nil? ? 1 : -1
  end
end