universum 0.2.0 → 0.3.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
  SHA1:
3
- metadata.gz: db4bf31c857f63ae6e203d9ddcdb718556c6f2a7
4
- data.tar.gz: 3989ec1e3962f0768a8a3c108e72a76b4c4b969b
3
+ metadata.gz: 70d14cee1fae619bb93bdf56c7a81b79987f16bb
4
+ data.tar.gz: 8ed7b6fde7aabaeb6430ebbc61c1863e50241064
5
5
  SHA512:
6
- metadata.gz: 276c1b77f06cf0e0dcb10a7a4ca4ef072365f1852c30e5b83279582f4d38447ad178c975c9e169e40aef5871a8d830cd8748d371c9d5846f3fd3211fe251ed07
7
- data.tar.gz: 7086a5e4984497cc116ee05eb397a34afc7e766474e5aacc802421a37c16c5c4031936dae03fbdecd8b9172c5d990c52391dc3a18b14620d4ae92b26e46405e4
6
+ metadata.gz: 6a06807b4ff6cd4a8b4d84dc688e8a9af995a08135aaca0fcf8b1f1d0c249cf0530560ca901ebcf9c189e4b11131037cd1862369e956a5117ac5a5ef9216287c
7
+ data.tar.gz: e3e45a313da7ee2c0e0094f61c5fc082e015b8a5d0dfcc3e291decbd430d48746fa10f0ad4db4f6fca171413fd6cb8a3af3aff5949e328ffddafe879fa731995
@@ -7,12 +7,11 @@ lib/universum.rb
7
7
  lib/universum/account.rb
8
8
  lib/universum/address.rb
9
9
  lib/universum/contract.rb
10
+ lib/universum/convert.rb
10
11
  lib/universum/enum.rb
11
12
  lib/universum/event.rb
12
13
  lib/universum/function.rb
13
14
  lib/universum/receipt.rb
14
- lib/universum/safe_array.rb
15
- lib/universum/safe_hash.rb
16
15
  lib/universum/transaction.rb
17
16
  lib/universum/type.rb
18
17
  lib/universum/units_money.rb
@@ -22,12 +21,9 @@ lib/universum/version.rb
22
21
  test/contracts/greeter.rb
23
22
  test/contracts/mytoken.rb
24
23
  test/helper.rb
25
- test/test_array.rb
26
24
  test/test_enum.rb
27
25
  test/test_event.rb
28
26
  test/test_greeter.rb
29
- test/test_hash.rb
30
- test/test_struct.rb
31
27
  test/test_units_money.rb
32
28
  test/test_units_time.rb
33
29
  test/test_version.rb
data/Rakefile CHANGED
@@ -18,6 +18,7 @@ Hoe.spec 'universum' do
18
18
  self.history_file = 'CHANGELOG.md'
19
19
 
20
20
  self.extra_deps = [
21
+ ['safestruct'],
21
22
  ]
22
23
 
23
24
  self.licenses = ['Public Domain']
@@ -9,6 +9,10 @@ require 'time'
9
9
  require 'uri'
10
10
 
11
11
 
12
+ ## 3rd party gems
13
+ require 'safestruct' # SafeArray, SafeHash, SafeStruct, etc.
14
+
15
+
12
16
 
13
17
  ## our own code
14
18
  require 'universum/version' # note: let version always go first
@@ -16,9 +20,6 @@ require 'universum/version' # note: let version always go first
16
20
  require 'universum/units_time'
17
21
  require 'universum/units_money'
18
22
 
19
- require 'universum/safe_array'
20
- require 'universum/safe_hash'
21
-
22
23
  require 'universum/enum'
23
24
  require 'universum/event'
24
25
 
@@ -33,6 +34,7 @@ require 'universum/receipt'
33
34
 
34
35
  require 'universum/universum'
35
36
 
37
+ require 'universum/convert'
36
38
 
37
39
 
38
40
  puts Universum.banner ## say hello
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # e.g. use Address(0)
5
+
6
+ def Address( arg )
7
+ if arg == 0 || arg == '0x0' || arg == '0x0000'
8
+ '0x0000' ## note: return a string for now and NOT (typed) Address.zero
9
+ else
10
+ ## assume string (hex) address
11
+ ## pass through for now
12
+ ##
13
+ ## use an address lookup in the future - why? why not?
14
+ ## Address.find_by_address( arg )
15
+ arg
16
+ end
17
+ end
18
+
19
+
20
+
21
+ ###
22
+ ## todo - add a global method_missing handler for (auto-)adding converters for Structs - why? why not?
23
+ ## e.g. how to auto-create Vote(0) for Vote.zero or Bet(0) for Bet.zero and so on - why? why not?
24
+ ##
25
+ ## def Vote( arg ) Vote.zero; end
26
+
@@ -36,6 +36,7 @@ class Event
36
36
  fields.zip( args ).each do |field, arg|
