module Qwaiter
  module Couchbase
    def self.connection
      $cb
    end

    def self.load_design_docs!
      return unless connection.present?
      Dir.glob(Rails.root.join('config/couchbase/design_docs', "*.json")).each do |design_doc|
        connection.save_design_doc File.open(design_doc)
      end
    end

    def self.design_doc(name)
      return unless connection.present?
      connection.design_docs[name]
    end

    def self.flush_counters!
      return unless connection.present?
      design_doc('supplier').counters(reduce: false).each{|counter| Qwaiter::Counter.set counter.key, 0}
    end
  end
end
