Class: Castkit::Serializers::DefaultSerializer

Inherits:
Base
  • Object
show all
Defined in:
lib/castkit/serializers/default_serializer.rb

Overview

Default serializer for Castkit::DataObject instances.

Serializes attributes into a plain Ruby hash, applying access rules, nil/blank filtering, and nested structure handling. The output format supports JSON-compatible structures and respects the class-level serialization configuration.

Instance Attribute Summary collapse

Attributes inherited from Base

#object

Instance Method Summary collapse

Methods inherited from Base

call

Instance Attribute Details

#attributesHash{Symbol => Castkit::Attribute} (readonly)

Returns the attributes to serialize.

Returns:



14
15
16
# File 'lib/castkit/serializers/default_serializer.rb', line 14

def attributes
  @attributes
end

#optionsHash (readonly)

Returns serialization config flags like :root, :ignore_nil, :allow_unknown.

Returns:

  • (Hash)

    serialization config flags like :root, :ignore_nil, :allow_unknown



20
21
22
# File 'lib/castkit/serializers/default_serializer.rb', line 20

def options
  @options
end

#unknown_attributesHash{Symbol => Object} (readonly)

Returns unrecognized attributes captured during deserialization.

Returns:

  • (Hash{Symbol => Object})

    unrecognized attributes captured during deserialization



17
18
19
# File 'lib/castkit/serializers/default_serializer.rb', line 17

def unknown_attributes
  @unknown_attributes
end

Instance Method Details

#callHash

Serializes the object to a hash.

Includes unknown attributes if configured, and wraps in a root key if defined.

Returns:

  • (Hash)

    the fully serialized result



27
28
29
30
31
32
# File 'lib/castkit/serializers/default_serializer.rb', line 27

def call
  result = serialize_attributes
  result.merge!(unknown_attributes) if options[:allow_unknown]

  options[:root] ? { options[:root].to_sym => result } : result
end