Class: Bridgetown::Tags::Find

Inherits:
Liquid::Tag
  • Object
show all
Includes:
Filters::ConditionHelpers, LiquidExtensions
Defined in:
bridgetown-core/lib/bridgetown-core/tags/find.rb

Constant Summary collapse

SYNTAX =
%r!^(.*?) (where|in) (.*?),(.*)$!
CONDITIONS_SEP =
"~FINDSEP~"

Instance Method Summary collapse

Methods included from LiquidExtensions

#lookup_variable

Methods included from Filters::ConditionHelpers

#parse_binary_comparison, #parse_comparison, #parse_condition

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Find

Returns a new instance of Find.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'bridgetown-core/lib/bridgetown-core/tags/find.rb', line 12

def initialize(tag_name, markup, tokens)
  super
  unless markup.strip =~ SYNTAX
    raise SyntaxError, <<~MSG
      Syntax Error in tag 'find' while parsing the following markup:

      #{markup}

      Valid syntax: find <varname> where|in <array>, <condition(s)>
    MSG
  end

  @new_var_name = Regexp.last_match(1).strip
  @single_or_group = Regexp.last_match(2)
  @arr_name = Regexp.last_match(3).strip
  @conditions = process_conditions(Regexp.last_match(4).strip)
end

Instance Method Details

#render(context) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'bridgetown-core/lib/bridgetown-core/tags/find.rb', line 30

def render(context)
  @group = lookup_variable(context, @arr_name)
  return "" unless @group.respond_to?(:select)

  @group = @group.values if @group.is_a?(Hash)

  expression = @conditions.split(CONDITIONS_SEP).map do |condition|
    "__find_tag_item__.#{condition.strip}"
  end.join(" and ")
  @liquid_condition = parse_condition(expression)

  context[@new_var_name] = @single_or_group == "where" ?
                             group_evaluate(context) :
                             single_evaluate(context)

  ""
end