Method: Generator.mapping_getter_function

Defined in:
lib/rubysol/generator.rb

.mapping_getter_function(contract_class, name, type) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/rubysol/generator.rb', line 296

def self.mapping_getter_function( contract_class, name, type )
    ## note: make sure name is always a symbol

    name          = name.to_sym
    
    index = 0
    current_type = type

    sig_args = []
    while current_type.mapping? do
      sig_args << current_type.key_type
      current_type = current_type.value_type
      index += 1
    end

    
    puts "[debug]  auto-generate public mapping getter - #{name} : #{type} (#{contract_class.name}):"
    puts "sig_args:"
    pp sig_args
    ## auto-add/register sig(nature) in here - why? why not?

    ## contract_class.sig( name, sig_args, :view, returns: current_type)

    puts "    index: #{index}"
# {:arg0=>:address}

#    index: 1


    ## check - if sig_args is alreay array of types (or typedclasses???)

    sig_args = sig_args.map {|sig_arg| typeof(sig_arg) }

    contract_class.sigs[ name ] = { inputs:  sig_args,
                                    outputs: [typeof(current_type)],
                                    options: [:view, :public] }
    contract_class.sigs_exclude << name


    puts  contract_class.sigs[ name ].pretty_print_inspect
 

      contract_class.class_eval do
       ## note: hack: must use kwargs for now!!! arg0, arg1, arg2, for now

       define_method name do |arg0:, arg1: nil, arg2: nil, arg3: nil|
        puts "[debug] call public (state) mapping getter for #{name} : #{type} - index: #{index} (#{contract_class.name})"
        puts "[debug]  self -> #{self}"
        args = [arg0, arg1, arg2, arg3]
        puts "[debug]  args -> #{args.pretty_print_inspect}"
     
        ## raise ArgumentError, "expected #{index} argument(s); got #{args.size}" if args.size != index


        value = instance_variable_get( "@#{name}" )
         (0...index).each do |i|
           value = value[ args[i] ]
         end
        value
       end
    end # class_eval


    ## auto-add typed wrapper!!!!!

    typed_function( contract_class, name, inputs: sig_args )

end