Sha256: 0d74908e8be066e804004244480b5739e5e8907939e70725dfa816ff346e7d9e

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module Batch
      #
      # SeregaAttributeNormalizer additional/patched instance methods
      #
      # @see SeregaAttributeNormalizer::AttributeInstanceMethods
      #
      module AttributeNormalizerInstanceMethods
        #
        # Returns normalized attribute :batch option with prepared :key and
        # :default options. Option :loader will be prepared at serialization
        # time as loaders are usually defined after attributes.
        #
        # @return [Hash] attribute :batch normalized options
        #
        def batch
          return @batch if instance_variable_defined?(:@batch)

          @batch = prepare_batch
        end

        private

        #
        # Patch for original `prepare_hide` method
        #
        # Marks attribute hidden if auto_hide option was set and attribute has batch loader
        #
        def prepare_hide
          res = super
          return res unless res.nil?

          if batch
            self.class.serializer_class.config.batch.auto_hide || nil
          end
        end

        def prepare_batch
          batch = init_opts[:batch]
          return unless batch

          # take loader
          loader = batch[:loader]

          # take key
          key = batch[:key] || self.class.serializer_class.config.batch.default_key
          proc_key =
            if key.is_a?(Symbol)
              proc { |object| object.public_send(key) }
            else
              key
            end

          # take default value
          default = batch.fetch(:default) { many ? FROZEN_EMPTY_ARRAY : nil }

          {loader: loader, key: proc_key, default: default}
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
serega-0.16.0 lib/serega/plugins/batch/lib/modules/attribute_normalizer.rb
serega-0.15.0 lib/serega/plugins/batch/lib/modules/attribute_normalizer.rb
serega-0.14.0 lib/serega/plugins/batch/lib/modules/attribute_normalizer.rb
serega-0.12.0 lib/serega/plugins/batch/lib/modules/attribute_normalizer.rb