Class: Aspen::AST::Nodes::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/aspen/ast/nodes/type.rb

Constant Summary collapse

STRING =
"STRING"
INTEGER =
"INTEGER"
FLOAT =
"FLOAT"
MATCH_FLOAT =
/^([\d,]+\.\d+)$/
MATCH_INTEGER =
/^([\d,]+)$/
MATCH_STRING =
/^(.+)$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_const) ⇒ Type

Returns a new instance of Type.



28
29
30
31
# File 'lib/aspen/ast/nodes/type.rb', line 28

def initialize(type_const)
  @content   = Aspen::AST::Nodes::Content.new(type_const)
  @converter = get_converter(type_const)
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



26
27
28
# File 'lib/aspen/ast/nodes/type.rb', line 26

def content
  @content
end

#converterObject (readonly)

Returns the value of attribute converter.



26
27
28
# File 'lib/aspen/ast/nodes/type.rb', line 26

def converter
  @converter
end

Class Method Details

.determine(value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/aspen/ast/nodes/type.rb', line 14

def self.determine(value)
  new(
    case value
    when MATCH_FLOAT   then FLOAT
    when MATCH_INTEGER then INTEGER
    when MATCH_STRING  then STRING            
    else
      raise ArgumentError, "Could not determine a type for value:\n\t#{value.inspect}"
    end
  )
end

Instance Method Details

#get_converter(type_const) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/aspen/ast/nodes/type.rb', line 33

def get_converter(type_const)
  case type_const
  when FLOAT   then :to_f
  when INTEGER then :to_i
  when STRING  then :to_s
  else
    raise ArgumentError, "Could not determine a converter method for type:\n\t#{value.inspect}"
  end
end