module Elasticsearch::Persistence::Repository::Naming

Wraps all naming-related features of the repository (index name, the domain object class, etc)

Public Instance Methods

__extract_id_from_document(document) click to toggle source

Extract a document ID from the document (assuming Hash or Hash-like object)

@note Calling this method will remove the ‘id` or `_id` key from the passed object.

@example

options = { title: 'Test', id: 'abc123' }
repository.__extract_id_from_document options
# => "abc123"
options
# => { title: 'Test' }

@api private

# File lib/elasticsearch/persistence/repository/naming.rb, line 108
def __extract_id_from_document(document)
  document.delete(:id) || document.delete('id') || document.delete(:_id) || document.delete('_id')
end
__get_id_from_document(document) click to toggle source

Get a document ID from the document (assuming Hash or Hash-like object)

@example

repository.__get_id_from_document title: 'Test', id: 'abc123'
=> "abc123"

@api private

# File lib/elasticsearch/persistence/repository/naming.rb, line 91
def __get_id_from_document(document)
  document[:id] || document['id'] || document[:_id] || document['_id']
end
__get_klass_from_type(type) click to toggle source

Get the Ruby class from the Elasticsearch ‘_type`

@example

repository.__get_klass_from_type 'note'
=> Note

@return [Class] The class corresponding to the passed type @raise [NameError] if the class cannot be found

@api private

# File lib/elasticsearch/persistence/repository/naming.rb, line 62
def __get_klass_from_type(type)
  klass = type.classify
  klass.constantize
rescue NameError => e
  raise NameError, "Attempted to get class '#{klass}' from the '#{type}' type, but no such class can be found."
end
__get_type_from_class(klass) click to toggle source

Get the Elasticsearch ‘_type` from the Ruby class

@example

repository.__get_type_from_class Note
=> "note"

@return [String] The type corresponding to the passed class

@api private

# File lib/elasticsearch/persistence/repository/naming.rb, line 79
def __get_type_from_class(klass)
  klass.to_s.underscore
end
document_type(name=nil) click to toggle source

Get or set the document type used when storing and retrieving documents

# File lib/elasticsearch/persistence/repository/naming.rb, line 41
def document_type name=nil
  @document_type = name || @document_type || (klass ? klass.to_s.underscore : nil)
end
Also aliased as: type
document_type=(name) click to toggle source

Set the document type used when storing and retrieving documents

# File lib/elasticsearch/persistence/repository/naming.rb, line 47
def document_type=(name)
  @document_type = name
end
Also aliased as: type=
index(name=nil)
Alias for: index_name
index=(name)
Alias for: index_name=
index_name(name=nil) click to toggle source

Get or set the index name used when storing and retrieving documents

# File lib/elasticsearch/persistence/repository/naming.rb, line 23
def index_name name=nil
  @index_name = name || @index_name || begin
    if respond_to?(:host) && host && host.is_a?(Module)
      self.host.to_s.underscore.gsub(/\//, '-')
    else
      self.class.to_s.underscore.gsub(/\//, '-')
    end
  end
end
Also aliased as: index
index_name=(name) click to toggle source

Set the index name used when storing and retrieving documents

# File lib/elasticsearch/persistence/repository/naming.rb, line 35
def index_name=(name)
  @index_name = name
end
Also aliased as: index=
klass(name=nil) click to toggle source

Get or set the class used to initialize domain objects when deserializing them

# File lib/elasticsearch/persistence/repository/naming.rb, line 11
def klass name=nil
  @klass = name || @klass
end
klass=(klass) click to toggle source

Set the class used to initialize domain objects when deserializing them

# File lib/elasticsearch/persistence/repository/naming.rb, line 17
def klass=klass
  @klass = klass
end
type(name=nil)
Alias for: document_type
type=(name)
Alias for: document_type=