Class: Codebot::Serializable

Inherits:
Object
  • Object
show all
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

Channel, Integration, Network

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deserialize(_key = nil, _val) ⇒ Hash

Note:

Child classes should override this method.

Deserializes an object.

Parameters:

  • _key (Object) (defaults to: nil)

    the hash key if the value was serialized into a hash

  • _val (Hash)

    the serialized data

Returns:

  • (Hash)

    the parameters to pass to the initializer



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.

Parameters:

  • data (Array, Hash)

    the data to deserialize

  • conf (Hash)

    the previously deserialized configuration

Returns:

  • (Array)

    the deserialized data



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.

Parameters:

  • ary (Array)

    the data to serialize

  • conf (Hash)

    the deserialized configuration

Returns:

  • (Array, Hash)

    the serialized data



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

Note:

Child classes might want to override this method.

Returns whether data is serialized into a hash rather than an array.

Returns:

  • (Boolean)

    whether data is serialized into an array containing two elements representing key and value of a Hash.



62
63
64
# File 'lib/codebot/serializable.rb', line 62

def self.serialize_as_hash?
  false
end

Instance Method Details

#serialize(_conf) ⇒ Array, Hash

Note:

Child classes should override this method.

Serializes this object.

Parameters:

  • _conf (Hash)

    the deserialized configuration

Returns:

  • (Array, Hash)

    the serialized object



43
44
45
# File 'lib/codebot/serializable.rb', line 43

def serialize(_conf)
  []
end