Module: JekyllFilters

Defined in:
lib/scribo/liquid/filters/jekyll_filters.rb

Instance Method Summary collapse

Instance Method Details

#compare_property_vs_target(property, target) ⇒ Object

‘where` filter helper



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/scribo/liquid/filters/jekyll_filters.rb', line 32

def compare_property_vs_target(property, target)
  case target
  when NilClass
    return true if property.nil?
  when Liquid::Expression::MethodLiteral # `empty` or `blank`
    target = target.to_s
    return true if property == target || Array(property).join == target
  else
    target = target.to_s
    if property.is_a? String
      return true if property == target
    else
      Array(property).each do |prop|
        return true if prop.to_s == target
      end
    end
  end

  false
end

#item_property(item, property) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/scribo/liquid/filters/jekyll_filters.rb', line 53

def item_property(item, property)
  # Jekyll uses :site here, but we use strings?
  @item_property_cache ||= @context.registers['site'].filter_cache[:item_property] ||= {}
  @item_property_cache[property] ||= {}
  @item_property_cache[property][item] ||= begin
    property = property.to_s
    property = if item.respond_to?(:to_liquid)
                 read_liquid_attribute(item.to_liquid, property)
               elsif item.respond_to?(:data)
                 item.data[property]
               else
                 item[property]
               end

    parse_sort_input(property)
  end
end

#parse_sort_input(property) ⇒ Object

return numeric values as numbers for proper sorting



84
85
86
87
88
89
90
# File 'lib/scribo/liquid/filters/jekyll_filters.rb', line 84

def parse_sort_input(property)
  stringified = property.to_s
  return property.to_i if INTEGER_LIKE.match?(stringified)
  return property.to_f if FLOAT_LIKE.match?(stringified)

  property
end

#read_liquid_attribute(liquid_data, property) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/scribo/liquid/filters/jekyll_filters.rb', line 71

def read_liquid_attribute(liquid_data, property)
  return liquid_data[property] unless property.include?('.')

  property.split('.').reduce(liquid_data) do |data, key|
    data.respond_to?(:[]) && data[key]
  end
end

#where(input, property, value) ⇒ Object

Filter an array of objects

input - the object array. property - the property within each object to filter by. value - the desired value.

Cannot be an instance of Array nor Hash since calling #to_s on them returns
their `#inspect` string object.

Returns the filtered array of objects



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/scribo/liquid/filters/jekyll_filters.rb', line 11

def where(input, property, value)
  return input if !property || value.is_a?(Array) || value.is_a?(Hash)
  return input unless input.respond_to?(:select)

  input    = input.values if input.is_a?(Hash)
  input_id = input.hash

  # implement a hash based on method parameters to cache the end-result
  # for given parameters.
  @where_filter_cache ||= {}
  @where_filter_cache[input_id] ||= {}
  @where_filter_cache[input_id][property] ||= {}

  # stash or retrive results to return
  @where_filter_cache[input_id][property][value] ||= input.select do |object|
    compare_property_vs_target(item_property(object, property), value)
  end.to_a
end