Class: Apiwork::Adapter::Serializer::Resource::Base
- Inherits:
-
Object
- Object
- Apiwork::Adapter::Serializer::Resource::Base
- 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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#representation_class ⇒ Class<Representation::Base>
readonly
The representation class for this serializer.
Class Method Summary collapse
-
.contract_builder(klass = nil) ⇒ Class<Builder::Contract::Base>?
The contract builder for this serializer.
-
.data_type(&block) ⇒ Proc?
The data type for this serializer.
- .serialize(representation_class, resource, context:, serialize_options:) ⇒ Object
Instance Method Summary collapse
- #contract_types(contract_class) ⇒ Object
-
#initialize(representation_class) ⇒ Base
constructor
A new instance of Base.
-
#serialize(resource, context:, serialize_options:) ⇒ Hash
Serializes a resource.
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_class ⇒ Class<Representation::Base> (readonly)
The representation class for this serializer.
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.
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.
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.
79 80 81 |
# File 'lib/apiwork/adapter/serializer/resource/base.rb', line 79 def serialize(resource, context:, serialize_options:) raise NotImplementedError end |