Module: RLP::Sedes::Serializable
- Defined in:
- lib/rlp/sedes/serializable.rb
Overview
Mixin for objects which can be serialized into RLP lists.
‘fields` defines which attributes are serialized and how this is done. It is expected to be a hash in the form of `name => sedes`. Here, `name` is the name of an attribute and `sedes` is the sedes object that will be used to serialize the corresponding attribute. The object as a whole is then serialized as a list of those fields.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#_cached_rlp ⇒ Object
Returns the value of attribute _cached_rlp.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #_set_field(field, value) ⇒ Object
- #initialize(*args) ⇒ Object
- #make_immutable! ⇒ Object
- #make_mutable! ⇒ Object
- #mutable? ⇒ Boolean
-
#parse_field_args(args) ⇒ Object
Mimic python’s argument syntax, accept both normal arguments and named arguments.
- #serializable_initialize(fields) ⇒ Object
Instance Attribute Details
#_cached_rlp ⇒ Object
Returns the value of attribute _cached_rlp.
98 99 100 |
# File 'lib/rlp/sedes/serializable.rb', line 98 def _cached_rlp @_cached_rlp end |
Class Method Details
.included(base) ⇒ Object
93 94 95 |
# File 'lib/rlp/sedes/serializable.rb', line 93 def included(base) base.extend ClassMethods end |
Instance Method Details
#==(other) ⇒ Object
152 153 154 155 |
# File 'lib/rlp/sedes/serializable.rb', line 152 def ==(other) return false unless other.class.respond_to?(:serialize) self.class.serialize(self) == other.class.serialize(other) end |
#_set_field(field, value) ⇒ Object
142 143 144 145 146 147 148 149 150 |
# File 'lib/rlp/sedes/serializable.rb', line 142 def _set_field(field, value) make_mutable! unless instance_variable_defined?(:@_mutable) if mutable? || !self.class.serializable_fields.has_key?(field) instance_variable_set :"@#{field}", value else raise ArgumentError, "Tried to mutate immutable object" end end |
#initialize(*args) ⇒ Object
100 101 102 |
# File 'lib/rlp/sedes/serializable.rb', line 100 def initialize(*args) serializable_initialize parse_field_args(args) end |
#make_immutable! ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'lib/rlp/sedes/serializable.rb', line 161 def make_immutable! make_mutable! self.class.serializable_fields.keys.each do |field| ::RLP::Utils.make_immutable! send(field) end @_mutable = false self end |
#make_mutable! ⇒ Object
171 172 173 |
# File 'lib/rlp/sedes/serializable.rb', line 171 def make_mutable! @_mutable = true end |
#mutable? ⇒ Boolean
157 158 159 |
# File 'lib/rlp/sedes/serializable.rb', line 157 def mutable? @_mutable end |
#parse_field_args(args) ⇒ Object
Mimic python’s argument syntax, accept both normal arguments and named arguments. Normal argument overrides named argument.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/rlp/sedes/serializable.rb', line 108 def parse_field_args(args) h = {} = args.last.is_a?(Hash) ? args.pop : {} field_set = self.class.serializable_fields.keys fields = self.class.serializable_fields.keys[0,args.size] fields.zip(args).each do |(field, arg)| h[field] = arg field_set.delete field end .each do |field, value| if field_set.include?(field) h[field] = value field_set.delete field end end h end |
#serializable_initialize(fields) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/rlp/sedes/serializable.rb', line 130 def serializable_initialize(fields) make_mutable! field_set = self.class.serializable_fields.keys fields.each do |field, value| _set_field field, value field_set.delete field end raise TypeError, "Not all fields initialized" unless field_set.size == 0 end |