37
37
  instance_variable_set( "@#{field}", arg )
38
38
  end
39
+ freeze ### note: add freeze - assumes "immutable" struct-like class
39
40
  end
40
41
 
41
42
  fields.each do |field|
@@ -1,15 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
 
4
- ## add dummy bool class for mapping and (payable) method signature
5
-
6
- class Integer
7
- def self.zero() 0; end
8
- end
9
-
10
- class Bool
11
- def self.zero() false; end
12
- end
4
+ ## add more dummy classes for mapping and (payable) method signature
13
5
 
14
6
  class Money
15
7
  def self.zero() 0; end
@@ -17,33 +9,3 @@ end
17
9
 
18
10
  class Void ## only used (reserved) for (payable) method signature now
19
11
  end
20
-
21
-
22
- class Mapping
23
-
24
- def self.of( *args )
25
- ## e.g. gets passed in [{Address=>Integer}]
26
- ## check for Integer - use Hash.new(0)
27
- ## check for Bool - use Hash.new(False)
28
- if args[0].is_a? Hash
29
- arg = args[0].to_a ## convert to array (for easier access)
30
- klass_key = arg[0][0]
31
- klass_value = arg[0][1]
32
- klass = SafeHash.build_class( klass_key, klass_value )
33
- klass.new
34
- else
35
- ## todo/fix: throw argument error/exception
36
- Hash.new ## that is, "plain" {} with all "standard" defaults
37
- end
38
- end
39
- end
40
-
41
-
42
- class Array
43
- ## "typed" safe array "constructor"
44
- ## e.g. Array.of( Address ) or Array.of( Money ) or Array.of( Proposal, size: 2 ) etc.
45
- def self.of( klass_value )
46
- klass = SafeArray.build_class( klass_value )
47
- klass.new ## todo: add klass.new( **kwargs ) for size: 2 etc.
48
- end
49
- end
@@ -7,7 +7,7 @@
7
7
  class Universum
8
8
 
9
9
  MAJOR = 0
10
- MINOR = 2
10
+ MINOR = 3
11
11
  PATCH = 0
12
12
  VERSION = [MAJOR,MINOR,PATCH].join('.')
13
13
 
@@ -24,6 +24,7 @@ def test_bet_placed
24
24
  assert_equal '0xaaaa', bet.user
25
25
  assert_equal 2000, bet.cap
26
26
  assert_equal 100, bet.amount
27
+ assert_equal true, bet.frozen?
27
28
  end
28
29
 
29
30
  def test_roll
@@ -33,6 +34,7 @@ def test_roll
33
34
  assert roll.is_a?( Event )
34
35
  assert_equal 1, roll.id
35
36
  assert_equal 2345, roll.rolled
37
+ assert_equal true, roll.frozen?
36
38
  end
37
39
 
38
40
  end # class TestEvent
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: universum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.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: 2019-02-10 00:00:00.000000000 Z
11
+ date: 2019-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: safestruct
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rdoc
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -60,12 +74,11 @@ files:
60
74
  - lib/universum/account.rb
61
75
  - lib/universum/address.rb
62
76
  - lib/universum/contract.rb
77
+ - lib/universum/convert.rb
63
78
  - lib/universum/enum.rb
64
79
  - lib/universum/event.rb
65
80
  - lib/universum/function.rb
66
81
  - lib/universum/receipt.rb
67
- - lib/universum/safe_array.rb
68
- - lib/universum/safe_hash.rb
69
82
  - lib/universum/transaction.rb
70
83
  - lib/universum/type.rb
71
84
  - lib/universum/units_money.rb
@@ -75,12 +88,9 @@ files:
75
88
  - test/contracts/greeter.rb
76
89
  - test/contracts/mytoken.rb
77
90
  - test/helper.rb
78
- - test/test_array.rb
79
91
  - test/test_enum.rb
80
92
  - test/test_event.rb
81
93
  - test/test_greeter.rb
82
- - test/test_hash.rb
83
- - test/test_struct.rb
84
94
  - test/test_units_money.rb
85
95
  - test/test_units_time.rb
86
96
  - test/test_version.rb
