Sha256: df3de6f7b48aaa9e56bac1db567a6098e3b5ad8cb66cb00f8b208749e5af6ac2

Contents?: true

Size: 1.88 KB

Versions: 8

Compression:

Stored size: 1.88 KB

Contents

module Elasticity
  class BaseDocument
    include ::ActiveModel::Model

    # Stores configuration for this class and all subclasses.
    class_attribute :config

    # Configure the given klass, changing default parameters and resetting
    # some of the internal state.
    def self.configure(&block)
      self.config = IndexConfig.new(Elasticity.config, self.index_config_defaults, &block)
    end

    # Define common attributes for all documents
    attr_accessor :_id, :highlighted, :_score, :sort, :_explanation, :highlighted_attrs, :matched_queries

    def attributes=(attributes)
      attributes.each do |attr, value|
        self.public_send("#{attr}=", value)
      end
    end

    # Defines equality by comparing the ID and values of each instance variable.
    def ==(other)
      return false if other.nil?
      return false if _id != other._id

      instance_variables.all? do |ivar|
        instance_variable_get(ivar) == other.instance_variable_get(ivar)
      end
    end

    # IMPLEMENT
    # Returns a hash with the attributes as they should be stored in the index.
    # This will be stored as _source attributes on Elasticsearch.
    def to_document
      raise NotImplementedError, "to_document needs to be implemented for #{self.class}"
    end

    # Update this object on the index, creating or updating the document.
    def update
      self._id, @created = self.class.index_document(_id, to_document)
    end

    def delete
      self.class.delete(self._id)
    end

    def created?
      @created || false
    end

    private

    def self.index_config_defaults
      {
        document_type: default_document_type,
        index_base_name: default_index_base_name
      }
    end

    def self.default_document_type
      self.name.gsub('::', '_').underscore
    end

    def self.default_index_base_name
      ActiveSupport::Inflector.pluralize(default_document_type)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
es-elasticity-0.14.1 lib/elasticity/base_document.rb
es-elasticity-0.14.0 lib/elasticity/base_document.rb
es-elasticity-0.14.0.jhumphreys lib/elasticity/base_document.rb
es-elasticity-0.13.5 lib/elasticity/base_document.rb
es-elasticity-0.13.5.dblackmon.1 lib/elasticity/base_document.rb
es-elasticity-0.13.4.dblackmon.1 lib/elasticity/base_document.rb
es-elasticity-0.13.4.dblackmon.0 lib/elasticity/base_document.rb
es-elasticity-0.13.4.dblackmon lib/elasticity/base_document.rb