Module: Exa::Types::Serializer

Defined in:
lib/exa/types/base.rb

Class Method Summary collapse

Class Method Details

.camelize(sym) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/exa/types/base.rb', line 47

def camelize(sym)
  key = sym.to_s
  return key if key.include?("$")
  parts = key.split("_")
  return key if parts.length == 1
  parts.first + parts[1..].map(&:capitalize).join
end

.serialize_hash(hash) ⇒ Object



40
41
42
43
44
45
# File 'lib/exa/types/base.rb', line 40

def serialize_hash(hash)
  hash.each_with_object({}) do |(key, val), acc|
    next if val.nil?
    acc[camelize(key)] = to_payload(val)
  end
end

.serialize_struct(struct) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/exa/types/base.rb', line 30

def serialize_struct(struct)
  struct.serialize.each_with_object({}) do |(key, val), acc|
    next if val.nil?
    method_name = key.to_sym
    raw_value = struct.respond_to?(method_name) ? struct.public_send(method_name) : nil
    schema_value = Exa::Types::Schema.maybe_convert(raw_value)
    acc[camelize(method_name)] = schema_value.nil? ? to_payload(val) : schema_value
  end
end

.to_payload(value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/exa/types/base.rb', line 8

def to_payload(value)
  schema_payload = Exa::Types::Schema.maybe_convert(value)
  return schema_payload unless schema_payload.nil?

  case value
  when StructWrapper
    value.__exa_attributes__
  when ::T::Struct
    serialize_struct(value)
  when Hash
    serialize_hash(value)
  when Array
    value.map { to_payload(_1) }
  when Symbol
    value.to_s
  when T::Enum
    value.serialize.to_s
  else
    value
  end
end