Class: Skit::Serialization::Processor::JsonSchemaConst

Inherits:
Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/skit/serialization/processor/json_schema_const.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#traverse

Constructor Details

#initialize(type_spec, registry:) ⇒ JsonSchemaConst

Returns a new instance of JsonSchemaConst.



18
19
20
21
22
23
24
25
# File 'lib/skit/serialization/processor/json_schema_const.rb', line 18

def initialize(type_spec, registry:)
  super
  unless type_spec.is_a?(Class) && type_spec < Skit::JsonSchema::Types::Const
    raise ArgumentError, "Expected Skit::JsonSchema::Types::Const subclass, got #{type_spec}"
  end

  @const_class = T.let(type_spec, T.class_of(Skit::JsonSchema::Types::Const))
end

Class Method Details

.handles?(type_spec) ⇒ Boolean

Returns:



11
12
13
14
15
# File 'lib/skit/serialization/processor/json_schema_const.rb', line 11

def self.handles?(type_spec)
  return false unless type_spec.is_a?(Class)

  !!(type_spec < Skit::JsonSchema::Types::Const)
end

Instance Method Details

#deserialize(value, path: Path.new) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/skit/serialization/processor/json_schema_const.rb', line 38

def deserialize(value, path: Path.new)
  return value if value.is_a?(@const_class)

  expected = @const_class.value

  unless value == expected
    raise DeserializeError.new(
      "Expected #{expected.inspect}, got #{value.inspect} for #{@const_class}",
      path: path
    )
  end

  @const_class.new
end

#serialize(value, path: Path.new) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/skit/serialization/processor/json_schema_const.rb', line 28

def serialize(value, path: Path.new)
  unless value.is_a?(@const_class)
    raise SerializeError.new("Expected #{@const_class}, got #{value.class}",
                             path: path)
  end

  value.value
end