Class: Liquid2::Identifier
- Inherits:
-
Expression
- Object
- Expression
- Liquid2::Identifier
- Defined in:
- lib/liquid2/expressions/identifier.rb
Overview
A single word identifier.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Attributes inherited from Expression
Class Method Summary collapse
-
.from(expr, trailing_question: true) ⇒ Object
Try to cast expr to an Identifier.
Instance Method Summary collapse
- #evaluate(_context) ⇒ Object
-
#initialize(token) ⇒ Identifier
constructor
A new instance of Identifier.
Methods inherited from Expression
Constructor Details
#initialize(token) ⇒ Identifier
Returns a new instance of Identifier.
34 35 36 37 |
# File 'lib/liquid2/expressions/identifier.rb', line 34 def initialize(token) super @name = token[1] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/liquid2/expressions/identifier.rb', line 8 def name @name end |
Class Method Details
.from(expr, trailing_question: true) ⇒ Object
Try to cast expr to an Identifier.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/liquid2/expressions/identifier.rb', line 12 def self.from(expr, trailing_question: true) # TODO: trailing question # TODO: expr might not have a token if its not a path. unless expr.is_a?(Path) && expr.segments.empty? raise LiquidSyntaxError.new("expected an identifier, found #{expr}", expr.token) end val = expr.head unless val.is_a?(String) raise LiquidSyntaxError.new("expected an identifier, found #{val}", expr.token) end # TODO: optimize unless val.to_s.match?(/[\u0080-\uFFFFa-zA-Z_][\u0080-\uFFFFa-zA-Z0-9_-]*/) raise LiquidSyntaxError.new("invalid identifier", expr.token) end new(expr.token) end |
Instance Method Details
#evaluate(_context) ⇒ Object
39 40 41 |
# File 'lib/liquid2/expressions/identifier.rb', line 39 def evaluate(_context) @name end |