module Elasticsearch::Persistence::Repository::Naming
Wraps all naming-related features of the repository (index name, the domain object class, etc)
Public Instance Methods
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 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 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 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
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
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
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
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
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
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