Sha256: 54b649b99ffec06281c036352d9e4e56a9a9cb85af13a0da65ed2c51ae8154a8

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

require_relative 'serialization/mixin'
require_relative 'serialization/serializer_attribute_set'
require_relative 'serialization/serializer_attribute'

module LedgerSync
  class Serializer
    include Serialization::Mixin

    def serialize(args = {})
      only_changes = args.fetch(:only_changes, false)
      resource     = args.fetch(:resource)

      ret = {}

      self.class.attributes.each_value do |serializer_attribute|
        next if only_changes && !resource.attribute_changed?(serializer_attribute.resource_attribute)

        ret = Util::HashHelpers.deep_merge(
          hash_to_merge_into: ret,
          other_hash: serializer_attribute.hash_attribute_hash_for(resource: resource)
        )
      end

      ret
    end

    def self.attribute(hash_attribute, args = {}, &block)
      _attribute(
        args.merge(
          block: (block if block_given?),
          hash_attribute: hash_attribute
        )
      )
    end

    def self.attribute_class
      Serialization::SerializerAttribute
    end

    def self.attributes
      @attributes ||= Serialization::SerializerAttributeSet.new(serializer_class: self)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ledger_sync-1.3.5 lib/ledger_sync/serializer.rb