Sha256: 9e7b62ad6a46a1707884a0385b9088258356d923053b881fd395d8013c5fc344

Contents?: true

Size: 1.44 KB

Versions: 17

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require_relative 'serializer/struct'

module LedgerSync
  module Domains
    class Serializer < LedgerSync::Serializer
      def self.split_attributes
        regular = []
        references = []

        attributes.each_value do |attr|
          if attr.references_many? || attr.references_one?
            references.push(attr)
          else
            regular.push(attr)
          end
        end
        [regular, references]
      end

      def serialize(args = {}) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
        only_changes = args.fetch(:only_changes, false)
        resource     = args.fetch(:resource)

        ret = {}

        regular, references = self.class.split_attributes
        regular.each do |serializer_attribute|
          if (only_changes && !resource.attribute_changed?(serializer_attribute.resource_attribute)) || # rubocop:disable Layout/LineLength
             (serializer_attribute.if_method.present? && !send(serializer_attribute.if_method, resource: resource)) # rubocop:disable Layout/LineLength
            next
          end

          ret = LedgerSync::Util::HashHelpers.deep_merge(
            hash_to_merge_into: ret,
            other_hash: serializer_attribute.hash_attribute_hash_for(resource: resource)
          )
        end
        Serializer::Struct.build(
          ret, self.class.to_s,
          resource: resource, references: references
        )
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ledger_sync-domains-1.2.1 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.2.0 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.1.4 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.1.3 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.1.1 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.1.0 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.0.5 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.0.4 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.0.3 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.0.2 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.0.1 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.0.0 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.0.0.rc10 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.0.0.rc9 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.0.0.rc8 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.0.0.rc7 lib/ledger_sync/domains/serializer.rb
ledger_sync-domains-1.0.0.rc6 lib/ledger_sync/domains/serializer.rb