Class: Types::Event

Inherits:
Typed
  • Object
show all
Defined in:
lib/solidity/typed/event.rb,
lib/solidity/typed/event_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) ⇒ Event

Returns a new instance of Event.



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
# File 'lib/solidity/typed/event.rb', line 13

def initialize( *args, **kwargs  )
   ## fix-fix-fix: first check for matching args - why? why not?
    if kwargs.size > 0   ## assume kwargs
      ## note: kwargs.size check matching attributes "upstream" in new!!!
      self.class.attributes.each do |key, type|
         value = kwargs[ key ]
         raise ArgumentError,  "event arg with key >#{key}< missing; sorry"  if value.nil?
         value = if value.is_a?(Typed)
                   ## fix-fix-fix - check type match here!!!
                   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)
                ## fix-fix-fix - check type match here!!!
                   value
                 else 
                   type.new( value )
                 end
         instance_variable_set( "@#{key}", value )
      end
   end
   self  ## note: return reference to self for chaining method calls
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
# File 'lib/solidity/typed/event_builder.rb', line 8

def self.build_class( class_name, scope: Types, **attributes )

 ## todo/fix:
 ## check if valid class_name MUST start with uppercase letter etc.
 ##  todo/fix: check if constant is undefined in scoped namespace!!!!

   attributes = attributes.map do |key,type|
                  [key, typeof( type )]
                end.to_h


  ## note: event is a like a value type e.g. only getters
  ##         no setters (can only initialized via constructor or such)              
  klass = Class.new( Event ) do
    attributes.each do |key,type|
      define_method( key ) do
        instance_variable_get( "@#{key}" )
      end
    end

    
    alias_method :old_freeze, :freeze   # note: store "old" orginal version of freeze
    define_method( :freeze ) do
      old_freeze    ## same as calling super
      attributes.keys.each do |key|
        instance_variable_get( "@#{key}" ).freeze
      end
      self   # return reference to self
    end
  end


  type = EventType.new( class_name, klass )
  klass.define_singleton_method( :type ) do
    @type ||= type       
  end

  ## add attributes (with keys / types) class method 
  klass.define_singleton_method( :attributes ) do
     attributes 
  end

  ### todo/fix:
  ##    auto-add support for kwargs too!!!

  ## add self.new too - note: call/forward to "old" orginal self.new of Event (base) class
  klass.define_singleton_method( :new ) do |*args, **kwargs|
      if kwargs.size > 0   ## assume kwargs
        if kwargs.size != attributes.size
          raise ArgumentError, "[Event] wrong number of arguments for #{name}.new - #{kwargs.size} for #{attributes.size}"
        end
        ## check for matching names too - why? why not?
        if kwargs.keys.sort != attributes.keys.sort
          raise ArgumentError, "[Event] argument key (names) not matching for #{name}.new - #{kwargs.key.sort} != #{attributes.key.sort} for #{attributes.size}"
        end  
        old_new( **kwargs )
      else
        if args.size != attributes.size
          ## check for required args/params - all MUST be passed in!!!
          raise ArgumentError, "[Event] wrong number of arguments for #{name}.new - #{args.size} for #{attributes.size}"
        end
        old_new( *args )
      end
  end

=begin
  ## note: use Kernel for "namespacing"
  ##   make all enums convenience converters (always) global
  ##     including uppercase methods (e.g. State(), Color(), etc.) does NOT work otherwise (with other module includes)

  ## add global "Kernel" convenience converter function
  ##  e.g. Vote(0) is same as Vote.convert(0)
  Kernel.class_eval( <<RUBY )
    def #{class_name}( arg )
       #{class_name}.convert( arg )
    end
RUBY
=end

 ## note: use scoped (module) and NOT Object for namespacing
 ##   use include Safe to make all structs global
 ##  fix-fix-fix - make class_name unique across contracts (e.g. reuse same name in different contract)
  scope.const_set( class_name, klass )   ## returns klass (plus sets global constant class name)
end

.zeroObject



5
6
7
# File 'lib/solidity/typed/event.rb', line 5

def self.zero
   raise "event cannot be zero (by defintion); sorry"
end

Instance Method Details

#==(other) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/solidity/typed/event.rb', line 53

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_dataObject



43
44
45
46
47
48
49
50
# File 'lib/solidity/typed/event.rb', line 43

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

Returns:

  • (Boolean)


9
# File 'lib/solidity/typed/event.rb', line 9

def zero?() false; end