Class: Mutant::Transform::Codec Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mutant/transform/codec.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Bidirectional codec over Ruby objects.

Wraps a pair of dump/load transforms that convert between domain objects and a JSON-compatible Ruby structure (hashes, arrays, primitives). The outer layer that converts the structure to/from a JSON string is handled by callers as needed.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_anima(klass) ⇒ Codec

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build a codec for simple Anima objects with primitive fields

Parameters:

  • klass (Class)

Returns:



19
20
21
22
23
24
# File 'lib/mutant/transform/codec.rb', line 19

def self.for_anima(klass)
  new(
    dump_transform: Success.new(block: ->(object) { object.to_h.transform_keys(&:to_s) }),
    load_transform: Success.new(block: ->(hash) { klass.new(**hash.transform_keys(&:to_sym)) })
  )
end

Instance Method Details

#dump(object) ⇒ Either<Error, Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Dump object to Ruby structure

Parameters:

  • object (Object)

Returns:



31
32
33
# File 'lib/mutant/transform/codec.rb', line 31

def dump(object)
  dump_transform.call(object)
end

#load(input) ⇒ Either<Error, Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load object from Ruby structure

Parameters:

  • input (Object)

Returns:



40
41
42
# File 'lib/mutant/transform/codec.rb', line 40

def load(input)
  load_transform.call(input)
end