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.
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# 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 |
Instance Attribute Details
#keys ⇒ Object (readonly)
keys return data columns array
98 99 100 |
# File 'lib/ciri/rlp/serializable.rb', line 98 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
145 146 147 148 149 150 151 152 153 154 |
# 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) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# 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) ⇒ 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 |