Class: CSVPlusPlus::Entities::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_plus_plus/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

Returns a new instance of Entity.

Parameters:

  • type (::String, Symbol)
  • id (::String, nil) (defaults to: nil)


17
18
19
20
# File 'lib/csv_plus_plus/entities/entity.rb', line 17

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

Parameters:

  • method_name (Symbol)

    The method_name to respond to



30
31
32
33
34
35
36
37
# File 'lib/csv_plus_plus/entities/entity.rb', line 30

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

#idSymbol (readonly)

The identifier of the entity. For functions this is the function name, for variables it’s the variable name

Returns:

  • (Symbol)

    the current value of id



12
13
14
# File 'lib/csv_plus_plus/entities/entity.rb', line 12

def id
  @id
end

#typeSymbol (readonly)

The type of the entity. Valid values are defined in ::CSVPlusPlus::Entities::TYPES

Returns:

  • (Symbol)

    the current value of type



12
13
14
# File 'lib/csv_plus_plus/entities/entity.rb', line 12

def type
  @type
end

Instance Method Details

#==(other) ⇒ boolean

Returns:

  • (boolean)


23
24
25
# File 'lib/csv_plus_plus/entities/entity.rb', line 23

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

#respond_to_missing?(method_name, *_arguments) ⇒ boolean

Respond to predicates by type (entity.boolean?, entity.string?, etc)

Parameters:

  • method_name (Symbol)

    The method_name to respond to

Returns:

  • (boolean)


44
45
46
# File 'lib/csv_plus_plus/entities/entity.rb', line 44

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