solidity-typed 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 634cc8c8ee7cf10c3a67c0a8dbcc7c5efad7fe17a5a961128a5140f7648a8ff0
4
- data.tar.gz: f6956a85d7b3e7ae5aa021052ddcc9981f0c5f394acb29f0457e2587a3cc8e56
3
+ metadata.gz: 6f81ad150dc806a0f2762f4d012dd5b2f64a6957f1a6b44b366823fab8a05b07
4
+ data.tar.gz: 269e9e51fa0a30e1b95d2abea121494bef11af559ec7f1063b6283c9315d15a3
5
5
  SHA512:
6
- metadata.gz: 9537d4935ea267c3ace17ac3625552fab903dee944718e9163392167c39cdcff478f3494c9780e09db327c6228458dac5d33e3738fddd3fa08463322d9fff99e
7
- data.tar.gz: 6210074c6e54f778fca18fbb738f8a884421702ceede73d73e4b5efd32ccce35ea2b30fd7507ed76f0d820753723c735bdd0f3af58ff252b480dfed2c9cf5833
6
+ metadata.gz: d614ebe7a9be302c64fec852d3c0105fd70574a9302315ac12d47a6dbedd434c5ec562eb00f657036bb20604ec32905644ad5af389e0fe318195c5ce26a8e892
7
+ data.tar.gz: 261b765c22ae3eb26c45e95ca9082dece2de4ee026d6cd74f9207db1fda6a02d040908d8bdd954e566333e6c5c2bb0d0b4423d57df2d4e1fad4b6ee7dd2d1ac1
data/CHANGELOG.md CHANGED
@@ -1,4 +1,4 @@
1
- ### 0.1.0
1
+ ### 0.2.0
2
2
  ### 0.0.1 / 2023-09-10
3
3
 
4
4
  * Everything is new. First release
data/Manifest.txt CHANGED
@@ -9,6 +9,8 @@ lib/solidity/typed/bool.rb
9
9
  lib/solidity/typed/conversion.rb
10
10
  lib/solidity/typed/enum.rb
11
11
  lib/solidity/typed/enum_builder.rb
12
+ lib/solidity/typed/event.rb
13
+ lib/solidity/typed/event_builder.rb
12
14
  lib/solidity/typed/mapping.rb
13
15
  lib/solidity/typed/mapping_builder.rb
14
16
  lib/solidity/typed/metatypes/array.rb
data/README.md CHANGED
@@ -312,7 +312,7 @@ at the ruby code commons (rubycocos) org.
312
312
  ## Questions? Comments?
313
313
 
