Sha256: 743b1fbc6fcb23025b50a2a0b88b8cabd3e803bd29581c21d2c5b68d47bd0a79

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

module Toy
  module Querying
    extend ActiveSupport::Concern

    module ClassMethods
      def get(id)
        key = store_key(id)

        if has_cache?
          value = cache.read(key)
          log_operation('RTG', cache, key, value)
        end

        if value.nil?
          value = store.read(key)
          log_operation('GET', store, key, value)

          if has_cache?
            cache.write(key, value)
            log_operation('RTS', cache, key, value)
          end
        end

        load(value)
      end

      def get!(id)
        get(id) || raise(Toy::NotFound.new(id))
      end

      def get_multi(*ids)
        ids.flatten.map { |id| get(id) }
      end

      def get_or_new(id)
        get(id) || new(:id => id)
      end

      def get_or_create(id)
        get(id) || create(:id => id)
      end

      def key?(id)
        key = store_key(id)
        value = store.key?(key)
        log_operation('KEY', store, key, value)
        value
      end
      alias :has_key? :key?

      def load(attrs)
        return nil if attrs.nil?
        allocate.initialize_from_database(attrs)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
toystore-0.6.1 lib/toy/querying.rb
toystore-0.6 lib/toy/querying.rb
toystore-0.5 lib/toy/querying.rb