Module: Datory::Context::Callable

Defined in:
lib/datory/context/callable.rb

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#describeObject Also known as: table



78
79
80
81
82
83
# File 'lib/datory/context/callable.rb', line 78

def describe
  Datory::Attributes::Descriptor.describe(
    service_class_name: name,
    collection_of_attributes:
  )
end

#deserializationObject



92
93
94
95
96
# File 'lib/datory/context/callable.rb', line 92

def deserialization
  assign_datory_model_type(:deserialization)

  self
end

#deserialize(data) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/datory/context/callable.rb', line 27

def deserialize(data) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  context = send(:new, _datory_to_model: false)

  prepared_data =
    if data.is_a?(Datory::Base)
      Datory::Attributes::Serialization::Model.to_hash(data)
    elsif data.is_a?(String)
      JSON.parse(data)
    else
      data
    end

  if [Set, Array].include?(prepared_data.class)
    prepared_data.map do |item|
      deserialize(item)
    end
  else
    _deserialize(context, **prepared_data)
  end
rescue Datory::Service::Exceptions::Input,
       Datory::Service::Exceptions::Internal,
       Datory::Service::Exceptions::Output => e
  raise Datory::Exceptions::DeserializationError.new(context: e.send(:context), message: e.message)
rescue JSON::ParserError => e
  # TODO: Needs to be moved to I18n
  message = "Failed to parse data for deserialization: #{e.message}"

  raise Datory::Exceptions::DeserializationError.new(
    context:,
    message:,
    meta: { original_exception: e }
  )
end

#form(model) ⇒ Object



6
7
8
# File 'lib/datory/context/callable.rb', line 6

def form(model)
  Datory::Attributes::Form.new(self, model)
end

#new(_datory_to_model: true, **attributes) ⇒ Object

rubocop:disable Lint/UnderscorePrefixedVariableName



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/datory/context/callable.rb', line 61

def new(_datory_to_model: true, **attributes) # rubocop:disable Lint/UnderscorePrefixedVariableName
  context = super()

  return context unless _datory_to_model

  direction = :serialization

  if defined?(@_datory_model_type) && @_datory_model_type[context.class.name].present?
    direction = @_datory_model_type.fetch(context.class.name)
    @_datory_model_type.delete(context.class.name)
  end

  direction = direction.to_s.inquiry

  _to_model(context, direction:, **attributes)
end

#serializationObject



86
87
88
89
90
# File 'lib/datory/context/callable.rb', line 86

def serialization
  assign_datory_model_type(:serialization)

  self
end

#serialize(model) ⇒ Object

rubocop:disable Metrics/MethodLength



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/datory/context/callable.rb', line 10

def serialize(model) # rubocop:disable Metrics/MethodLength
  context = send(:new, _datory_to_model: false)

  if [Set, Array].include?(model.class)
    model.map do |model_item|
      serialize(model_item)
    end
  else
    model = Datory::Attributes::Serialization::Model.prepare(model)
    _serialize(context, model)
  end
rescue Datory::Service::Exceptions::Input,
       Datory::Service::Exceptions::Internal,
       Datory::Service::Exceptions::Output => e
  raise Datory::Exceptions::SerializationError.new(context: e.send(:context), message: e.message)
end