@@ -1,69 +0,0 @@
1
- # encoding: utf-8
2
-
3
- class SafeArray
4
-
5
- ## e.g.
6
- ## Array.of( Address ), Array.of( Integer), etc.
7
-
8
- def self.build_class( klass_value )
9
- ## note: care for now only about value type / class
10
-
11
- ## note: keep a class cache
12
- cache = @@cache ||= {}
13
- klass = cache[ klass_value ]
14
- return klass if klass
15
-
16
- klass = Class.new( SafeArray )
17
- klass.class_eval( <<RUBY )
18
- def self.klass_value
19
- @klass_value ||= #{klass_value}
20
- end
21
- RUBY
22
- ## add to cache for later (re)use
23
- cache[ klass_value ] = klass
24
- klass
25
- end
26
-
27
-
28
- def self.new_zero() new; end
29
- def self.zero() @zero ||= new_zero; end
30
-
31
-
32
-
33
- def initialize
34
- ## todo/check: if array works if value is a (nested/multi-dimensional) array
35
- @ary = []
36
- end
37
-
38
- def []=(index, value)
39
- @ary[index] = value
40
- end
41
-
42
- def [](index)
43
- item = @ary[ index ]
44
- if item.nil?
45
- ## todo/check:
46
- ## always return (deep) frozen zero object - why? why not?
47
- ## let user change the returned zero object - why? why not?
48
- if self.class.klass_value.respond_to?( :new_zero )
49
- ## note: use a new unfrozen copy of the zero object
50
- ## changes to the object MUST be possible (new "empty" modifable object expected)
51
- item = self.class.klass_value.new_zero
52
- else # assume value semantics e.g. Integer, Bool, etc. zero values gets replaced
53
- ## puts "use value semantics"
54
- item = self.class.klass_value.zero
55
- end
56
- end
57
- item
58
- end
59
-
60
- def push( item )
61
- ## todo/fix: check if item.is_a? @type
62
- ## note: Address might be a String too (Address | String)
63
- ## store Address always as String!!! - why? why not?
64
- @ary.push( item )
65
- end
66
-
67
- def size() @ary.size; end
68
- def length() size; end
69
- end # class SafeArray
@@ -1,77 +0,0 @@
1
- # encoding: utf-8
2
-
3
-
4
- class SafeHash
5
-
6
- ## e.g.
7
- ## Mapping.of( Address => Money )
8
-
9
- ## note: need to create new class!! for every mapping
10
- ## make klass_key class and
11
- ## klass_value class into class instance variables
12
- ## that can get used by zero
13
- ## self.new returns a Hash.new/SafeHash.new like object
14
-
15
- def self.build_class( klass_key, klass_value )
16
- ## note: care for now only about value type / class
17
-
18
- ## note: keep a class cache
19
- cache = @@cache ||= {}
20
- klass = cache[ klass_value ]
21
- return klass if klass
22
-
23
- klass = Class.new( SafeHash )
24
- klass.class_eval( <<RUBY )
25
- def self.klass_key
26
- @klass_key ||= #{klass_key}
27
- end
28
- def self.klass_value
29
- @klass_value ||= #{klass_value}
30
- end
31
- RUBY
32
- ## add to cache for later (re)use
33
- cache[ klass_value ] = klass
34
- klass
35
- end
36
-
37
-
38
- def self.new_zero() new; end
39
- def self.zero() @zero ||= new_zero; end
40
-
41
-
42
- def initialize
43
- ## todo/check: if hash works if value is a (nested) hash
44
- @h = {}
45
- end
46
-
47
-
48
- def []=(key, value)
49
- @h[key] = value
50
- end
51
-
52
- def [](key)
53
- item = @h[ key ]
54
- if item.nil?
55
- ## pp self.class.klass_value
56
- ## pp self.class.klass_value.zero
57
-
58
- #####
59
- # todo/check:
60
- # add zero to hash on lookup (increases size/length)
61
- # why? why not?
62
-
63
- if self.class.klass_value.respond_to?( :new_zero )
64
- ## note: use a dup(licated) unfrozen copy of the zero object
65
- ## changes to the object MUST be possible (new "empty" modifable object expected)
66
- item = @h[ key ] = self.class.klass_value.new_zero
67
- else # assume value semantics e.g. Integer, Bool, etc. zero values gets replaced
68
- ## puts "use value semantics"
69
- item = @h[ key ] = self.class.klass_value.zero
70
- end
71
- end
72
- item
73
- end
74
-
75
- def size() @h.size; end
76
- def length() size; end
77
- end # class SafeHash
@@ -1,52 +0,0 @@
1
- # encoding: utf-8
2
-
3
- ###
4
- # to run use
5
- # ruby -I ./lib -I ./test test/test_array.rb
6
-
7
-
8
- require 'helper'
9
-
10
-
11
- class TestArray < MiniTest::Test
12
-
13
- Array_Integer = SafeArray.build_class( Integer )
14
- Array_Bool = SafeArray.build_class( Bool )
15
-
16
- def test_integer
17
- pp Array_Integer
18
- pp ary = Array_Integer.new
19
-
20
- assert_equal Integer, Array_Integer.klass_value
21
- assert_equal 0, ary[0]
22
- assert_equal 0, ary[1]
23
-
24
- ary[0] = 101
25
- ary[1] = 102
26
- assert_equal 101, ary[0]
27
- assert_equal 102, ary[1]
28
-
29
- ## check Array.of (uses cached classes)
30
- assert_equal Array_Integer, Array.of( Integer ).class
31
- end
32
-
33
-
34
- def test_bool
35
- pp Array_Bool
36
- pp ary = Array_Bool.new
37
-
38
- assert_equal Bool, Array_Bool.klass_value
39
- assert_equal false, ary[0]
40
- assert_equal false, ary[1]
41
-
42
- ary[0] = true
43
- ary[1] = true
44
- assert_equal true, ary[0]
45
- assert_equal true, ary[1]
46
-
47
- ## check Array.of (uses cached classes)
48
- assert_equal Array_Bool, Array.of( Bool ).class
49
- end
50
-
51
-
52
- end # class TestArray
@@ -1,84 +0,0 @@
1
- # encoding: utf-8
2
-
3
- ###
4
- # to run use
5
- # ruby -I ./lib -I ./test test/test_hash.rb
6
-
7
-
8
- require 'helper'
9
-
10
-
11
- class TestHash < MiniTest::Test
12
-
13
- ## sig: [Integer, Bool, Integer, Address]
14
- Voter = Struct.new( :weight, :voted, :vote, :delegate ) do
15
- def self.new_zero
16
- new( 0, false, 0, '0x0000' )
17
- end
18
- def self.zero
19
- @zero ||= new_zero
20
- end
21
- end
22
-
23
- Hash_X_Integer = SafeHash.build_class( String, Integer )
24
- Hash_X_Bool = SafeHash.build_class( String, Bool )
25
- Hash_X_Voter = SafeHash.build_class( String, Voter )
26
-
27
- def test_integer
28
- pp Hash_X_Integer
29
- pp h = Hash_X_Integer.new
30
-
31
- assert_equal Integer, Hash_X_Integer.klass_value
32
- assert_equal 0, h['0x1111']
33
- assert_equal 0, h['0x2222']
34
-
35
- h['0x1111'] = 101
36
- h['0x2222'] = 102
37
- assert_equal 101, h['0x1111']
38
- assert_equal 102, h['0x2222']
39
-
40
- ## check Mapping.of (uses cached classes)
41
- assert_equal Hash_X_Integer, Mapping.of( String => Integer ).class
42
- end
43
-
44
-
45
- def test_bool
46
- pp Hash_X_Bool
47
- pp h = Hash_X_Bool.new
48
-
49
- assert_equal Bool, Hash_X_Bool.klass_value
50
- assert_equal false, h['0x1111']
51
- assert_equal false, h['0x2222']
52
-
53
- h['0x1111'] = true
54
- h['0x2222'] = true
55
- assert_equal true, h['0x1111']
56
- assert_equal true, h['0x2222']
57
-
58
- ## check Mapping.of (uses cached classes)
59
- assert_equal Hash_X_Bool, Mapping.of( String => Bool ).class
60
- end
61
-
62
-
63
- def test_voter
64
- pp Hash_X_Voter
65
- pp h = Hash_X_Voter.new
66
-
67
- assert_equal Voter, Hash_X_Voter.klass_value
68
- assert_equal Voter.zero, h['0x1111']
69
- assert_equal Voter.zero, h['0x2222']
70
-
71
- h['0x1111'].voted = true
72
- h['0x2222'].voted = true
73
- assert_equal true, h['0x1111'].voted
74
- assert_equal true, h['0x2222'].voted
75
-
76
- pp h['0x1111']
77
- pp h['0x2222']
78
-
79
- ## check Mapping.of (uses cached classes)
80
- assert_equal Hash_X_Voter, Mapping.of( String => Voter ).class
81
- end
82
-
83
-
84
- end # class TestHash
@@ -1,35 +0,0 @@
1
- # encoding: utf-8
2
-
3
- ###
4
- # to run use
5
- # ruby -I ./lib -I ./test test/test_struct.rb
6
-
7
-
8
- require 'helper'
9
-
10
-
11
- class TestStruct < MiniTest::Test
12
-
13
- ## sig: [Integer, Bool, Integer, Address]
14
- Voter = Struct.new( :weight, :voted, :vote, :delegate ) do
15
- def self.new_zero
16
- new( 0, false, 0, '0x0000' )
17
- end
18
- def self.zero
19
- @zero ||= new_zero
20
- end
21
- end
22
-
23
-
24
- def test_zero
25
- assert_equal Voter.zero, Voter.zero
26
-
27
- zerodup = Voter.zero.dup
28
- zerodup.delegate = '0x1111'
29
- pp zerodup
30
-
31
- assert_equal '0x0000', Voter.zero.delegate
32
- end
33
-
34
-
35
- end # class TestStruct