Class: Estreet::Identifier

Inherits:
Expression show all
Defined in:
lib/estreet/identifier.rb

Instance Attribute Summary

Attributes inherited from Node

#source_location

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#[], #call, coerce, #property, #to_expression, #to_statement

Methods inherited from Node

#as_json, #loc, #type

Constructor Details

#initialize(name) ⇒ Identifier

Returns a new instance of Identifier.

Parameters:

Raises:

  • (ArgumentError)


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

Returns:

  • (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

#attributesObject



9
10
11
# File 'lib/estreet/identifier.rb', line 9

def attributes
  super.merge(name: @name)
end

#to_patternObject



13
14
15
# File 'lib/estreet/identifier.rb', line 13

def to_pattern
  self
end

#to_sObject

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