Class: Apiwork::Adapter::Serializer::Resource::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/apiwork/adapter/serializer/resource/base.rb

Overview

Base class for resource serializers.

Resource serializers handle serialization of records and collections and define resource types at the contract level.

Examples:

class MyResourceSerializer < Serializer::Resource::Base
  contract_builder Builder::Contract

  def serialize(resource, context:, serialize_options:)
    representation_class.serialize(resource, context:)
  end
end

Direct Known Subclasses

Default

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(representation_class) ⇒ Base

Returns a new instance of Base.



56
57
58
# File 'lib/apiwork/adapter/serializer/resource/base.rb', line 56

def initialize(representation_class)
  @representation_class = representation_class
end

Instance Attribute Details

#representation_classClass<Representation::Base> (readonly)

The representation class for this serializer.

Returns:



54
55
56
# File 'lib/apiwork/adapter/serializer/resource/base.rb', line 54

def representation_class
  @representation_class
end

Class Method Details

.contract_builder(klass = nil) ⇒ Class<Builder::Contract::Base>?

The contract builder for this serializer.

Parameters:

Returns:



44
45
46
47
# File 'lib/apiwork/adapter/serializer/resource/base.rb', line 44

def contract_builder(klass = nil)
  @contract_builder = klass if klass
  @contract_builder
end

.data_type(&block) ⇒ Proc?

The data type for this serializer.

Parameters:

  • block (Proc, nil)

    (nil) Block that receives representation_class and returns type name.

Returns:

  • (Proc, nil)


33
34
35
36
# File 'lib/apiwork/adapter/serializer/resource/base.rb', line 33

def data_type(&block)
  @data_type = block if block
  @data_type
end

.serialize(representation_class, resource, context:, serialize_options:) ⇒ Object



23
24
25
# File 'lib/apiwork/adapter/serializer/resource/base.rb', line 23

def serialize(representation_class, resource, context:, serialize_options:)
  new(representation_class).serialize(resource, context:, serialize_options:)
end

Instance Method Details

#contract_types(contract_class) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/apiwork/adapter/serializer/resource/base.rb', line 60

def contract_types(contract_class)
  register_representation_types(contract_class)

  builder_class = self.class.contract_builder
  return unless builder_class

  builder_class.new(contract_class, representation_class).build
end

#serialize(resource, context:, serialize_options:) ⇒ Hash

Serializes a resource.

Parameters:

  • resource (Object)

    The resource to serialize.

  • context (Hash)

    The serialization context.

  • serialize_options (Hash)

    The options (e.g., include).

Returns:

  • (Hash)

Raises:

  • (NotImplementedError)


79
80
81
# File 'lib/apiwork/adapter/serializer/resource/base.rb', line 79

def serialize(resource, context:, serialize_options:)
  raise NotImplementedError
end