Class: Luoma::Variable

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

Instance Attribute Summary collapse

Attributes inherited from Expression

#span, #token

Instance Method Summary collapse

Methods inherited from Expression

#scope

Constructor Details

#initialize(token, root, segments) ⇒ Variable

(t_token, Name | StringLiteral | Variable, Array)

Parameters:



630
631
632
633
634
635
# File 'lib/luoma/expression.rb', line 630

def initialize(token, root, segments)
  super(token)
  @root = root
  @segments = segments
  @span = segments.empty? ? root.span : Luoma.span(root.span, segments.last.span)
end

Instance Attribute Details

#rootName, ... (readonly)

Returns the value of attribute root.

Returns:



627
628
629
# File 'lib/luoma/expression.rb', line 627

def root
  @root
end

#segmentsArray[t_path_segment] (readonly)

Returns the value of attribute segments.

Returns:

  • (Array[t_path_segment])


627
628
629
# File 'lib/luoma/expression.rb', line 627

def segments
  @segments
end

Instance Method Details

#childrenObject



660
661
662
663
664
665
666
# File 'lib/luoma/expression.rb', line 660

def children
  if @root.is_a?(Variable)
    [@root, *@segments.grep(Variable)] #: Array[_Traversable]
  else
    @segments.filter.grep(Variable) #: Array[_Traversable]
  end
end

#evaluate(context) ⇒ Object

(RenderContext) -> untyped

Parameters:

Returns:

  • (Object)


638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/luoma/expression.rb', line 638

def evaluate(context)
  root_segment = @root.is_a?(Variable) ? @root.evaluate(context) : @root.value # steep:ignore
  root = root_segment.is_a?(String) ? context.scopes.fetch(root_segment, :nothing) : :nothing

  obj, index = if @segments.empty?
                 [root, 0]
               else
                 context.resolve_path(root, @segments.map { |s| s.evaluate(context) })
               end

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

#ident?(name) ⇒ Boolean

Return true if this variable has a root segment matching name and no other segments.

Signature:

  • (String) -> bool

Parameters:

  • name (String)

Returns:

  • (Boolean)


676
677
678
# File 'lib/luoma/expression.rb', line 676

def ident?(name)
  @segments.empty? && @root.is_a?(Name) && @root.value == name # steep:ignore
end

#path(segments) ⇒ String

(Array) -> String

Parameters:

  • segments (Array[t_path_segment])

Returns:

  • (String)


683
684
685
686
687
688
689
# File 'lib/luoma/expression.rb', line 683

def path(segments)
  root = @root.is_a?(Name) ? @root.to_s : "[#{@root}]"
  return root if segments.empty?

  segments_ = segments.map { |s| s.is_a?(Name) || s.is_a?(Predicate) ? ".#{s}" : "[#{s}]" }.join
  "#{root}#{segments_}"
end

#to_sString

() -> String

Returns:

  • (String)


669
670
671
# File 'lib/luoma/expression.rb', line 669

def to_s
  path(@segments)
end