Sha256: 7e859b57c6247501929d2a279daebdd76b1fc94e94b951d5b2385ebf7ccacc6d

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 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', self, cache, key, value)
        end

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

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

        load(key, 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', self, store, key, value)
        value
      end
      alias :has_key? :key?

      def load(key, attrs)
        attrs && allocate.initialize_from_database(attrs.update('id' => key))
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
toystore-0.6.6 lib/toy/querying.rb
toystore-0.6.5 lib/toy/querying.rb
toystore-0.6.4 lib/toy/querying.rb