Sha256: 370a660be36b30bd82dc05db834a14fe289e933602e2c410126dd8444f821742

Contents?: true

Size: 1.87 KB

Versions: 6

Compression:

Stored size: 1.87 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

    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

6 entries across 6 versions & 1 rubygems

Version Path
es-elasticity-0.13.4 lib/elasticity/base_document.rb
es-elasticity-0.13.4.zpesic lib/elasticity/base_document.rb
es-elasticity-0.13.3 lib/elasticity/base_document.rb
es-elasticity-0.13.3.pre1 lib/elasticity/base_document.rb
es-elasticity-0.12.0 lib/elasticity/base_document.rb
es-elasticity-0.11.5 lib/elasticity/base_document.rb