module Elasticsearch::Model::Proxy

This module provides a proxy interfacing between the including class and {Elasticsearch::Model}, preventing the pollution of the including class namespace.

The only “gateway” between the model and Elasticsearch::Model is the ‘__elasticsearch__` class and instance method.

The including class must be compatible with [ActiveModel](github.com/rails/rails/tree/master/activemodel).

@example Include the {Elasticsearch::Model} module into an ‘Article` model

class Article < ActiveRecord::Base
  include Elasticsearch::Model
end

Article.__elasticsearch__.respond_to?(:search)
# => true

article = Article.first

article.respond_to? :index_document
# => false

article.__elasticsearch__.respond_to?(:index_document)
# => true

Public Class Methods

__elasticsearch__(&block) click to toggle source

{ClassMethodsProxy} instance, accessed as ‘MyModel.__elasticsearch__`

# File lib/elasticsearch/model/proxy.rb, line 42
def self.__elasticsearch__ &block
  @__elasticsearch__ ||= ClassMethodsProxy.new(self)
  @__elasticsearch__.instance_eval(&block) if block_given?
  @__elasticsearch__
end
included(base) click to toggle source

Define the ‘__elasticsearch__` class and instance methods in the including class and register a callback for intercepting changes in the model.

@note The callback is triggered only when ‘Elasticsearch::Model` is included in the

module and the functionality is accessible via the proxy.
# File lib/elasticsearch/model/proxy.rb, line 38
def self.included(base)
  base.class_eval do
    # {ClassMethodsProxy} instance, accessed as `MyModel.__elasticsearch__`
    #
    def self.__elasticsearch__ &block
      @__elasticsearch__ ||= ClassMethodsProxy.new(self)
      @__elasticsearch__.instance_eval(&block) if block_given?
      @__elasticsearch__
    end

    # {InstanceMethodsProxy}, accessed as `@mymodel.__elasticsearch__`
    #
    def __elasticsearch__ &block
      @__elasticsearch__ ||= InstanceMethodsProxy.new(self)
      @__elasticsearch__.instance_eval(&block) if block_given?
      @__elasticsearch__
    end

    # Register a callback for storing changed attributes for models which implement
    # `before_save` and `changed_attributes` methods (when `Elasticsearch::Model` is included)
    #
    # @see http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
    #
    before_save do |i|
      i.__elasticsearch__.instance_variable_set(:@__changed_attributes,
                                                Hash[ i.changes.map { |key, value| [key, value.last] } ])
    end if respond_to?(:before_save) && instance_methods.include?(:changed_attributes)
  end
end

Public Instance Methods

__elasticsearch__(&block) click to toggle source

{InstanceMethodsProxy}, accessed as ‘@mymodel.__elasticsearch__`

# File lib/elasticsearch/model/proxy.rb, line 50
def __elasticsearch__ &block
  @__elasticsearch__ ||= InstanceMethodsProxy.new(self)
  @__elasticsearch__.instance_eval(&block) if block_given?
  @__elasticsearch__
end