Skip to content

Commit

Permalink
Refactor model type handling and improve result serialization
Browse files Browse the repository at this point in the history
Refactor code to replace "model_type" with "direction" throughout various modules for better readability and consistency. Improve the `to_hash` method in the `Datory::Result` class to exclude ignored keys and handle nested hashes correctly. Update attributes and examples to reflect these changes.
  • Loading branch information
afuno committed Aug 5, 2024
1 parent b6bde12 commit ed08fc1
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 139 deletions.
8 changes: 4 additions & 4 deletions lib/datory/attributes/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def input_serialization_options # rubocop:disable Metrics/AbcSize, Metrics/Metho

def output_serialization_options # rubocop:disable Metrics/AbcSize
hash = {
consists_of: to.consists_of == Hash ? Datory::Result : from.consists_of,
type: to.type == Datory::Result ? Hash : from.type
consists_of: from.consists_of,
type: from.type
}

hash[:min] = from.min if from.min.present?
Expand Down Expand Up @@ -97,8 +97,8 @@ def input_deserialization_options # rubocop:disable Metrics/MethodLength, Metric

def output_deserialization_options # rubocop:disable Metrics/AbcSize
hash = {
consists_of: from.consists_of == Hash ? Datory::Result : to.consists_of,
type: from.type == Hash ? Datory::Result : to.type
consists_of: to.consists_of,
type: to.type
}

hash[:min] = to.min if to.min.present?
Expand Down
4 changes: 3 additions & 1 deletion lib/datory/attributes/deserialization/service_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def initialize(context, incoming_attributes, collection_of_attributes)
def build!
ServiceFactory.create(@context.class, @collection_of_attributes)

builder_class.call!(**@incoming_attributes)
result = builder_class.call!(**@incoming_attributes)

@context.class.serialization.new(**result.to_hash)
end

private
Expand Down
4 changes: 2 additions & 2 deletions lib/datory/attributes/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def one!(name, include:, to: nil)
to: to.presence || name,
from: [include, Hash],
include: include,
as: [include, Datory::Result, Hash]
as: [include, Hash]
)
end
# NOTE: This will most likely be marked as deprecated in the future in favor of `one!`
Expand All @@ -51,7 +51,7 @@ def many!(name, include:, to: nil)
name,
to: to.presence || name,
from: Array,
consists_of: [include, Datory::Result, Hash],
consists_of: [include, Hash],
include: include,
as: Array
)
Expand Down
29 changes: 4 additions & 25 deletions lib/datory/attributes/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,21 @@ def self.build!(...)
new(...).build!
end

def initialize(context, model_type, attributes, collection_of_attributes)
def initialize(context, direction, attributes, collection_of_attributes)
@context = context
@model_type = model_type
@direction = direction
@attributes = attributes
@collection_of_attributes = collection_of_attributes
end

def build!
@collection_of_attributes.each do |attribute|
attribute_name = @model_type == :serialization ? attribute.to.name : attribute.from.name
attribute_name = @direction.serialization? ? attribute.to.name : attribute.from.name
attribute_value = @attributes.fetch(attribute_name, nil)

if attribute_value.is_a?(Hash)
# TODO
else
assign_attribute_for(@context, name: attribute_name, value: attribute_value)
end
assign_attribute_for(@context, name: attribute_name, value: attribute_value)
end

# @attributes.each do |attribute_name, attribute_value|
# if attribute_value.is_a?(Hash)
# # nested_attribute = @collection_of_attributes.find_by(name: attribute_name)
# #
# # nested_result =
# # if nested_attribute.present?
# # nested_attribute.to.include_class.new(**attribute_value)
# # else
# # attribute_value
# # end
# #
# # assign_attribute_for(nested_attribute.to.include_class, name: attribute_name, value: nested_result)
# else
# assign_attribute_for(@context, name: attribute_name, value: attribute_value)
# end
# end

@context
end

Expand Down
4 changes: 2 additions & 2 deletions lib/datory/attributes/workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def deserialize(incoming_attributes:, collection_of_attributes:)
Deserialization::ServiceBuilder.build!(self, incoming_attributes, collection_of_attributes)
end

def to_model(model_type:, attributes:, collection_of_attributes:)
def to_model(direction:, attributes:, collection_of_attributes:)
super

Model.build!(self, model_type, attributes, collection_of_attributes)
Model.build!(self, direction, attributes, collection_of_attributes)

self
end
Expand Down
12 changes: 7 additions & 5 deletions lib/datory/context/callable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ def new(_datory_to_model: true, **attributes) # rubocop:disable Lint/UnderscoreP

return context unless _datory_to_model

model_type = :serialization
direction = :serialization

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

_to_model(context, model_type: model_type, **attributes)
direction = direction.to_s.inquiry

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

def describe
Expand Down Expand Up @@ -106,10 +108,10 @@ def _deserialize(context, **attributes)
)
end

def _to_model(context, model_type:, **attributes)
def _to_model(context, direction:, **attributes)
context.send(
:_to_model,
model_type: model_type,
direction: direction,
attributes: attributes,
collection_of_attributes: collection_of_attributes
)
Expand Down
4 changes: 2 additions & 2 deletions lib/datory/context/workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ def _deserialize(incoming_attributes:, collection_of_attributes:)
)
end

def _to_model(model_type:, attributes:, collection_of_attributes:)
def _to_model(direction:, attributes:, collection_of_attributes:)
to_model(
model_type: model_type,
direction: direction,
attributes: attributes,
collection_of_attributes: collection_of_attributes
)
Expand Down
17 changes: 16 additions & 1 deletion lib/datory/result.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# frozen_string_literal: true

module Datory
class Result < Servactory::Result; end
class Result < Servactory::Result
IGNORED_KEYS = %i[success? failure?].freeze
private_constant :IGNORED_KEYS

def to_hash(result = self)
result.methods(false).filter do |key|
!key.in?(IGNORED_KEYS)
end.to_h do |key| # rubocop:disable Style/MultilineBlockChain
value = result.public_send(key)

value = value.except(*IGNORED_KEYS) if value.is_a?(Hash)

[key.to_s.delete_prefix("@").to_sym, value]
end.compact
end
end
end
Loading

0 comments on commit ed08fc1

Please sign in to comment.