Class: Ciri::RLP::Serializable::Schema
- Inherits:
-
Object
- Object
- Ciri::RLP::Serializable::Schema
- Defined in:
- lib/ciri/rlp/serializable.rb
Overview
Schema specific columns types of classes, normally you should not use Serializable::Schema directly
Defined Under Namespace
Classes: InvalidSchemaError, KeySchema
Instance Attribute Summary collapse
-
#keys ⇒ Object
readonly
keys return data columns array.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get column type, see Serializable::TYPES for supported type.
-
#initialize(schema) ⇒ Schema
constructor
A new instance of Schema.
- #rlp_decode(input) ⇒ Object
- #rlp_encode(data, skip_keys: nil, white_list_keys: nil) ⇒ Object
-
#validate!(data) ⇒ Object
Validate data, data is a Hash.
Methods included from Decode
Methods included from Encode
#encode, #encode_simple, #encode_with_type
Constructor Details
#initialize(schema) ⇒ Schema
Returns a new instance of Schema.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ciri/rlp/serializable.rb', line 94 def initialize(schema) keys = [] @_schema = {} schema.each do |key| if key.is_a?(Hash) = [:optional].map {|o| [o, key.delete(o)]}.to_h raise InvalidSchemaError.new("include unknown options #{key}") unless key.size == 1 key, type = key.to_a[0] else = {} type = Raw end raise InvalidSchemaError.new("missing type #{type} for key #{key}") unless check_key_type(type) keys << key @_schema[key] = KeySchema.new(type: type, options: ) end @_schema.freeze @keys = keys.freeze end |
Instance Attribute Details
#keys ⇒ Object (readonly)
keys return data columns array
90 91 92 |
# File 'lib/ciri/rlp/serializable.rb', line 90 def keys @keys end |
Instance Method Details
#[](key) ⇒ Object
Get column type, see Serializable::TYPES for supported type
117 118 119 |
# File 'lib/ciri/rlp/serializable.rb', line 117 def [](key) @_schema[key] end |
#rlp_decode(input) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/ciri/rlp/serializable.rb', line 146 def rlp_decode(input) values = decode_list(input) do |list, stream| keys.each do |key| # decode data by type next if stream.eof? && self[key].[:optional] 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) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# 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] next if value.nil? && self[key].[:optional] data_list << encode_with_type(value, self[key].type) end encode_list(data_list) end |
#validate!(data) ⇒ Object
Validate data, data is a Hash
122 123 124 125 126 |
# 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 |