Class: Esse::Document
- Inherits:
-
Object
- Object
- Esse::Document
- Defined in:
- lib/esse/document.rb
Direct Known Subclasses
DocumentForPartialUpdate, HashDocument, NullDocument, Serializer
Constant Summary collapse
- MUTATIONS_FALLBACK =
{}.freeze
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #doc_header ⇒ Object
- #document_for_partial_update(source) ⇒ Object
- #eql?(other, match_lazy_doc_header: false) ⇒ Boolean (also: #==)
-
#id ⇒ String, Number
abstract
The document ID.
- #ignore_on_delete? ⇒ Boolean
- #ignore_on_index? ⇒ Boolean
-
#initialize(object, **options) ⇒ Document
constructor
A new instance of Document.
- #inspect ⇒ Object
-
#meta ⇒ Hash
abstract
The document meta.
- #mutate(key) ⇒ Object
- #mutated_source ⇒ Object
- #mutations ⇒ Object
-
#routing ⇒ String?
abstract
The document routing.
-
#routing? ⇒ Boolean
Whether the document has routing.
-
#source ⇒ Hash
abstract
The document source.
- #to_bulk(data: true, operation: nil) ⇒ Object
-
#to_h ⇒ Hash
The document data.
-
#type ⇒ String?
abstract
The document type.
-
#type? ⇒ Boolean
Whether the document has type.
Constructor Details
#initialize(object, **options) ⇒ Document
Returns a new instance of Document.
9 10 11 12 |
# File 'lib/esse/document.rb', line 9 def initialize(object, **) @object = object @options = .freeze end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
7 8 9 |
# File 'lib/esse/document.rb', line 7 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/esse/document.rb', line 7 def @options end |
Instance Method Details
#doc_header ⇒ Object
100 101 102 103 104 105 |
# File 'lib/esse/document.rb', line 100 def doc_header { _id: id }.tap do |h| h[:_type] = type if type h[:routing] = routing if routing? end end |
#document_for_partial_update(source) ⇒ Object
107 108 109 |
# File 'lib/esse/document.rb', line 107 def document_for_partial_update(source) DocumentForPartialUpdate.new(self, source: source) end |
#eql?(other, match_lazy_doc_header: false) ⇒ Boolean Also known as: ==
89 90 91 92 93 94 95 96 97 |
# File 'lib/esse/document.rb', line 89 def eql?(other, match_lazy_doc_header: false) if match_lazy_doc_header other.eql?(self) else other.is_a?(Esse::Document) && ( id.to_s == other.id.to_s && type == other.type && routing == other.routing && == other. ) end end |
#id ⇒ String, Number
Override this method to return the document ID
Returns the document ID.
16 17 18 |
# File 'lib/esse/document.rb', line 16 def id raise NotImplementedError, 'Override this method to return the document ID' end |
#ignore_on_delete? ⇒ Boolean
85 86 87 |
# File 'lib/esse/document.rb', line 85 def ignore_on_delete? id.nil? end |
#ignore_on_index? ⇒ Boolean
81 82 83 |
# File 'lib/esse/document.rb', line 81 def ignore_on_index? id.nil? end |
#inspect ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/esse/document.rb', line 111 def inspect attributes = {id: :id, routing: :routing, source: :memoized_source}.map do |attr_name, attr_src| value = send(attr_src) next unless value "#{attr_name}: #{value.inspect}" rescue nil end.compact.join(', ') attributes << " mutations: #{@__mutations__.inspect}" if @__mutations__ "#<#{self.class.name || 'Esse::Document'} #{attributes}>" end |
#meta ⇒ Hash
Override this method to return the document meta
Returns the document meta.
49 50 51 |
# File 'lib/esse/document.rb', line 49 def {} end |
#mutate(key) ⇒ Object
123 124 125 126 127 |
# File 'lib/esse/document.rb', line 123 def mutate(key) @__mutations__ ||= {} @__mutations__[key] = yield instance_variable_set(:@__mutated_source__, nil) end |
#mutated_source ⇒ Object
133 134 135 136 137 |
# File 'lib/esse/document.rb', line 133 def mutated_source return memoized_source unless @__mutations__ @__mutated_source__ ||= memoized_source.merge(@__mutations__) end |
#mutations ⇒ Object
129 130 131 |
# File 'lib/esse/document.rb', line 129 def mutations @__mutations__ || MUTATIONS_FALLBACK end |
#routing ⇒ String?
Override this method to return the document routing
Returns the document routing.
33 34 35 |
# File 'lib/esse/document.rb', line 33 def routing nil end |
#routing? ⇒ Boolean
Returns whether the document has routing.
38 39 40 |
# File 'lib/esse/document.rb', line 38 def routing? !routing.nil? end |
#source ⇒ Hash
Override this method to return the document source
Returns the document source.
55 56 57 |
# File 'lib/esse/document.rb', line 55 def source {} end |
#to_bulk(data: true, operation: nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/esse/document.rb', line 70 def to_bulk(data: true, operation: nil) doc_header.tap do |h| if data && operation == :update h[:data] = { doc: mutated_source } elsif data h[:data] = mutated_source end h.merge!() end end |
#to_h ⇒ Hash
Returns the document data.
60 61 62 63 64 65 66 67 68 |
# File 'lib/esse/document.rb', line 60 def to_h mutated_source.merge( _id: id, ).tap do |hash| hash[:_type] = type if type hash[:_routing] = routing if routing hash.merge!() end end |
#type ⇒ String?
Override this method to return the document type
Returns the document type.
22 23 24 |
# File 'lib/esse/document.rb', line 22 def type nil end |
#type? ⇒ Boolean
Returns whether the document has type.
27 28 29 |
# File 'lib/esse/document.rb', line 27 def type? !type.nil? end |