Class: Types::Mapping
- Inherits:
-
TypedReference
show all
- Extended by:
- Forwardable
- Defined in:
- lib/solidity/typed/mapping.rb,
lib/solidity/typed/mapping_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
#==, #eql?, #hash
Methods inherited from Typed
#as_json, dump, serialize, #serialize, #type, type
Constructor Details
#initialize(initial_value = {}) ⇒ Mapping
todo/check: make “internal” data/hash available? why? why not? attr_reader :data
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/solidity/typed/mapping.rb', line 15
def initialize( initial_value = {} )
raise ArgumentError, "expected literal of type #{type}; got typed #{initial_value.pretty_print_inspect}" if initial_value.is_a?( Typed )
@data = type.check_and_normalize_literal( initial_value ).map do |key, value|
[
type.key_type.new( key ),
type.value_type.new( value )
]
end.to_h
end
|
Class Method Details
.build_class(key_type, value_type) ⇒ Object
Also known as:
new
6
7
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
|
# File 'lib/solidity/typed/mapping_builder.rb', line 6
def self.build_class( key_type, value_type )
key_type = typeof( key_type )
value_type = typeof( value_type )
type = MappingType.instance( key_type, value_type )
class_name = type.typedclass_name
cache = @@cache ||= {}
klass = cache[ class_name ]
if klass.nil?
klass = Class.new( Mapping )
klass.define_singleton_method( :type ) do
@type ||= type
end
klass.define_singleton_method( :new ) do |*args|
old_new( *args )
end
cache[ class_name ] = klass
Types.const_set( class_name, klass )
end
klass
end
|
.zero ⇒ Object
5
|
# File 'lib/solidity/typed/mapping.rb', line 5
def self.zero() @zero ||= new; end
|
Instance Method Details
#[](key) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/solidity/typed/mapping.rb', line 36
def [](key)
puts "[debug] Mapping#[]( #{key} )"
key_var = key.is_a?( Typed ) ? key : key_type.new( key )
obj = @data[key_var]
if obj.nil?
obj = value_type.new_zero
@data[ key_var ] = obj
end
obj
end
|
#[]=(key, new_value) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/solidity/typed/mapping.rb', line 61
def []=(key, new_value)
puts "[debug] Mapping#[]=( #{key}:#{key.class.name}, #{new_value}:#{new_value.class.name})"
pp type.key_type
key_var = key.is_a?( Typed ) ? key : key_type.new( key )
pp key_var
obj = new_value.is_a?( Typed ) ? new_value : value_type.new( new_value )
pp obj
if value_type.mapping?
raise 'What?'
end
@data[key_var] = obj
end
|
#as_data ⇒ Object
note: pass through value!!!!
in new scheme - only "plain" ruby arrays/hash/string/integer/bool used!!!!
no wrappers!!! - no need to convert!!!
93
94
95
96
97
98
99
100
101
|
# File 'lib/solidity/typed/mapping.rb', line 93
def as_data
puts "[debug] Mapping#as_data"
@data.reduce({}) do |h, (k, v)|
h[k.as_data] = v.as_data
h
end
end
|
#delete(key) ⇒ Object
84
85
86
87
|
# File 'lib/solidity/typed/mapping.rb', line 84
def delete( key )
key_var = key.is_a?( Typed ) ? key : key_type.new( key )
@data.delete( key_var )
end
|
#key?(key) ⇒ Boolean
Also known as:
has_key?
78
79
80
81
|
# File 'lib/solidity/typed/mapping.rb', line 78
def key?( key )
key_var = key.is_a?( Typed ) ? key : key_type.new( key )
@data.key?( key_var )
end
|
#key_type ⇒ Object
add short-cut helpers why? why not?
9
|
# File 'lib/solidity/typed/mapping.rb', line 9
def key_type() self.class.type.key_type; end
|
#pretty_print(printer) ⇒ Object
103
104
105
|
# File 'lib/solidity/typed/mapping.rb', line 103
def pretty_print( printer )
printer.text( "<ref #{type}:#{@data.pretty_print_inspect}>" );
end
|
#value_type ⇒ Object
10
|
# File 'lib/solidity/typed/mapping.rb', line 10
def value_type() self.class.type.value_type; end
|
#zero? ⇒ Boolean
6
|
# File 'lib/solidity/typed/mapping.rb', line 6
def zero?() @data.empty? end
|