scale.rb 0.2.18 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  module Scale
2
2
  module Types
3
3
  class MetadataV5
4
- include SingleValue
4
+ include Base
5
5
  attr_accessor :call_index, :event_index
6
6
 
7
7
  def initialize(value)
@@ -11,7 +11,7 @@ module Scale
11
11
  end
12
12
 
13
13
  def self.decode(scale_bytes)
14
- modules = Scale::Types.type_of("Vec<MetadataV5Module>").decode(scale_bytes).value
14
+ modules = Scale::Types.get("Vec<MetadataV5Module>").decode(scale_bytes).value
15
15
 
16
16
  value = {
17
17
  magicNumber: 1_635_018_093,
@@ -49,7 +49,7 @@ module Scale
49
49
  end
50
50
 
51
51
  class MetadataV5Module
52
- include SingleValue
52
+ include Base
53
53
  def self.decode(scale_bytes)
54
54
  name = String.decode(scale_bytes).value
55
55
  prefix = String.decode(scale_bytes).value
@@ -61,19 +61,19 @@ module Scale
61
61
 
62
62
  has_storage = Bool.decode(scale_bytes).value
63
63
  if has_storage
64
- storages = Scale::Types.type_of("Vec<MetadataV5ModuleStorage>").decode(scale_bytes).value
64
+ storages = Scale::Types.get("Vec<MetadataV5ModuleStorage>").decode(scale_bytes).value
65
65
  result[:storage] = storages.map(&:value)
66
66
  end
67
67
 
68
68
  has_calls = Bool.decode(scale_bytes).value
69
69
  if has_calls
70
- calls = Scale::Types.type_of("Vec<MetadataModuleCall>").decode(scale_bytes).value
70
+ calls = Scale::Types.get("Vec<MetadataModuleCall>").decode(scale_bytes).value
71
71
  result[:calls] = calls.map(&:value)
72
72
  end
73
73
 
74
74
  has_events = Bool.decode(scale_bytes).value
75
75
  if has_events
76
- events = Scale::Types.type_of("Vec<MetadataModuleEvent>").decode(scale_bytes).value
76
+ events = Scale::Types.get("Vec<MetadataModuleEvent>").decode(scale_bytes).value
77
77
  result[:events] = events.map(&:value)
78
78
  end
79
79
 
@@ -82,14 +82,24 @@ module Scale
82
82
  end
83
83
 
84
84
  class MetadataV5ModuleStorage
85
- include SingleValue
85
+ include Base
86
86
  def self.decode(scale_bytes)
87
+ name = String.decode(scale_bytes).value
88
+ modifier_enum = {
89
+ "type" => "enum",
90
+ "value_list" => ["Optional", "Default"]
91
+ }
92
+ modifier = Scale::Types.get(modifier_enum).decode(scale_bytes).value
87
93
  result = {
88
- name: String.decode(scale_bytes).value,
89
- modifier: Scale::Types.type_of("Enum", %w[Optional Default]).decode(scale_bytes).value
94
+ name: name,
95
+ modifier: modifier
90
96
  }
91
97
 
92
- storage_function_type = Scale::Types.type_of("Enum", %w[Plain Map DoubleMap]).decode(scale_bytes).value
98
+ storage_function_type_enum = {
99
+ "type" => "enum",
100
+ "value_list" => %w[Plain Map DoubleMap]
101
+ }
102
+ storage_function_type = Scale::Types.get(storage_function_type_enum).decode(scale_bytes).value
93
103
  if storage_function_type == "Plain"
94
104
  result[:type] = {
95
105
  Plain: String.decode(scale_bytes).value
@@ -116,7 +126,7 @@ module Scale
116
126
  end
117
127
 
118
128
  result[:fallback] = Hex.decode(scale_bytes).value
119
- result[:documentation] = Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
129
+ result[:documentation] = Scale::Types.get("Vec<String>").decode(scale_bytes).value.map(&:value)
120
130
 
121
131
  MetadataV5ModuleStorage.new(result)
122
132
  end
@@ -1,7 +1,7 @@
1
1
  module Scale
2
2
  module Types
3
3
  class MetadataV6
4
- include SingleValue
4
+ include Base
5
5
  attr_accessor :call_index, :event_index
6
6
 
7
7
  def initialize(value)
@@ -11,7 +11,7 @@ module Scale
11
11
  end
12
12
 
13
13
  def self.decode(scale_bytes)
14
- modules = Scale::Types.type_of("Vec<MetadataV6Module>").decode(scale_bytes).value
14
+ modules = Scale::Types.get("Vec<MetadataV6Module>").decode(scale_bytes).value
15
15
 
16
16
  value = {
17
17
  magicNumber: 1_635_018_093,
@@ -49,7 +49,7 @@ module Scale
49
49
  end
50
50
 
51
51
  class MetadataV6Module
52
- include SingleValue
52
+ include Base
53
53
  def self.decode(scale_bytes)
54
54
  name = String.decode(scale_bytes).value
55
55
  prefix = String.decode(scale_bytes).value
@@ -61,23 +61,23 @@ module Scale
61
61
 
62
62
  has_storage = Bool.decode(scale_bytes).value
63
63
  if has_storage
64
- storages = Scale::Types.type_of("Vec<MetadataV6ModuleStorage>").decode(scale_bytes).value
64
+ storages = Scale::Types.get("Vec<MetadataV6ModuleStorage>").decode(scale_bytes).value
65
65
  result[:storage] = storages.map(&:value)
66
66
  end
67
67
 
68
68
  has_calls = Bool.decode(scale_bytes).value
69
69
  if has_calls
70
- calls = Scale::Types.type_of("Vec<MetadataModuleCall>").decode(scale_bytes).value
70
+ calls = Scale::Types.get("Vec<MetadataModuleCall>").decode(scale_bytes).value
71
71
  result[:calls] = calls.map(&:value)
72
72
  end
73
73
 
74
74
  has_events = Bool.decode(scale_bytes).value
75
75
  if has_events
76
- events = Scale::Types.type_of("Vec<MetadataModuleEvent>").decode(scale_bytes).value
76
+ events = Scale::Types.get("Vec<MetadataModuleEvent>").decode(scale_bytes).value
77
77
  result[:events] = events.map(&:value)
78
78
  end
79
79
 
80
- result[:constants] = Scale::Types.type_of("Vec<MetadataV6ModuleConstants>").decode(scale_bytes).value.map(&:value)
80
+ result[:constants] = Scale::Types.get("Vec<MetadataV6ModuleConstants>").decode(scale_bytes).value.map(&:value)
81
81
 
82
82
  MetadataV6Module.new(result)
83
83
  end
@@ -86,13 +86,13 @@ module Scale
86
86
  class MetadataV6ModuleStorage < MetadataV5ModuleStorage; end
87
87
 
88
88
  class MetadataV6ModuleConstants
89
- include SingleValue
89
+ include Base
90
90
  def self.decode(scale_bytes)
91
91
  result = {
92
92
  name: String.decode(scale_bytes).value,
93
93
  type: String.decode(scale_bytes).value, # convert
94
94
  value: Hex.decode(scale_bytes).value,
95
- documentation: Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
95
+ documentation: Scale::Types.get("Vec<String>").decode(scale_bytes).value.map(&:value)
96
96
  }
97
97
  MetadataV6ModuleConstants.new result
98
98
  end
@@ -1,7 +1,7 @@
1
1
  module Scale
2
2
  module Types
3
3
  class MetadataV7
4
- include SingleValue
4
+ include Base
5
5
  attr_accessor :call_index, :event_index
6
6
 
7
7
  def initialize(value)
@@ -11,7 +11,7 @@ module Scale
11
11
  end
12
12
 
13
13
  def self.decode(scale_bytes)
14
- modules = Scale::Types.type_of("Vec<MetadataV7Module>").decode(scale_bytes).value
14
+ modules = Scale::Types.get("Vec<MetadataV7Module>").decode(scale_bytes).value
15
15
 
16
16
  value = {
17
17
  magicNumber: 1_635_018_093,
@@ -49,7 +49,7 @@ module Scale
49
49
  end
50
50
 
51
51
  class MetadataV7Module
52
- include SingleValue
52
+ include Base
53
53
  def self.decode(scale_bytes)
54
54
  name = String.decode(scale_bytes).value
55
55
 
@@ -66,27 +66,27 @@ module Scale
66
66
 
67
67
  has_calls = Bool.decode(scale_bytes).value
68
68
  if has_calls
69
- calls = Scale::Types.type_of("Vec<MetadataModuleCall>").decode(scale_bytes).value
69
+ calls = Scale::Types.get("Vec<MetadataModuleCall>").decode(scale_bytes).value
70
70
  result[:calls] = calls.map(&:value)
71
71
  end
72
72
 
73
73
  has_events = Bool.decode(scale_bytes).value
74
74
  if has_events
75
- events = Scale::Types.type_of("Vec<MetadataModuleEvent>").decode(scale_bytes).value
75
+ events = Scale::Types.get("Vec<MetadataModuleEvent>").decode(scale_bytes).value
76
76
  result[:events] = events.map(&:value)
77
77
  end
78
78
 
79
- result[:constants] = Scale::Types.type_of("Vec<MetadataV7ModuleConstants>").decode(scale_bytes).value.map(&:value)
79
+ result[:constants] = Scale::Types.get("Vec<MetadataV7ModuleConstants>").decode(scale_bytes).value.map(&:value)
80
80
 
81
81
  MetadataV7Module.new(result)
82
82
  end
83
83
  end
84
84
 
85
85
  class MetadataV7ModuleStorage
86
- include SingleValue
86
+ include Base
87
87
  def self.decode(scale_bytes)
88
88
  prefix = String.decode(scale_bytes).value
89
- items = Scale::Types.type_of("Vec<MetadataV7ModuleStorageEntry>").decode(scale_bytes).value.map(&:value)
89
+ items = Scale::Types.get("Vec<MetadataV7ModuleStorageEntry>").decode(scale_bytes).value.map(&:value)
90
90
  result = {
91
91
  prefix: prefix,
92
92
  items: items
@@ -97,13 +97,24 @@ module Scale
97
97
  end
98
98
 
99
99
  class MetadataV7ModuleStorageEntry
100
- include SingleValue
100
+ include Base
101
101
  def self.decode(scale_bytes)
102
+ name = String.decode(scale_bytes).value
103
+ modifier_enum = {
104
+ "type" => "enum",
105
+ "value_list" => ["Optional", "Default"]
106
+ }
107
+ modifier = Scale::Types.get(modifier_enum).decode(scale_bytes).value
102
108
  result = {
103
- name: String.decode(scale_bytes).value,
104
- modifier: Scale::Types.type_of("Enum", %w[Optional Default]).decode(scale_bytes).value
109
+ name: name,
110
+ modifier: modifier
111
+ }
112
+
113
+ storage_function_type_enum = {
114
+ "type" => "enum",
115
+ "value_list" => %w[Plain Map DoubleMap]
105
116
  }
106
- storage_function_type = Scale::Types.type_of("Enum", %w[Plain Map DoubleMap]).decode(scale_bytes).value
117
+ storage_function_type = Scale::Types.get(storage_function_type_enum).decode(scale_bytes).value
107
118
  if storage_function_type == "Plain"
108
119
  result[:type] = {
109
120
  Plain: String.decode(scale_bytes).value
@@ -130,20 +141,20 @@ module Scale
130
141
  end
131
142
 
132
143
  result[:fallback] = Hex.decode(scale_bytes).value
133
- result[:documentation] = Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
144
+ result[:documentation] = Scale::Types.get("Vec<String>").decode(scale_bytes).value.map(&:value)
134
145
 
135
146
  MetadataV7ModuleStorageEntry.new(result)
136
147
  end
137
148
  end
138
149
 
139
150
  class MetadataV7ModuleConstants
140
- include SingleValue
151
+ include Base
141
152
  def self.decode(scale_bytes)
142
153
  result = {
143
154
  name: String.decode(scale_bytes).value,
144
155
  type: String.decode(scale_bytes).value, # convert
145
156
  value: Hex.decode(scale_bytes).value,
146
- documentation: Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
157
+ documentation: Scale::Types.get("Vec<String>").decode(scale_bytes).value.map(&:value)
147
158
  }
148
159
  MetadataV7ModuleConstants.new result
149
160
  end
@@ -1,7 +1,7 @@
1
1
  module Scale
2
2
  module Types
3
3
  class MetadataV8
4
- include SingleValue
4
+ include Base
5
5
  attr_accessor :call_index, :event_index
6
6
 
7
7
  def initialize(value)
@@ -11,7 +11,7 @@ module Scale
11
11
  end
12
12
 
13
13
  def self.decode(scale_bytes)
14
- modules = Scale::Types.type_of("Vec<MetadataV8Module>").decode(scale_bytes).value
14
+ modules = Scale::Types.get("Vec<MetadataV8Module>").decode(scale_bytes).value
15
15
 
16
16
  value = {
17
17
  magicNumber: 1_635_018_093,
@@ -49,7 +49,7 @@ module Scale
49
49
  end
50
50
 
51
51
  class MetadataV8Module
52
- include SingleValue
52
+ include Base
53
53
  def self.decode(scale_bytes)
54
54
  name = String.decode(scale_bytes).value
55
55
 
@@ -66,29 +66,29 @@ module Scale
66
66
 
67
67
  has_calls = Bool.decode(scale_bytes).value
68
68
  if has_calls
69
- calls = Scale::Types.type_of("Vec<MetadataModuleCall>").decode(scale_bytes).value
69
+ calls = Scale::Types.get("Vec<MetadataModuleCall>").decode(scale_bytes).value
70
70
  result[:calls] = calls.map(&:value)
71
71
  end
72
72
 
73
73
  has_events = Bool.decode(scale_bytes).value
74
74
  if has_events
75
- events = Scale::Types.type_of("Vec<MetadataModuleEvent>").decode(scale_bytes).value
75
+ events = Scale::Types.get("Vec<MetadataModuleEvent>").decode(scale_bytes).value
76
76
  result[:events] = events.map(&:value)
77
77
  end
78
78
 
79
- result[:constants] = Scale::Types.type_of("Vec<MetadataV7ModuleConstants>").decode(scale_bytes).value.map(&:value)
80
- result[:errors] = Scale::Types.type_of("Vec<MetadataModuleError>").decode(scale_bytes).value.map(&:value)
79
+ result[:constants] = Scale::Types.get("Vec<MetadataV7ModuleConstants>").decode(scale_bytes).value.map(&:value)
80
+ result[:errors] = Scale::Types.get("Vec<MetadataModuleError>").decode(scale_bytes).value.map(&:value)
81
81
 
82
82
  MetadataV8Module.new(result)
83
83
  end
84
84
  end
85
85
 
86
86
  class MetadataModuleError
87
- include SingleValue
87
+ include Base
88
88
  def self.decode(scale_bytes)
89
89
  result = {
90
90
  name: String.decode(scale_bytes).value,
91
- documentation: Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
91
+ documentation: Scale::Types.get("Vec<String>").decode(scale_bytes).value.map(&:value)
92
92
  }
93
93
 
94
94
  MetadataModuleError.new(result)
@@ -1,7 +1,7 @@
1
1
  module Scale
2
2
  module Types
3
3
  class MetadataV9
4
- include SingleValue
4
+ include Base
5
5
  attr_accessor :call_index, :event_index
6
6
 
7
7
  def initialize(value)
@@ -11,7 +11,7 @@ module Scale
11
11
  end
12
12
 
13
13
  def self.decode(scale_bytes)
14
- modules = Scale::Types.type_of("Vec<MetadataV8Module>").decode(scale_bytes).value
14
+ modules = Scale::Types.get("Vec<MetadataV8Module>").decode(scale_bytes).value
15
15
 
16
16
  value = {
17
17
  magicNumber: 1_635_018_093,
data/lib/scale/base.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Scale
2
2
  module Types
3
3
 
4
- module SingleValue
4
+ module Base
5
5
  attr_reader :value
6
6
 
7
7
  def initialize(value)
@@ -9,26 +9,28 @@ module Scale
9
9
  end
10
10
 
11
11
  def ==(other)
12
- value == other.value
12
+ self.class == other.class && self.value == other.value
13
13
  end
14
14
 
15
15
  def to_human
16
16
  if @value.class == ::Hash
17
17
  @value.transform_values do |v|
18
- if v.class.included_modules.include?(SingleValue)
18
+ if v.class.included_modules.include?(Base)
19
19
  v.to_human
20
20
  else
21
21
  v
22
22
  end
23
- end
23
+ end.transform_keys(&:to_sym)
24
24
  elsif @value.class == ::Array
25
25
  @value.map do |v|
26
- if v.class.included_modules.include?(SingleValue)
26
+ if v.class.included_modules.include?(Base)
27
27
  v.to_human
28
28
  else
29
29
  v
30
30
  end
31
31
  end
32
+ elsif @value.class.include?(Base)
33
+ @value.to_human
32
34
  else
33
35
  @value
34
36
  end
@@ -46,9 +48,9 @@ module Scale
46
48
  end
47
49
  end
48
50
 
49
- # value: one of nil, false, true, scale object
51
+ # if value is bool, see type `OptionBool` in types.rb
50
52
  module Option
51
- include SingleValue
53
+ include Base
52
54
 
53
55
  module ClassMethods
54
56
  def decode(scale_bytes)
@@ -58,29 +60,23 @@ module Scale
58
60
  puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
59
61
  new(nil)
60
62
  elsif byte == [1]
61
- if self::INNER_TYPE_STR == "boolean"
62
- puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
63
- new(false)
64
- else
65
- # big process
66
- value = Scale::Types.get(self::INNER_TYPE_STR).decode(scale_bytes)
67
- puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
68
- new(value)
69
- end
70
- elsif byte == [2]
71
- if self::INNER_TYPE_STR == "boolean"
72
- puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
73
- new(true)
74
- else
75
- raise "bad data"
76
- end
63
+ # big process
64
+ type =
65
+ if self::INNER_TYPE.class == ::String
66
+ Scale::Types.get(self::INNER_TYPE)
67
+ else
68
+ self::INNER_TYPE
69
+ end
70
+ value = type.decode(scale_bytes)
71
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
72
+ new(value)
77
73
  else
78
- raise "bad data"
74
+ raise BadDataError.new("Bad scale data for #{self::TYPE_NAME}")
79
75
  end
80
76
  end
81
77
 
82
- def inner_type(type_str)
83
- const_set(:INNER_TYPE_STR, type_str)
78
+ def inner_type(type)
79
+ const_set(:INNER_TYPE, type)
84
80
  end
85
81
  end
86
82
 
@@ -93,15 +89,13 @@ module Scale
93
89
  if value.nil?
94
90
  "00"
95
91
  else
96
- return "02" if value.class == TrueClass && value === true
97
- return "01" if value.class == FalseClass && value === false
98
92
  "01" + value.encode
99
93
  end
100
94
  end
101
95
  end
102
96
 
103
97
  module FixedWidthInt
104
- include SingleValue
98
+ include Base
105
99
 
106
100
  module ClassMethods
107
101
  def decode(scale_bytes)
@@ -129,7 +123,7 @@ module Scale
129
123
  end
130
124
 
131
125
  module FixedWidthUInt
132
- include SingleValue
126
+ include Base
133
127
 
134
128
  module ClassMethods
135
129
  attr_accessor :byte_length
@@ -162,7 +156,7 @@ module Scale
162
156
  end
163
157
 
164
158
  module Struct
165
- include SingleValue
159
+ include Base
166
160
  # new(1.to_u32, U32(69))
167
161
  module ClassMethods
168
162
  def inherited(child)
@@ -171,36 +165,40 @@ module Scale
171
165
 
172
166
  def decode(scale_bytes)
173
167
  puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
174
- item_values = self::ITEM_TYPE_STRS.map do |item_type_str|
175
- type = Scale::Types.get(item_type_str)
176
- type.decode(scale_bytes)
177
- end
178
168
 
179
- value = {}
180
- self::ITEM_NAMES.zip(item_values) do |attr, val|
181
- value[attr] = val
169
+ # item_values:
170
+ # {
171
+ # a: ...,
172
+ # b: ...
173
+ # }
174
+ item_values = {}
175
+ self::ITEMS.each_pair do |item_name, item_type|
176
+ if item_type.class == ::String
177
+ item_type = Scale::Types.get(item_type)
178
+ end
179
+ item_values[item_name] = item_type.decode(scale_bytes)
182
180
  end
183
181
 
184
- result = new(value)
185
- value.each_pair do |attr, val|
186
- result.send "#{attr}=", val
187
- end
188
- puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
189
- result
190
- end
182
+ # value = {}
183
+ # self::ITEM_NAMES.zip(item_values) do |attr, val|
184
+ # value[attr] = val
185
+ # end
191
186
 
192
- def items(items)
193
- attr_names = []
194
- attr_type_strs = []
187
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
195
188
 
196
- items.each_pair do |attr_name, attr_type_str|
197
- attr_names << attr_name.to_s.gsub("-", "")
198
- attr_type_strs << attr_type_str
189
+ result = new(item_values)
190
+ item_values.each_pair do |item_name, item_value|
191
+ result.send "#{item_name.to_s}=", item_value
199
192
  end
200
193
 
201
- const_set(:ITEM_NAMES, attr_names)
202
- const_set(:ITEM_TYPE_STRS, attr_type_strs)
203
- attr_accessor *attr_names
194
+ result
195
+ end
196
+
197
+ # items(a: Scale::Types::Type1, b: "Type2")
198
+ def items(**items)
199
+ const_set(:ITEMS, items)
200
+ item_names = items.keys
201
+ attr_accessor *item_names
204
202
  end
205
203
  end
206
204
 
@@ -210,29 +208,33 @@ module Scale
210
208
  end
211
209
 
212
210
  def encode
213
- [].tap do |result|
214
- value.each_pair do |attr_name, attr_value|
215
- result << attr_value.encode
216
- end
211
+ value.values.map do |item_value|
212
+ item_value.encode
217
213
  end.join
218
214
  end
219
215
  end
220
216
 
221
217
  module Tuple
222
- include SingleValue
218
+ include Base
223
219
 
224
220
  module ClassMethods
225
221
  def decode(scale_bytes)
226
222
  puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
227
- values = self::TYPE_STRS.map do |type_str|
228
- Scale::Types.get(type_str).decode(scale_bytes)
223
+
224
+ values = self::INNER_TYPES.map do |type|
225
+ if type.class == ::String
226
+ type = Scale::Types.get(type)
227
+ end
228
+ type.decode(scale_bytes)
229
229
  end
230
+
230
231
  puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
231
232
  new(values)
232
233
  end
233
234
 
234
- def inner_types(*type_strs)
235
- const_set(:TYPE_STRS, type_strs)
235
+ # inner_types Scale::Types::U8, "U8"
236
+ def inner_types(*inner_types)
237
+ const_set(:INNER_TYPES, inner_types)
236
238
  end
237
239
  end
238
240
 
@@ -246,41 +248,46 @@ module Scale
246
248
  end
247
249
 
248
250
  module Enum
249
- include SingleValue
251
+ include Base
252
+
253
+ attr_accessor :index
250
254
 
251
255
  module ClassMethods
252
256
  def decode(scale_bytes)
253
257
  puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
254
258
  index = scale_bytes.get_next_bytes(1)[0]
255
- if const_defined? "ITEM_TYPE_STRS"
256
- item_type_str = self::ITEM_TYPE_STRS[index]
257
- raise "There is no such member with index #{index} for enum #{self}" if item_type_str.nil?
258
- value = Scale::Types.get(item_type_str).decode(scale_bytes)
259
- else
259
+ if const_defined? "ITEMS"
260
+ type = self::ITEMS.values[index]
261
+ if type.class == ::String
262
+ type = Scale::Types.get(type)
263
+ end
264
+ value = type.decode(scale_bytes)
265
+ elsif const_defined? "INNER_TYPES"
266
+ type = self::INNER_TYPES[index]
267
+ if type.class == ::String
268
+ type = Scale::Types.get(type)
269
+ end
270
+ value = type.decode(scale_bytes)
271
+ else # VALUES
260
272
  value = self::VALUES[index]
261
273
  end
262
- result = new(value)
263
274
  puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
275
+ result = new(value)
276
+ result.index = index
264
277
  result
265
278
  end
266
279
 
267
- def items(items)
268
- if items.class == ::Hash
269
- attr_names = []
270
- attr_type_strs = []
271
-
272
- items.each_pair do |attr_name, attr_type_str|
273
- attr_names << attr_name
274
- attr_type_strs << attr_type_str
275
- end
280
+ # inner_types(Scale::Types::Compact, "Hex")
281
+ def inner_types(*inner_types)
282
+ const_set(:INNER_TYPES, inner_types)
283
+ end
276
284
 
277
- const_set(:ITEM_NAMES, attr_names)
278
- const_set(:ITEM_TYPE_STRS, attr_type_strs)
279
- elsif items.class == ::Array
280
- const_set(:ITEM_TYPE_STRS, items)
281
- end
285
+ # items(Item1: Scale::Types::Compact, Item2: "Hex")
286
+ def items(**items)
287
+ const_set(:ITEMS, items)
282
288
  end
283
289
 
290
+ # [1, "hello"]
284
291
  def values(*values)
285
292
  const_set(:VALUES, values)
286
293
  end
@@ -291,17 +298,17 @@ module Scale
291
298
  end
292
299
 
293
300
  def encode
294
- if self.class.const_defined? "ITEM_NAMES"
295
- value_type_str = value.class.to_s.split("::").last.to_s
296
- index = self.class::ITEM_TYPE_STRS.index(value_type_str).to_s(16).rjust(2, "0")
297
- index + value.encode
301
+ if self.class.const_defined? "ITEMS"
302
+ index.to_s(16).rjust(2, "0") + value.encode
298
303
  else
299
304
  self.class::VALUES.index(value).to_s(16).rjust(2, "0")
300
305
  end
301
306
  end
302
307
 
303
308
  def to_human
304
- if self.class.const_defined? "ITEM_TYPE_STRS"
309
+ if self.class.const_defined? "ITEMS"
310
+ @value.to_human
311
+ elsif self.class.const_defined? "INNER_TYPES"
305
312
  @value.to_human
306
313
  else
307
314
  @value
@@ -310,7 +317,7 @@ module Scale
310
317
  end
311
318
 
312
319
  module Vec
313
- include SingleValue # value is an array
320
+ include Base # value is an array
314
321
 
315
322
  module ClassMethods
316
323
  def decode(scale_bytes, raw = false)
@@ -318,16 +325,15 @@ module Scale
318
325
  number = Scale::Types::Compact.decode(scale_bytes).value
319
326
  items = []
320
327
  number.times do
321
- type = Scale::Types.get(self::INNER_TYPE_STR)
322
- item = type.decode(scale_bytes)
328
+ item = self::INNER_TYPE.decode(scale_bytes)
323
329
  items << item
324
330
  end
325
331
  puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
326
332
  raw ? items : new(items)
327
333
  end
328
334
 
329
- def inner_type(type_str)
330
- const_set(:INNER_TYPE_STR, type_str)
335
+ def inner_type(type)
336
+ const_set(:INNER_TYPE, type)
331
337
  end
332
338
  end
333
339
 
@@ -346,7 +352,7 @@ module Scale
346
352
  end
347
353
 
348
354
  module Set
349
- include SingleValue
355
+ include Base
350
356
 
351
357
  module ClassMethods
352
358
  def decode(scale_bytes)
@@ -384,7 +390,7 @@ module Scale
384
390
  end
385
391
 
386
392
  module VecU8FixedLength
387
- include SingleValue
393
+ include Base
388
394
 
389
395
  module ClassMethods
390
396
  def decode(scale_bytes)
@@ -421,5 +427,39 @@ module Scale
421
427
  end
422
428
  end
423
429
 
430
+ module Array
431
+ include Base
432
+
433
+ module ClassMethods
434
+ def decode(scale_bytes)
435
+ puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
436
+ items = (0 ... self::LENGTH).map do |_|
437
+ self::INNER_TYPE.decode(scale_bytes)
438
+ end
439
+ puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true
440
+ new(items)
441
+ end
442
+
443
+ def inner_type(type)
444
+ const_set(:INNER_TYPE, type)
445
+ end
446
+
447
+ def length(len)
448
+ const_set(:LENGTH, len)
449
+ end
450
+ end
451
+
452
+ def self.included(base)
453
+ base.extend ClassMethods
454
+ end
455
+
456
+ def encode
457
+ self.value.map do |item|
458
+ item.encode
459
+ end.join
460
+ end
461
+ end
462
+
463
+
424
464
  end
425
465
  end