Sha256: 56ae022d9649dd88c2104700d3a06badda4f01e28a0f602ec708f484734f1d4f

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

class Serega
  class SeregaMap
    module ClassMethods
      def call(opts)
        max_cache_size = serializer_class.config.max_cached_map_per_serializer_count
        return map_for(opts) if max_cache_size.zero?

        cached_map_for(opts, max_cache_size)
      end

      private

      def map_for(opts)
        construct_map(serializer_class, **modifiers(opts))
      end

      def cached_map_for(opts, max_cache_size)
        @cache ||= {}
        cache_key = opts.to_s
        map = @cache[cache_key] ||= map_for(opts)
        @cache.shift if @cache.length > max_cache_size
        map
      end

      def modifiers(opts)
        {
          only: opts[:only] || FROZEN_EMPTY_HASH,
          except: opts[:except] || FROZEN_EMPTY_HASH,
          with: opts[:with] || FROZEN_EMPTY_HASH
        }
      end

      def construct_map(serializer_class, only:, except:, with:)
        serializer_class.attributes.each_with_object([]) do |(name, attribute), map|
          next unless attribute.visible?(only: only, except: except, with: with)

          nested_points =
            if attribute.relation?
              construct_map(
                attribute.serializer,
                only: only[name] || FROZEN_EMPTY_HASH,
                with: with[name] || FROZEN_EMPTY_HASH,
                except: except[name] || FROZEN_EMPTY_HASH
              )
            end

          map << serializer_class::SeregaMapPoint.new(attribute, nested_points)
        end
      end
    end

    extend ClassMethods
    extend Serega::SeregaHelpers::SerializerClassHelper
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
serega-0.6.1 lib/serega/map.rb
serega-0.6.0 lib/serega/map.rb
serega-0.5.2 lib/serega/map.rb
serega-0.5.1 lib/serega/map.rb
serega-0.5.0 lib/serega/map.rb