class Ciri::RLP::Serializable::Schema
Schema
specific columns types of classes, normally you should not use Serializable::Schema
directly
Constants
- KeySchema
Attributes
keys[R]
keys return data columns array
Public Class Methods
new(schema)
click to toggle source
# File lib/ciri/rlp/serializable.rb, line 102 def initialize(schema) keys = [] @_schema = {} schema.each do |key, type| raise InvalidSchemaError.new("incorrect type on key #{key}, #{type} is not a valid RLP class") unless check_key_type(type) keys << key @_schema[key] = KeySchema.new(type: type) end @_schema.freeze @keys = keys.freeze end
Public Instance Methods
[](key)
click to toggle source
Get column type, see Serializable::TYPES for supported type
# File lib/ciri/rlp/serializable.rb, line 117 def [](key) @_schema[key] end
rlp_decode(input)
click to toggle source
# File lib/ciri/rlp/serializable.rb, line 145 def rlp_decode(input) values = decode_list(input) do |list, stream| keys.each do |key| # decode data by type list << decode_with_type(stream, self[key].type) end end # convert to key value hash keys.zip(values).to_h end
rlp_encode(data, skip_keys: nil, white_list_keys: nil)
click to toggle source
# File lib/ciri/rlp/serializable.rb, line 128 def rlp_encode(data, skip_keys: nil, white_list_keys: nil) # pre-encode, encode data to rlp compatible format(only string or array) used_keys = if white_list_keys white_list_keys elsif skip_keys keys - skip_keys else keys end data_list = [] used_keys.each do |key| value = data[key] data_list << encode_with_type(value, self[key].type) end encode_list(data_list) end
validate!(data)
click to toggle source
Validate data, data is a Hash
# File lib/ciri/rlp/serializable.rb, line 122 def validate!(data) keys.each do |key| raise InvalidSchemaError.new("missing key #{key}") unless data.key?(key) end end
Private Instance Methods
check_key_type(type)
click to toggle source
# File lib/ciri/rlp/serializable.rb, line 158 def check_key_type(type) return true if TYPES.key?(type) return true if type.is_a?(Class) && type.respond_to?(:rlp_decode) && type.respond_to?(:rlp_encode) if type.is_a?(Array) && type.size == 1 check_key_type(type[0]) else false end end