Class: CSVPlusPlus::Entities::Entity
- Inherits:
-
Object
- Object
- CSVPlusPlus::Entities::Entity
- Defined in:
- lib/csv_plus_plus/entities/entity.rb
Overview
A basic building block of the abstract syntax tree (AST)
Direct Known Subclasses
Boolean, CellReference, Date, EntityWithArguments, Number, RuntimeValue, String, Variable
Instance Attribute Summary collapse
-
#id ⇒ Symbol
readonly
The identifier of the entity.
-
#type ⇒ Symbol
readonly
The type of the entity.
Instance Method Summary collapse
- #==(other) ⇒ boolean
-
#initialize(type, id: nil) ⇒ Entity
constructor
A new instance of Entity.
-
#method_missing(method_name, *_arguments) ⇒ Object
Respond to predicates that correspond to types like #boolean?, #string?, etc.
-
#respond_to_missing?(method_name, *_arguments) ⇒ boolean
Respond to predicates by type (entity.boolean?, entity.string?, etc).
Constructor Details
#initialize(type, id: nil) ⇒ Entity
Returns a new instance of Entity.
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
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
#id ⇒ Symbol (readonly)
The identifier of the entity. For functions this is the function name, for variables it’s the variable name
12 13 14 |
# File 'lib/csv_plus_plus/entities/entity.rb', line 12 def id @id end |
#type ⇒ Symbol (readonly)
The type of the entity. Valid values are defined in ::CSVPlusPlus::Entities::TYPES
12 13 14 |
# File 'lib/csv_plus_plus/entities/entity.rb', line 12 def type @type end |
Instance Method Details
#==(other) ⇒ 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)
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 |