Sha256: f380cd647b08c42893684a75f7bf0750ef3a3e633726d17c933a4787ad135ad6

Contents?: true

Size: 874 Bytes

Versions: 3

Compression:

Stored size: 874 Bytes

Contents

# frozen_string_literal: true

module IdentityCache
  module Cached
    class AttributeByMulti < Attribute
      def build
        cached_attribute = self

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

      private

      # Attribute method overrides

      def cast_db_key(key_values)
        field_types.each_with_index do |type, i|
          key_values[i] = type.cast(key_values[i])
        end
        key_values
      end

      def unhashed_values_cache_key_string(key_values)
        key_values.map { |v| v.try!(:to_s).inspect }.join("/")
      end

      def load_from_db_where_conditions(key_values)
        Hash[key_fields.zip(key_values)]
      end

      alias_method :cache_key_from_key_values, :cache_key
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
identity_cache-1.3.1 lib/identity_cache/cached/attribute_by_multi.rb
identity_cache-1.3.0 lib/identity_cache/cached/attribute_by_multi.rb
identity_cache-1.2.0 lib/identity_cache/cached/attribute_by_multi.rb