Class: Elasticsearch::Model::Adapter::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/model/adapter.rb

Overview

Contains an adapter for specific OxM or architecture.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Adapter

Returns a new instance of Adapter.



71
72
73
# File 'lib/elasticsearch/model/adapter.rb', line 71

def initialize(klass)
  @klass = klass
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



69
70
71
# File 'lib/elasticsearch/model/adapter.rb', line 69

def klass
  @klass
end

Class Method Details

.adaptersHash

Return the collection of registered adapters

Examples:

Return the currently registered adapters


Elasticsearch::Model::Adapter.adapters
# => {
#  Elasticsearch::Model::Adapter::ActiveRecord => #<Proc:0x007...(lambda)>,
#  Elasticsearch::Model::Adapter::Mongoid => #<Proc:0x007... (lambda)>,
# }

Returns:

  • (Hash)

    The collection of adapters



120
121
122
# File 'lib/elasticsearch/model/adapter.rb', line 120

def self.adapters
  @adapters ||= {}
end

.register(name, condition) ⇒ Object

Registers an adapter for specific condition

Examples:

Register an adapter for DataMapper


module DataMapperAdapter

  # Implement the interface for fetching records
  #
  module Records
    def records
      klass.all(id: @ids)
    end

    # ...
  end
end

# Register the adapter
#
Elasticsearch::Model::Adapter.register(
  DataMapperAdapter,
  lambda { |klass|
    defined?(::DataMapper::Resource) and klass.ancestors.include?(::DataMapper::Resource)
  }
)

Parameters:

  • name (Module)

    The module containing the implemented interface

  • condition (Proc)

    An object with a ‘call` method which is evaluated in #adapter



104
105
106
# File 'lib/elasticsearch/model/adapter.rb', line 104

def self.register(name, condition)
  self.adapters[name] = condition
end

Instance Method Details

#adapterObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the adapter module



152
153
154
155
156
157
# File 'lib/elasticsearch/model/adapter.rb', line 152

def adapter
  @adapter ||= begin
    self.class.adapters.find( lambda {[]} ) { |name, condition| condition.call(klass) }.first \
    || Elasticsearch::Model::Adapter::Default
  end
end

#callbacks_mixinObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the module with Default::Callbacks interface implementation



136
137
138
# File 'lib/elasticsearch/model/adapter.rb', line 136

def callbacks_mixin
  adapter.const_get(:Callbacks)
end

#importing_mixinObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the module with Default::Importing interface implementation



144
145
146
# File 'lib/elasticsearch/model/adapter.rb', line 144

def importing_mixin
  adapter.const_get(:Importing)
end

#records_mixinObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the module with Default::Records interface implementation



128
129
130
# File 'lib/elasticsearch/model/adapter.rb', line 128

def records_mixin
  adapter.const_get(:Records)
end