Class: Types::Struct
- Inherits:
-
Typed
- Object
- Typed
- Types::Struct
show all
- Defined in:
- lib/solidity/typed/struct.rb,
lib/solidity/typed/struct_builder.rb
Constant Summary
Constants inherited
from Typed
Typed::ADDRESS_ZERO, Typed::BYTES20_ZERO, Typed::BYTES32_ZERO, Typed::BYTES_ZERO, Typed::INSCRIPTION_ID_ZERO, Typed::STRING_ZERO
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Typed
#as_json, dump, serialize, #serialize, #type, type
Constructor Details
#initialize(*args, **kwargs) ⇒ Struct
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/solidity/typed/struct.rb', line 18
def initialize( *args, **kwargs )
if kwargs.size > 0
self.class.attributes.each do |key, type|
value = kwargs.has_key?( key ) ? kwargs[ key ] : type.new_zero
value = if value.is_a?( Typed )
value
else
type.new( value )
end
instance_variable_set( "@#{key}", value )
end
else
self.class.attributes.zip( args ).each do |(key, type), value|
value = if value.is_a?(Typed)
value
else
type.new( value )
end
instance_variable_set( "@#{key}", value )
end
end
self
end
|
Class Method Details
.build_class(class_name, scope: Types, **attributes) ⇒ Object
Also known as:
new
todo/fix: scope: keep empty by default
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/solidity/typed/struct_builder.rb', line 8
def self.build_class( class_name, scope: Types, **attributes )
attributes = attributes.map do |key,type|
[key, typeof( type )]
end.to_h
klass = Class.new( Struct ) do
attributes.each do |key,type|
define_method( key ) do
instance_variable_get( "@#{key}" )
end
if type == BoolType.instance
define_method( "#{key}?" ) do
instance_variable_get( "@#{key}" )
end
end
define_method( "#{key}=" ) do |value|
value = if value.is_a?(Typed)
value
else
type.new( value )
end
instance_variable_set( "@#{key}", value )
end
end
alias_method :old_freeze, :freeze
define_method( :freeze ) do
old_freeze
attributes.keys.each do |key|
instance_variable_get( "@#{key}" ).freeze
end
self
end
end
type = StructType.new( class_name, klass )
klass.define_singleton_method( :type ) do
@type ||= type
end
klass.define_singleton_method( :attributes ) do
attributes
end
klass.define_singleton_method( :new ) do |*args, **kwargs|
if kwargs.size > 0
old_new( **kwargs )
else
if args.empty?
new_zero
else
if args.size != attributes.size
raise ArgumentError, "[Struct] wrong number of arguments for #{name}.new - #{args.size} for #{attributes.size}"
end
old_new( *args )
end
end
end
klass.define_singleton_method( :new_zero ) do
values = attributes.values.map do |type|
if type.respond_to?( :new_zero )
type.new_zero
else
raise ArgumentError, "[Struct] no new_zero support for type #{type}; sorry"
end
end
old_new( *values )
end
scope.const_set( class_name, klass )
end
|
.zero ⇒ Object
5
6
7
8
9
10
11
12
|
# File 'lib/solidity/typed/struct.rb', line 5
def self.zero
@zero ||= new_zero.freeze
end
|
Instance Method Details
#==(other) ⇒ Object
58
59
60
61
62
63
64
65
66
|
# File 'lib/solidity/typed/struct.rb', line 58
def ==(other)
if other.is_a?( self.class )
self.class.attributes.keys.all? do |key|
__send__( key ) == other.__send__( key )
end
else
false
end
end
|
#as_data ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/solidity/typed/struct.rb', line 47
def as_data
self.class.attributes.keys.map do |key|
ivar = instance_variable_get( "@#{key}" )
puts " @#{key}:"
pp ivar
ivar.as_data
end
end
|
#zero? ⇒ Boolean
14
|
# File 'lib/solidity/typed/struct.rb', line 14
def zero?() self == self.class.zero; end
|