Class: Luoma::Variable
- Inherits:
-
Expression
- Object
- Expression
- Luoma::Variable
- Defined in:
- lib/luoma/expression.rb,
sig/luoma/expression.rbs
Instance Attribute Summary collapse
-
#root ⇒ Name, ...
readonly
Returns the value of attribute root.
-
#segments ⇒ Array[t_path_segment]
readonly
Returns the value of attribute segments.
Attributes inherited from Expression
Instance Method Summary collapse
- #children ⇒ Object
-
#evaluate(context) ⇒ Object
(RenderContext) -> untyped.
-
#ident?(name) ⇒ Boolean
Return true if this variable has a root segment matching
nameand no other segments. -
#initialize(token, root, segments) ⇒ Variable
constructor
(t_token, Name | StringLiteral | Variable, Array).
-
#path(segments) ⇒ String
(Array) -> String.
-
#to_s ⇒ String
() -> String.
Methods inherited from Expression
Constructor Details
#initialize(token, root, segments) ⇒ Variable
(t_token, Name | StringLiteral | Variable, Array)
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
#root ⇒ Name, ... (readonly)
Returns the value of attribute root.
627 628 629 |
# File 'lib/luoma/expression.rb', line 627 def root @root end |
#segments ⇒ Array[t_path_segment] (readonly)
Returns the value of attribute segments.
627 628 629 |
# File 'lib/luoma/expression.rb', line 627 def segments @segments end |
Instance Method Details
#children ⇒ Object
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
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.
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
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_s ⇒ String
() -> String
669 670 671 |
# File 'lib/luoma/expression.rb', line 669 def to_s path(@segments) end |