lib/ledger_sync/deserializer.rb in ledger_sync-1.3.5 vs lib/ledger_sync/deserializer.rb in ledger_sync-1.4.0

- old
+ new

@@ -6,12 +6,24 @@ module LedgerSync class Deserializer include Serialization::Mixin - def attribute_value_from_ledger(hash:, ledger_serializer_attribute:, resource:) - ledger_serializer_attribute.value_from_hash( + class Delegator + def deserialize(args = {}) + deserializer_for(args).new.deserialize(args) + end + + private + + def deserializer_for(_args = {}) + raise NotImplementedError + end + end + + def attribute_value_from_ledger(hash:, deserializer_attribute:, resource:) + deserializer_attribute.value_from_hash( hash: hash, resource: resource ) end @@ -20,27 +32,31 @@ resource = args.fetch(:resource) deserialize_into = resource.dup # Do not overwrite values in the resource hash = Util::HashHelpers.deep_stringify_keys(hash) - self.class.attributes.each_value do |ledger_serializer_attribute| + self.class.attributes.each_value do |deserializer_attribute| value = attribute_value_from_ledger( hash: hash, - ledger_serializer_attribute: ledger_serializer_attribute, + deserializer_attribute: deserializer_attribute, resource: deserialize_into ) deserialize_into.assign_attribute( - ledger_serializer_attribute.resource_attribute_dot_parts.first, + deserializer_attribute.resource_attribute_dot_parts.first, value ) end deserialize_into end def self.attribute(resource_attribute, args = {}, &block) + if args.key?(:resource_attribute) + raise 'You cannot provide resource_attribute in args. Pass the value as the first argument.' + end + _attribute( args.merge( block: (block if block_given?), resource_attribute: resource_attribute ) @@ -52,8 +68,41 @@ end def self.attributes @attributes ||= Serialization::DeserializerAttributeSet.new(serializer_class: self) end + + def self.references_one(resource_attribute, args = {}, &block) + attribute( + resource_attribute, + { + type: Serialization::Type::DeserializerReferencesOneType.new( + deserializer: deserializer_from(resource_attribute, args) + ) + }.merge(args), + &block + ) + end + + def self.references_many(resource_attribute, args = {}, &block) + attribute( + resource_attribute, + { + type: Serialization::Type::DeserializerReferencesManyType.new( + deserializer: deserializer_from(resource_attribute, args) + ) + }.merge(args), + &block + ) + end + + def self.deserializer_from(resource_attribute, args = {}) + if args.key?(:deserializer) + args.fetch(:deserializer) + else + resource_key = inferred_resource_class.resource_attributes[resource_attribute].type.resource_class.resource_type + require "ledger_sync/ledgers/#{inferred_client_class.root_key}/#{resource_key}/deserializer" + inferred_client_class.resources[resource_key]::Deserializer + end + end end end -