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

Methods inherited from TypedReference

#==, #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

Raises:

  • (ArgumentError)


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 = {} )
  ## was: initial_value ||= {}             
  ##     check if nil gets passed in - default not used?  
 
  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   = Type.create( key_type )    if key_type.is_a?( Symbol ) ||
    #                                            key_type.is_a?( String )
    # value_type = Type.create( value_type )  if value_type.is_a?( Symbol ) ||
    #                                            value_type.is_a?( String )
    # key_type   = key_type.type     if key_type.is_a?( Class ) && key_type.ancestors.include?( Typed )
    # value_type = value_type.type   if value_type.is_a?( Class ) && value_type.ancestors.include?( Typed )

    key_type   = typeof( key_type )
    value_type = typeof( value_type ) 

    type = MappingType.instance( key_type, value_type )

    class_name =  type.typedclass_name
  
    ## note: keep a class cache
    ##  note: klasses may have different init sizes (default 0)
    cache           = @@cache ||= {}
    klass           = cache[ class_name ]
    ## fix-fix-fix - check if const klass defined for cache (no cache needed)!!!!!!!!
  
    if klass.nil?
      klass = Class.new( Mapping )
      klass.define_singleton_method( :type ) do
        @type  ||= type
      end

      ## add self.new too - note: call/forward to "old" orginal self.new of Event (base) class
      klass.define_singleton_method( :new ) do |*args|
        old_new( *args )
      end

      ## add to cache for later (re)use
      cache[ class_name ] = klass
      Types.const_set( class_name, klass )
    end

    klass
end

.zeroObject



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]

  ## was:
  ## if value_type.mapping? && obj.nil?
  ##  obj = 
  ##  @data[key_var] = obj
  ## end
  ##
  ## note:
  ##  change to 
  ## allow access to ref to struct and than update change to assign !!=!!!!!!

  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?
    ## val_var = Proxy.new(keytype: valuetype.keytype, valuetype: valuetype.valuetype)
    raise 'What?'
  end

  @data[key_var] = obj
end

#as_dataObject

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)|
    ## todo/fix:
    ##   check if value is zero/ do not serialize zero - why? why not?
      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?

Returns:

  • (Boolean)


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_typeObject

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_typeObject



10
# File 'lib/solidity/typed/mapping.rb', line 10

def value_type() self.class.type.value_type; end

#zero?Boolean

Returns:

  • (Boolean)


6
# File 'lib/solidity/typed/mapping.rb', line 6

def zero?()  @data.empty? end