314
314
  Join us in the [Rubidity (community) discord (chat server)](https://discord.gg/3JRnDUap6y). Yes you can.
315
- Your questions and commetary welcome.
315
+ Your questions and commentary welcome.
316
316
 
317
317
  Or post them over at the [Help & Support](https://github.com/geraldb/help) page. Thanks.
318
318
 
@@ -0,0 +1,68 @@
1
+
2
+ module Types
3
+ class Event < Typed
4
+
5
+ def self.zero
6
+ raise "event cannot be zero (by defintion); sorry"
7
+ end
8
+
9
+ def zero?() false; end
10
+
11
+
12
+
13
+ def initialize( *args, **kwargs )
14
+ ## fix-fix-fix: first check for matching args - why? why not?
15
+ if kwargs.size > 0 ## assume kwargs
16
+ ## note: kwargs.size check matching attributes "upstream" in new!!!
17
+ self.class.attributes.each do |key, type|
18
+ value = kwargs[ key ]
19
+ raise ArgumentError, "event arg with key >#{key}< missing; sorry" if value.nil?
20
+ value = if value.is_a?(Typed)
21
+ ## fix-fix-fix - check type match here!!!
22
+ value
23
+ else
24
+ type.new( value )
25
+ end
26
+ instance_variable_set( "@#{key}", value )
27
+ end
28
+ else
29
+ self.class.attributes.zip( args ).each do |(key, type), value|
30
+ value = if value.is_a?(Typed)
31
+ ## fix-fix-fix - check type match here!!!
32
+ value
33
+ else
34
+ type.new( value )
35
+ end
36
+ instance_variable_set( "@#{key}", value )
37
+ end
38
+ end
39
+ self ## note: return reference to self for chaining method calls
40
+ end
41
+
42
+
43
+ def as_data
44
+ self.class.attributes.keys.map do |key|
45
+ ivar = instance_variable_get( "@#{key}" )
46
+ puts " @#{key}:"
47
+ pp ivar
48
+ ivar.as_data
49
+ end
50
+ end
51
+
52
+
53
+ def ==(other)
54
+ if other.is_a?( self.class )
55
+ self.class.attributes.keys.all? do |key|
56
+ __send__( key ) == other.__send__( key )
57
+ end
58
+ else
59
+ false
60
+ end
61
+ end
62
+ ## do note override eql? why? why not?
63
+ # default is object id equality???
64
+ ## alias_method :eql?, :==
65
+
66
+
67
+ end # class Event
68
+ end # module Types
@@ -0,0 +1,101 @@
1
+
2
+ module Types
3
+ class Event
4
+
5
+
6
+ # todo/fix: scope: keep empty by default
7
+
8
+ def self.build_class( class_name, scope: Types, **attributes )
9
+
10
+ ## todo/fix:
11
+ ## check if valid class_name MUST start with uppercase letter etc.
12
+ ## todo/fix: check if constant is undefined in scoped namespace!!!!
13
+
14
+ attributes = attributes.map do |key,type|
15
+ [key, typeof( type )]
16
+ end.to_h
17
+
18
+
19
+ ## note: event is a like a value type e.g. only getters
20
+ ## no setters (can only initialized via constructor or such)
21
+ klass = Class.new( Event ) do
22
+ attributes.each do |key,type|
23
+ define_method( key ) do
24
+ instance_variable_get( "@#{key}" )
25
+ end
26
+ end
27
+
28
+
29
+ alias_method :old_freeze, :freeze # note: store "old" orginal version of freeze
30
+ define_method( :freeze ) do
31
+ old_freeze ## same as calling super
32
+ attributes.keys.each do |key|
33
+ instance_variable_get( "@#{key}" ).freeze
34
+ end
35
+ self # return reference to self
36
+ end
37
+ end
38
+
39
+
40
+ type = EventType.new( class_name, klass )
41
+ klass.define_singleton_method( :type ) do
42
+ @type ||= type
43
+ end
44
+
45
+ ## add attributes (with keys / types) class method
46
+ klass.define_singleton_method( :attributes ) do
47
+ attributes
48
+ end
49
+
50
+ ### todo/fix:
51
+ ## auto-add support for kwargs too!!!
52
+
53
+ ## add self.new too - note: call/forward to "old" orginal self.new of Event (base) class
54
+ klass.define_singleton_method( :new ) do |*args, **kwargs|
55
+ if kwargs.size > 0 ## assume kwargs
56
+ if kwargs.size != attributes.size
57
+ raise ArgumentError, "[Event] wrong number of arguments for #{name}.new - #{kwargs.size} for #{attributes.size}"
58
+ end
59
+ ## check for matching names too - why? why not?
60
+ if kwargs.keys.sort != attributes.keys.sort
61
+ raise ArgumentError, "[Event] argument key (names) not matching for #{name}.new - #{kwargs.key.sort} != #{attributes.key.sort} for #{attributes.size}"
62
+ end
63
+ old_new( **kwargs )
64
+ else
65
+ if args.size != attributes.size
66
+ ## check for required args/params - all MUST be passed in!!!
67
+ raise ArgumentError, "[Event] wrong number of arguments for #{name}.new - #{args.size} for #{attributes.size}"
68
+ end
69
+ old_new( *args )
70
+ end
71
+ end
72
+
73
+ =begin
74
+ ## note: use Kernel for "namespacing"
75
+ ## make all enums convenience converters (always) global
76
+ ## including uppercase methods (e.g. State(), Color(), etc.) does NOT work otherwise (with other module includes)
77
+
78
+ ## add global "Kernel" convenience converter function
79
+ ## e.g. Vote(0) is same as Vote.convert(0)
80
+ Kernel.class_eval( <<RUBY )
81
+ def #{class_name}( arg )
82
+ #{class_name}.convert( arg )
83
+ end
84
+ RUBY
85
+ =end
86
+
87
+ ## note: use scoped (module) and NOT Object for namespacing
88
+ ## use include Safe to make all structs global
89
+ ## fix-fix-fix - make class_name unique across contracts (e.g. reuse same name in different contract)
90
+ scope.const_set( class_name, klass ) ## returns klass (plus sets global constant class name)
91
+ end # method build_class
92
+
93
+
94
+ class << self
95
+ alias_method :old_new, :new # note: store "old" orginal version of new
96
+ alias_method :new, :build_class # replace original version with create
97
+ end
98
+
99
+
100
+ end # class Event
101
+ end # module Types
@@ -53,7 +53,7 @@ class Type
53
53
 
54
54
  ## todo/check - use a different base class for contracts - why? why not?
55
55
  ## fix fix fix: check matching contract type/class too - why? why not?
56
- if literal.is_a?( ContractBase )
56
+ if literal.is_a?( Contract )
57
57
  return literal
58
58
  else
59
59
  raise TypeError, "No literals allowed for contract types got: #{literal}; sorry"
@@ -16,7 +16,6 @@
16
16
  # global helper(s) - move to ??? - why? why not?
17
17
 
18
18
  def _sanitize_class_name( name )
19
- name = name.sub( /\bContractBase::/, '' ) ## remove contract module from name if present
20
19
  name = name.sub( /\bContract::/, '' ) ## remove contract module from name if present
21
20
  name = name.sub( /\bTyped::/, '' )
22
21
  name = name.sub( /\bTypes::/, '' )
@@ -2,7 +2,7 @@
2
2
  =begin
3
3
  class Object ### move to core_ext/object - why? why not?
4
4
  ## check - add scoped class here too - why? why not?
5
- ## e.g. is_a?( Typed ) || is_a?( ContractBase )
5
+ ## e.g. is_a?( Typed )
6
6
  ## or add a TypedContract delagate class or such - why? why not?
7
7
  ## fix - check for class has singelton method type - why? why not?
8
8
  def typed?() is_a?( Typed ); end
@@ -2,7 +2,7 @@ module Solidity
2
2
  module Module
3
3
  module Typed
4
4
  MAJOR = 0
5
- MINOR = 1
5
+ MINOR = 2
6
6
  PATCH = 0
7
7
  VERSION = [MAJOR,MINOR,PATCH].join('.')
8
8
 
@@ -15,7 +15,7 @@ class ValueError < StandardError; end
15
15
 
16
16
  ## forward declare contract base (from solidity)
17
17
  ## for type checking
18
- class ContractBase
18
+ class Contract
19
19
  end
20
20
 
21
21
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidity-typed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-09 00:00:00.000000000 Z
11
+ date: 2023-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc
@@ -67,6 +67,8 @@ files:
67
67
  - lib/solidity/typed/conversion.rb
68
68
  - lib/solidity/typed/enum.rb
69
69
  - lib/solidity/typed/enum_builder.rb
70
+ - lib/solidity/typed/event.rb
71
+ - lib/solidity/typed/event_builder.rb
70
72
  - lib/solidity/typed/mapping.rb
71
73
  - lib/solidity/typed/mapping_builder.rb
72
74
  - lib/solidity/typed/metatypes/array.rb