Class: Mayak::JsonCodec::FromHashSerializable

Inherits:
Object
  • Object
show all
Includes:
Codec, Monads::Try::Mixin
Defined in:
lib/mayak/json_codec/from_hash_serializable.rb

Constant Summary

Constants included from Codec

Codec::Entity, Codec::Protocol

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Codec

#to_decoder, #to_encoder

Methods included from Monads::Try::Mixin

#Try, #check_try!, #do_try!, #for_try, #guard_try!

Constructor Details

#initialize(type) ⇒ FromHashSerializable

Returns a new instance of FromHashSerializable.



14
15
16
# File 'lib/mayak/json_codec/from_hash_serializable.rb', line 14

def initialize(type)
  @type = type
end

Class Method Details

.[](type) ⇒ Object



18
19
20
# File 'lib/mayak/json_codec/from_hash_serializable.rb', line 18

def self.[](type)
  ::Mayak::JsonCodec::FromHashSerializable.new(type)
end

Instance Method Details

#decode(response) ⇒ Object



30
31
32
33
34
35
# File 'lib/mayak/json_codec/from_hash_serializable.rb', line 30

def decode(response)
  Try do
    parsed = JSON.parse(response)
    ::TypeCoerce::Converter.new(@type).from(parsed)
  end
end

#encode(entity) ⇒ Object



26
27
28
# File 'lib/mayak/json_codec/from_hash_serializable.rb', line 26

def encode(entity)
  JSON.dump(entity.serialize)
end

#newObject



22
23
24
# File 'lib/mayak/json_codec/from_hash_serializable.rb', line 22

def new
  self
end