Class: Paquito::Types::CustomTypesRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/paquito/types.rb

Class Method Summary collapse

Class Method Details

.packer(value) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/paquito/types.rb', line 33

def packer(value)
  packers.fetch(klass = value.class) do
    if packable?(value) && unpackable?(klass)
      @packers[klass] = SERIALIZE_PROC
    end
  end
end

.register(klass, packer: nil, unpacker:) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/paquito/types.rb', line 49

def register(klass, packer: nil, unpacker:)
  if packer
    raise ArgumentError, "packer for #{klass} already defined" if packers.key?(klass)

    packers[klass] = packer
  end

  raise ArgumentError, "unpacker for #{klass} already defined" if unpackers.key?(klass)

  unpackers[klass] = unpacker

  self
end

.unpacker(klass) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/paquito/types.rb', line 41

def unpacker(klass)
  unpackers.fetch(klass) do
    if unpackable?(klass)
      @unpackers[klass] = klass.method(DESERIALIZE_METHOD).to_proc
    end
  end
end