Class: Luoma::GroupExpression

Inherits:
Expression show all
Defined in:
lib/luoma/expression.rb,
sig/luoma/expression.rbs

Instance Attribute Summary

Attributes inherited from Expression

#span, #token

Instance Method Summary collapse

Methods inherited from Expression

#scope

Constructor Details

#initialize(token, expr, segments) ⇒ GroupExpression

(t_token, Expression, Array) -> void

Parameters:

  • token (t_token)
  • expr (Expression)
  • segments (Array[t_path_segment])


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/luoma/expression.rb', line 38

def initialize(token, expr, segments)
  super(token)
  @expr = expr
  @segments = segments

  @span = if segments.empty?
            Luoma.span(token, expr.span)
          else
            Luoma.span(token, segments.last.token)
          end
end

Instance Method Details

#childrenObject



70
71
72
# File 'lib/luoma/expression.rb', line 70

def children
  [@expr, *@segments.grep(Variable)] #: Array[_Traversable]
end

#evaluate(context) ⇒ Object

Signature:

  • (RenderContext) -> untyped



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/luoma/expression.rb', line 51

def evaluate(context)
  obj = @expr.evaluate(context)

  return obj if @segments.empty?

  obj, index = context.resolve_path(obj, @segments.map { |s| s.evaluate(context) })

  if obj == :nothing
    context.env.undefined.new(
      path(@segments[0..index] || raise),
      @span,
      context.template.source,
      context.template.name
    )
  else
    obj
  end
end

#path(segments) ⇒ String

(Array) -> String

Parameters:

  • (Array[t_path_segment])

Returns:

  • (String)


87
88
89
90
91
92
93
# File 'lib/luoma/expression.rb', line 87

def path(segments)
  if segments.empty?
    ""
  else
    segments.map { |s| s.is_a?(Name) ? ".#{s}" : "[#{s}]" }.join
  end
end

#to_sObject

Signature:

  • () -> String



80
81
82
# File 'lib/luoma/expression.rb', line 80

def to_s
  "(#{@expr})#{path(@segments)}"
end

#with(token) ⇒ self

(t_token) -> self

Parameters:

  • token (t_token)

Returns:

  • (self)


43
44
45
46
# File 'sig/luoma/expression.rbs', line 43

def with(token)
  @span = Luoma.span(@token, token)
  self
end