Class: TypeDef::DataConstructor
- Inherits:
-
Object
- Object
- TypeDef::DataConstructor
show all
- Defined in:
- lib/thaip/type_def.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, data_container, *args, **argkw) ⇒ DataConstructor
Returns a new instance of DataConstructor.
27
28
29
30
31
32
33
34
35
|
# File 'lib/thaip/type_def.rb', line 27
def initialize(name, data_container, *args, **argkw)
@name = name
@data_container = data_container
@vals = {}
@args = args
@argkw = argkw
Object.const_set(name, self)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/thaip/type_def.rb', line 89
def method_missing(name, *args)
if name.to_s.end_with?('=')
key = name.to_s[0..-2].to_sym
return super(name, *args) unless @vals.key?(key)
type = @argkw[key]
value = args[0]
raise "Expected type <#{type}> for value <#{value}>" unless value.nil? || value.is_a?(type)
@vals[key] = value
else
return super(name, *args) unless @vals.key?(name)
@vals[name]
end
end
|
Instance Attribute Details
#vals ⇒ Object
Returns the value of attribute vals.
25
26
27
|
# File 'lib/thaip/type_def.rb', line 25
def vals
@vals
end
|
Instance Method Details
#===(other) ⇒ Object
54
55
56
57
58
|
# File 'lib/thaip/type_def.rb', line 54
def ===(other)
return is_a?(other) if other.is_a?(DataContainer)
is_a?(other.type)
end
|
#[](index) ⇒ Object
106
107
108
109
|
# File 'lib/thaip/type_def.rb', line 106
def [](index)
raise "Index out of bounds" if index >= @vals.size
@vals.values[index]
end
|
#[]=(index, val) ⇒ Object
111
112
113
114
115
116
|
# File 'lib/thaip/type_def.rb', line 111
def []=(index, val)
raise "Index out of bounds" if index >= @vals.size
@vals[@vals.keys[index]] = val
end
|
#__class ⇒ Object
118
|
# File 'lib/thaip/type_def.rb', line 118
alias_method :__class, :class
|
#class ⇒ Object
119
120
121
|
# File 'lib/thaip/type_def.rb', line 119
def class
@name
end
|
#defined?(name) ⇒ Boolean
84
85
86
87
|
# File 'lib/thaip/type_def.rb', line 84
def defined?(name)
return true if @vals.key?(name)
super(name)
end
|
#is_a?(a_type) ⇒ Boolean
46
47
48
49
50
51
52
|
# File 'lib/thaip/type_def.rb', line 46
def is_a?(a_type)
if a_type.respond_to?(:__class) && a_type.__class == DataConstructor
return self.class == a_type.class
end
a_type === type
end
|
#new(*args, **kwargs) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/thaip/type_def.rb', line 60
def new(*args, **kwargs)
self_clone = clone
self_clone.vals = {}
@args.zip(args).each_with_index do |(type, arg), index|
raise "Expected type <#{type}> for value <#{arg}>" unless arg.nil? || arg.is_a?(type)
key = "#{type}-#{index}"
self_clone.vals[key] = arg
end
@argkw.each do |k, type|
raise "Expected type <#{type}> for value <#{kwargs[k]}>" unless kwargs[k].nil? || kwargs[k].is_a?(type)
self_clone.vals[k] = kwargs[k]
end
self_clone
end
|
#to_s ⇒ Object
Also known as:
inspect
37
38
39
|
# File 'lib/thaip/type_def.rb', line 37
def to_s
"<#{@name}#{@vals.map { |k, v| " #{k}:#{v}"}.join}>"
end
|
#type ⇒ Object
42
43
44
|
# File 'lib/thaip/type_def.rb', line 42
def type
@data_container
end
|
#value ⇒ Object
80
81
82
|
# File 'lib/thaip/type_def.rb', line 80
def value
@vals.values[0]
end
|