Class: CSVPlusPlus::Language::Entities::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_plus_plus/language/entities/entity.rb

Overview

A basic building block of the abstract syntax tree (AST)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, id: nil) ⇒ Entity

initialize



13
14
15
16
# File 'lib/csv_plus_plus/language/entities/entity.rb', line 13

def initialize(type, id: nil)
  @type = type.to_sym
  @id = id.downcase.to_sym if id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_arguments) ⇒ Object

Respond to predicates that correspond to types like #boolean?, #string?, etc



24
25
26
27
28
29
30
31
# File 'lib/csv_plus_plus/language/entities/entity.rb', line 24

def method_missing(method_name, *_arguments)
  if method_name =~ /^(\w+)\?$/
    t = ::Regexp.last_match(1)
    a_type?(t) && @type == t.to_sym
  else
    super
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/csv_plus_plus/language/entities/entity.rb', line 10

def id
  @id
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/csv_plus_plus/language/entities/entity.rb', line 10

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



19
20
21
# File 'lib/csv_plus_plus/language/entities/entity.rb', line 19

def ==(other)
  self.class == other.class && @type == other.type && @id == other.id
end

#respond_to_missing?(method_name, *_arguments) ⇒ Boolean

support predicates by type

Returns:



34
35
36
# File 'lib/csv_plus_plus/language/entities/entity.rb', line 34

def respond_to_missing?(method_name, *_arguments)
  (method_name =~ /^(\w+)\?$/ && a_type?(::Regexp.last_match(1))) || super
end