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.
-
#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.
- #to_s ⇒ Object
-
#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.
25 26 27 28 |
# File 'lib/esse/document.rb', line 25 def initialize(object, **) @object = object @options = .freeze end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
23 24 25 |
# File 'lib/esse/document.rb', line 23 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
23 24 25 |
# File 'lib/esse/document.rb', line 23 def @options end |
Instance Method Details
#doc_header ⇒ Object
111 112 113 114 115 116 |
# File 'lib/esse/document.rb', line 111 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
118 119 120 |
# File 'lib/esse/document.rb', line 118 def document_for_partial_update(source) DocumentForPartialUpdate.new(self, source: source) end |
#eql?(other, match_lazy_doc_header: false) ⇒ Boolean Also known as: ==
100 101 102 103 104 105 106 107 108 |
# File 'lib/esse/document.rb', line 100 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.
32 33 34 |
# File 'lib/esse/document.rb', line 32 def id raise NotImplementedError, 'Override this method to return the document ID' end |
#ignore_on_delete? ⇒ Boolean
96 97 98 |
# File 'lib/esse/document.rb', line 96 def ignore_on_delete? id.nil? end |
#ignore_on_index? ⇒ Boolean
92 93 94 |
# File 'lib/esse/document.rb', line 92 def ignore_on_index? id.nil? end |
#meta ⇒ Hash
Override this method to return the document meta
Returns the document meta.
60 61 62 |
# File 'lib/esse/document.rb', line 60 def {} end |
#mutate(key) ⇒ Object
134 135 136 137 138 |
# File 'lib/esse/document.rb', line 134 def mutate(key) @__mutations__ ||= {} @__mutations__[key] = yield instance_variable_set(:@__mutated_source__, nil) end |
#mutated_source ⇒ Object
144 145 146 147 148 |
# File 'lib/esse/document.rb', line 144 def mutated_source return memoized_source unless @__mutations__ @__mutated_source__ ||= memoized_source.merge(@__mutations__) end |
#mutations ⇒ Object
140 141 142 |
# File 'lib/esse/document.rb', line 140 def mutations @__mutations__ || MUTATIONS_FALLBACK end |
#routing ⇒ String?
Override this method to return the document routing
Returns the document routing.
49 50 51 |
# File 'lib/esse/document.rb', line 49 def routing nil end |
#routing? ⇒ Boolean
Returns whether the document has routing.
54 55 56 |
# File 'lib/esse/document.rb', line 54 def routing? !routing.nil? end |
#source ⇒ Hash
Override this method to return the document source
Returns the document source.
66 67 68 |
# File 'lib/esse/document.rb', line 66 def source {} end |
#to_bulk(data: true, operation: nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/esse/document.rb', line 81 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.
71 72 73 74 75 76 77 78 79 |
# File 'lib/esse/document.rb', line 71 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 |
#to_s ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/esse/document.rb', line 122 def to_s 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 |
#type ⇒ String?
Override this method to return the document type
Returns the document type.
38 39 40 |
# File 'lib/esse/document.rb', line 38 def type nil end |
#type? ⇒ Boolean
Returns whether the document has type.
43 44 45 |
# File 'lib/esse/document.rb', line 43 def type? !type.nil? end |