Module: Bridgetown::Filters::ConditionHelpers

Included in:
Bridgetown::Filters, Tags::Find
Defined in:
lib/bridgetown-core/filters/condition_helpers.rb

Instance Method Summary collapse

Instance Method Details

#parse_binary_comparison(parser) ⇒ Object

Generate a Liquid::Condition object from a Liquid::Parser object additionally processing the parsed expression based on whether the expression consists of binary operations with Liquid operators and or or

  • parser: an instance of Liquid::Parser

Returns an instance of Liquid::Condition



25
26
27
28
29
30
31
32
33
34
# File 'lib/bridgetown-core/filters/condition_helpers.rb', line 25

def parse_binary_comparison(parser)
  condition = parse_comparison(parser)
  first_condition = condition
  while (binary_operator = parser.id?("and") || parser.id?("or"))
    child_condition = parse_comparison(parser)
    condition.send(binary_operator, child_condition)
    condition = child_condition
  end
  first_condition
end

#parse_comparison(parser) ⇒ Object

Generates a Liquid::Condition object from a Liquid::Parser object based on whether the parsed expression involves a "comparison" operator (e.g. <, ==, >, !=, etc)

  • parser: an instance of Liquid::Parser

Returns an instance of Liquid::Condition



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bridgetown-core/filters/condition_helpers.rb', line 43

def parse_comparison(parser)
  left_operand = Liquid::Expression.parse(parser.expression)
  operator     = parser.consume?(:comparison)

  # No comparison-operator detected. Initialize a Liquid::Condition using only left operand
  return Liquid::Condition.new(left_operand) unless operator

  # Parse what remained after extracting the left operand and the `:comparison` operator
  # and initialize a Liquid::Condition object using the operands and the comparison-operator
  Liquid::Condition.new(left_operand, operator, Liquid::Expression.parse(parser.expression))
end

#parse_condition(exp) ⇒ Object

Parse a string to a Liquid Condition



10
11
12
13
14
15
16
# File 'lib/bridgetown-core/filters/condition_helpers.rb', line 10

def parse_condition(exp)
  parser    = Liquid::Parser.new(exp)
  condition = parse_binary_comparison(parser)

  parser.consume(:end_of_string)
  condition
end