Class: Liquid2::Path

Inherits:
Expression show all
Defined in:
lib/liquid2/expressions/path.rb

Overview

A path to some variable data. If the path has just one segment, it is often just called a "variable".

Constant Summary collapse

RE_PROPERTY =
/\A[\u0080-\uFFFFa-zA-Z_][\u0080-\uFFFFa-zA-Z0-9_-]*\Z/

Instance Attribute Summary collapse

Attributes inherited from Expression

#token

Instance Method Summary collapse

Methods inherited from Expression

#scope

Constructor Details

#initialize(token, segments) ⇒ Path

Returns a new instance of Path.

Parameters:

  • segments (Array[String | Integer | Path])


14
15
16
17
18
# File 'lib/liquid2/expressions/path.rb', line 14

def initialize(token, segments)
  super(token)
  @segments = segments
  @head = @segments.shift
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



9
10
11
# File 'lib/liquid2/expressions/path.rb', line 9

def head
  @head
end

#segmentsObject (readonly)

Returns the value of attribute segments.



9
10
11
# File 'lib/liquid2/expressions/path.rb', line 9

def segments
  @segments
end

Instance Method Details

#childrenObject



28
29
30
31
32
33
34
# File 'lib/liquid2/expressions/path.rb', line 28

def children
  if @head.is_a?(Path)
    [@head, *@segments.filter { |segment| segment.is_a?(Path) }]
  else
    @segments.filter { |segment| segment.is_a?(Path) }
  end
end

#evaluate(context) ⇒ Object



20
21
22
# File 'lib/liquid2/expressions/path.rb', line 20

def evaluate(context)
  context.fetch(@head, @segments, node: self)
end

#to_sObject



24
25
26
# File 'lib/liquid2/expressions/path.rb', line 24

def to_s
  segment_to_s(@head, head: true) + @segments.map { |segment| segment_to_s(segment) }.join
end