Class: Estreet::Identifier
- Inherits:
-
Expression
- Object
- Node
- Expression
- Estreet::Identifier
- Defined in:
- lib/estreet/identifier.rb
Instance Attribute Summary
Attributes inherited from Node
Class Method Summary collapse
-
.[](name) ⇒ Object
Returns an identifier created with name, or returns name if it is already an identifier.
- .valid?(name) ⇒ Boolean
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(name) ⇒ Identifier
constructor
A new instance of Identifier.
- #to_pattern ⇒ Object
-
#to_s ⇒ Object
Returns the identifier as a Ruby string.
Methods inherited from Expression
#[], #call, coerce, #property, #to_expression, #to_statement
Methods inherited from Node
Constructor Details
#initialize(name) ⇒ Identifier
Returns a new instance of Identifier.
4 5 6 7 |
# File 'lib/estreet/identifier.rb', line 4 def initialize(name) raise ArgumentError, "Invalid identifier: #{name}" unless self.class.valid?(name) @name = name.to_s end |
Class Method Details
.[](name) ⇒ Object
Returns an identifier created with name, or returns name if it is already an identifier.
23 24 25 26 |
# File 'lib/estreet/identifier.rb', line 23 def self.[](name) return name if name.is_a?(self) new(name) end |
.valid?(name) ⇒ Boolean
29 30 31 |
# File 'lib/estreet/identifier.rb', line 29 def self.valid?(name) !!JS_IDENTIFIER.match(name.to_s) end |
Instance Method Details
#attributes ⇒ Object
9 10 11 |
# File 'lib/estreet/identifier.rb', line 9 def attributes super.merge(name: @name) end |
#to_pattern ⇒ Object
13 14 15 |
# File 'lib/estreet/identifier.rb', line 13 def to_pattern self end |
#to_s ⇒ Object
Returns the identifier as a Ruby string.
18 19 20 |
# File 'lib/estreet/identifier.rb', line 18 def to_s @name.to_s # this allows us to do Identifier.new(Identifier.new('hello')) and get an identifier end |