Sha256: e9ee33bd3cc1f4e4ba58ccd1548665b628281523e9174a30bf9f93a915e102ff

Contents?: true

Size: 1.15 KB

Versions: 13

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module IdentityCache
  module Cached
    class AttributeByOne < Attribute
      attr_reader :key_field

      def initialize(*)
        super
        @key_field = key_fields.first
      end

      def build
        cached_attribute = self

        model.define_singleton_method(:"fetch_#{fetch_method_suffix}") do |key|
          raise_if_scoped
          cached_attribute.fetch(key)
        end

        model.define_singleton_method(:"fetch_multi_#{fetch_method_suffix}") do |keys|
          raise_if_scoped
          cached_attribute.fetch_multi(keys)
        end
      end

      private

      # Attribute method overrides

      def cast_db_key(key)
        field_types.first.cast(key)
      end

      def unhashed_values_cache_key_string(key)
        key.try!(:to_s).inspect
      end

      def load_from_db_where_conditions(key_values)
        { key_field => key_values }
      end

      def load_multi_rows(keys)
        model.reorder(nil).where(load_from_db_where_conditions(keys)).pluck(key_field, attribute)
      end

      def cache_key_from_key_values(key_values)
        cache_key(key_values.first)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
identity_cache-1.6.3 lib/identity_cache/cached/attribute_by_one.rb
identity_cache-1.6.2 lib/identity_cache/cached/attribute_by_one.rb
identity_cache-1.6.1 lib/identity_cache/cached/attribute_by_one.rb
identity_cache-1.6.0 lib/identity_cache/cached/attribute_by_one.rb
identity_cache-1.5.6 lib/identity_cache/cached/attribute_by_one.rb
identity_cache-1.5.5 lib/identity_cache/cached/attribute_by_one.rb
identity_cache-1.5.4 lib/identity_cache/cached/attribute_by_one.rb
identity_cache-1.5.3 lib/identity_cache/cached/attribute_by_one.rb
identity_cache-1.5.2 lib/identity_cache/cached/attribute_by_one.rb
identity_cache-1.5.1 lib/identity_cache/cached/attribute_by_one.rb
identity_cache-1.5.0 lib/identity_cache/cached/attribute_by_one.rb
identity_cache-1.4.1 lib/identity_cache/cached/attribute_by_one.rb
identity_cache-1.4.0 lib/identity_cache/cached/attribute_by_one.rb