Class: Datory::Service::Builder

Inherits:
Base
  • Object
show all
Defined in:
lib/datory/service/builder.rb

Class Method Summary collapse

Class Method Details

.prepare_deserialization_data_for(attribute) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/datory/service/builder.rb', line 53

def self.prepare_deserialization_data_for(attribute) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  serialized_name = attribute.from.name
  deserialized_name = attribute.to.name
  method_name = :"assign_#{deserialized_name}_output"

  input serialized_name, **attribute.input_deserialization_options

  output deserialized_name, **attribute.output_deserialization_options

  make method_name

  define_method(method_name) do
    value = inputs.public_send(deserialized_name)

    if value.present?
      type = attribute.to.type

      # NOTE: For optional attributes.
      type = (type - [NilClass]).first if type.is_a?(Array)

      value = TRANSFORMATIONS.fetch(:DESERIALIZATION).fetch(type, ->(v) { v }).call(value)
    end

    outputs.public_send(:"#{deserialized_name}=", value)
  end
end

.prepare_serialization_data_for(attribute) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/datory/service/builder.rb', line 33

def self.prepare_serialization_data_for(attribute) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  serialized_name = attribute.from.name
  deserialized_name = attribute.to.name
  method_name = :"assign_#{serialized_name}_output"

  input deserialized_name, **attribute.input_serialization_options

  output serialized_name, **attribute.output_serialization_options

  make method_name

  define_method(method_name) do
    value = inputs.public_send(deserialized_name)

    value = TRANSFORMATIONS.fetch(:SERIALIZATION).fetch(value.class, ->(v) { v }).call(value)

    outputs.public_send(:"#{serialized_name}=", value)
  end
end