Exception: Types::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Types::Error
- Defined in:
- lib/solidity/typed/error.rb,
lib/solidity/typed/error_builder.rb
Class Method Summary collapse
-
.build_class(class_name, scope: Types, **attributes) ⇒ Object
(also: new)
todo/fix: scope: keep empty by default.
- .zero ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #as_data ⇒ Object
- #as_json ⇒ Object
-
#initialize(*args, **kwargs) ⇒ Error
constructor
A new instance of Error.
-
#serialize ⇒ Object
keep serialize and/or as_json - why? why not?.
- #type ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(*args, **kwargs) ⇒ Error
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 |
# File 'lib/solidity/typed/error.rb', line 22 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, "error 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
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 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 92 93 |
# File 'lib/solidity/typed/error_builder.rb', line 7 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: error is a like a value type e.g. only getters ## no setters (can only initialized via constructor or such) klass = Class.new( Error ) 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 ## check - if Typed:: qualifier needed here? ## note Error class NOT derived from Typed (but StandardError)!!!! ## thus requires qualifier!!! type = Typed::ErrorType.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 Error (base) class klass.define_singleton_method( :new ) do |*args, **kwargs| if kwargs.size > 0 ## assume kwargs if kwargs.size != attributes.size raise ArgumentError, "[Error] 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, "[Error] 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, "[Error] 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 |
.zero ⇒ Object
15 16 17 |
# File 'lib/solidity/typed/error.rb', line 15 def self.zero raise "error cannot be zero (by defintion); sorry" end |
Instance Method Details
#==(other) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/solidity/typed/error.rb', line 66 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_data ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/solidity/typed/error.rb', line 52 def as_data self.class.attributes.keys.map do |key| ivar = instance_variable_get( "@#{key}" ) puts " @#{key}:" pp ivar ivar.as_data end end |
#as_json ⇒ Object
62 |
# File 'lib/solidity/typed/error.rb', line 62 def as_json() as_data; end |
#serialize ⇒ Object
keep serialize and/or as_json - why? why not?
61 |
# File 'lib/solidity/typed/error.rb', line 61 def serialize() as_data; end |
#type ⇒ Object
14 |
# File 'lib/solidity/typed/error.rb', line 14 def type() self.class.type; end |
#zero? ⇒ Boolean
18 |
# File 'lib/solidity/typed/error.rb', line 18 def zero?() false; end |