Class: Codebot::Serializable
- Inherits:
-
Object
- Object
- Codebot::Serializable
- Defined in:
- lib/codebot/serializable.rb
Overview
A class that can be serialized. Child classes should override the #serialize and deserialize methods, and change serialize_as_hash? to return true
if the #serialize method returns an Array
containing two elements representing key and value of a Hash
.
Direct Known Subclasses
Class Method Summary collapse
-
.deserialize(_key = nil, _val) ⇒ Hash
Deserializes an object.
-
.deserialize_all(data, conf) ⇒ Array
Deserializes an array or a hash into an array.
-
.serialize_all(ary, conf) ⇒ Array, Hash
Serializes an array into an array or a hash.
-
.serialize_as_hash? ⇒ Boolean
Returns whether data is serialized into a hash rather than an array.
Instance Method Summary collapse
-
#serialize(_conf) ⇒ Array, Hash
Serializes this object.
Class Method Details
.deserialize(_key = nil, _val) ⇒ Hash
Child classes should override this method.
Deserializes an object.
53 54 55 |
# File 'lib/codebot/serializable.rb', line 53 def self.deserialize(_key = nil, _val) {} end |
.deserialize_all(data, conf) ⇒ Array
Deserializes an array or a hash into an array.
28 29 30 31 32 33 34 35 36 |
# File 'lib/codebot/serializable.rb', line 28 def self.deserialize_all(data, conf) return [] if data.nil? if serialize_as_hash? deserialize_all_from_hash(data, conf) else deserialize_all_from_array(data, conf) end end |
.serialize_all(ary, conf) ⇒ Array, Hash
Serializes an array into an array or a hash.
16 17 18 19 20 21 |
# File 'lib/codebot/serializable.rb', line 16 def self.serialize_all(ary, conf) data = ary.map { |entry| entry.serialize(conf) } return data.to_h if serialize_as_hash? data end |
.serialize_as_hash? ⇒ Boolean
Child classes might want to override this method.
Returns whether data is serialized into a hash rather than an array.
62 63 64 |
# File 'lib/codebot/serializable.rb', line 62 def self.serialize_as_hash? false end |
Instance Method Details
#serialize(_conf) ⇒ Array, Hash
Child classes should override this method.
Serializes this object.
43 44 45 |
# File 'lib/codebot/serializable.rb', line 43 def serialize(_conf) [] end |