Class: Chronicle::Serialization::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/chronicle/serialization/serializer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, options = {}) ⇒ Serializer

Construct a new instance of this serializer.

Parameters:

options

Options for configuring this Serializers



8
9
10
11
12
13
14
15
16
# File 'lib/chronicle/serialization/serializer.rb', line 8

def initialize(record, options = {})
  unless record.is_a?(Chronicle::Serialization::Record)
    raise(ArgumentError,
      "Must be a Record. It is a: #{record.class}")
  end

  @record = record
  @options = options
end

Class Method Details

.serialize(data) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chronicle/serialization/serializer.rb', line 23

def self.serialize(data)
  record = if data.is_a?(Chronicle::Models::Base)
             Chronicle::Serialization::Record.build_from_model(data)
           elsif data.is_a?(Chronicle::Serialization::Record)
             data
           else
             raise ArgumentError, 'data must be a Chronicle::Models::Base or Chronicle::Serialization::Record'
           end

  serializer = new(record)
  serializer.serializable_hash
end

Instance Method Details

#serializable_hashObject

Serialize a record as a hash

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/chronicle/serialization/serializer.rb', line 19

def serializable_hash
  raise NotImplementedError
end