require 'setback/base'
require 'setback/memory_provider'

module Setback
  module MemoryBacked
    include Setback::Base

    def initialize(options = {})
      if options[:cache?]
        Setback::MEMORY_BACKED_INSTANCES[self.class.name] = self
      end
    end

    def provider
      @provider ||= Setback::MemoryProvider.new
    end

    def backed_by
      :memory
    end

    def self.included(base)
      class << base
        include ClassMethods
      end
    end

    module ClassMethods
      def lookup
        Setback::MEMORY_BACKED_INSTANCES[self.name]
      end
    end
  end

  MEMORY_BACKED_INSTANCES = {}
end
