Sha256: 4254cd9a9c064ec2696b5c68aa35dd958c314a4823f0fef50966d33e619cf364

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require 'forwardable'

module Dolly
  class MangoIndex
    class << self
      extend Forwardable

      ALL_DOCS = '_all_docs'
      DESIGN = '_index'
      ROWS_KEY = :rows
      DESIGN_PREFIX = '_design/'

      def_delegators :connection, :get, :post

      def all
        get(DESIGN)[:indexes]
      end

      def create(name, fields, type = 'json')
        post(DESIGN, build_index_structure(name, fields, type))
      end

      def create_in_database(database, name, fields, type = 'json')
        connection_for_database(database).post(DESIGN, build_index_structure(name, fields, type))
      end

      def find_by_fields(fields)
        rows = get(ALL_DOCS, key: key_from_fields(fields))[ROWS_KEY]
        (rows && rows.any?)
      end

      def delete_all
        all.each do |index_doc|
          next if index_doc[:ddoc].nil?
          delete(index_doc)
        end
      end

      def delete(index_doc)
        resource = "#{DESIGN}/#{index_doc[:ddoc]}/json/#{index_doc[:name]}"
        connection.delete(resource, escape: false)
      end

      private

      def connection_for_database(database)
        Dolly::Connection.new(database.to_sym, Rails.env || :development)
      end

      def connection
        @connection ||= Dolly::Document.connection
      end

      def build_index_structure(name, fields, type)
        {
          ddoc: key_from_fields(fields).gsub(DESIGN_PREFIX, ''),
          index: {
            fields: fields
          },
          name: name,
          type: type
        }
      end

      def key_from_fields(fields)
        "#{DESIGN_PREFIX}index_#{fields.join('_')}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dolly-3.1.4 lib/dolly/mango_index.rb
dolly-3.1.3 lib/dolly/mango_index.rb