Module: Elasticsearch::Persistence::Repository::Naming

Included in:
Class
Defined in:
lib/elasticsearch/persistence/repository/naming.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#__extract_id_from_document(document) ⇒ Object

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.

Note:

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

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

Examples:

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


108
109
110
# 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) ⇒ Object

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.

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

Examples:

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


91
92
93
# 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) ⇒ Class

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.

Get the Ruby class from the Elasticsearch ‘_type`

Examples:

repository.__get_klass_from_type 'note'
=> Note

Returns:

  • (Class)

    The class corresponding to the passed type

Raises:

  • (NameError)

    if the class cannot be found



62
63
64
65
66
67
# 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) ⇒ String

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.

Get the Elasticsearch ‘_type` from the Ruby class

Examples:

repository.__get_type_from_class Note
=> "note"

Returns:

  • (String)

    The type corresponding to the passed class



79
80
81
# File 'lib/elasticsearch/persistence/repository/naming.rb', line 79

def __get_type_from_class(klass)
  klass.to_s.underscore
end

#document_type(name = nil) ⇒ Object Also known as: type

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



41
42
43
# 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

#document_type=(name) ⇒ Object Also known as: type=

Set the document type used when storing and retrieving documents



47
48
49
# File 'lib/elasticsearch/persistence/repository/naming.rb', line 47

def document_type=(name)
  @document_type = name
end

#index_name(name = nil) ⇒ Object Also known as: index

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



23
24
25
26
27
28
29
30
31
# 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

#index_name=(name) ⇒ Object Also known as: index=

Set the index name used when storing and retrieving documents



35
36
37
# File 'lib/elasticsearch/persistence/repository/naming.rb', line 35

def index_name=(name)
  @index_name = name
end

#klass(name = nil) ⇒ Object

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



11
12
13
# File 'lib/elasticsearch/persistence/repository/naming.rb', line 11

def klass name=nil
  @klass = name || @klass
end

#klass=(klass) ⇒ Object

Set the class used to initialize domain objects when deserializing them



17
18
19
# File 'lib/elasticsearch/persistence/repository/naming.rb', line 17

def klass=klass
  @klass = klass
end