Class: Esse::Document
- Inherits:
-
Object
- Object
- Esse::Document
- Defined in:
- lib/esse/document.rb
Direct Known Subclasses
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
- #==(other) ⇒ Object
- #doc_header ⇒ Object
-
#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
-
#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.
7 8 9 10 |
# File 'lib/esse/document.rb', line 7 def initialize(object, **) @object = object @options = end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
5 6 7 |
# File 'lib/esse/document.rb', line 5 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/esse/document.rb', line 5 def @options end |
Instance Method Details
#==(other) ⇒ Object
87 88 89 90 91 |
# File 'lib/esse/document.rb', line 87 def ==(other) other.is_a?(self.class) && ( id == other.id && type == other.type && routing == other.routing && == other. && source == other.source ) end |
#doc_header ⇒ Object
93 94 95 96 97 98 |
# File 'lib/esse/document.rb', line 93 def doc_header { _id: id }.tap do |h| h[:_type] = type if type h[:routing] = routing if routing? end end |
#id ⇒ String, Number
Override this method to return the document ID
Returns the document ID.
14 15 16 |
# File 'lib/esse/document.rb', line 14 def id raise NotImplementedError, 'Override this method to return the document ID' end |
#ignore_on_delete? ⇒ Boolean
83 84 85 |
# File 'lib/esse/document.rb', line 83 def ignore_on_delete? id.nil? end |
#ignore_on_index? ⇒ Boolean
79 80 81 |
# File 'lib/esse/document.rb', line 79 def ignore_on_index? id.nil? end |
#inspect ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/esse/document.rb', line 100 def inspect attributes = %i[id routing source].map do |attr| value = send(attr) next unless value "#{attr}: #{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.
47 48 49 |
# File 'lib/esse/document.rb', line 47 def {} end |
#mutate(key) ⇒ Object
112 113 114 115 116 |
# File 'lib/esse/document.rb', line 112 def mutate(key) @__mutations__ ||= {} @__mutations__[key] = yield instance_variable_set(:@__mutated_source__, nil) end |
#routing ⇒ String?
Override this method to return the document routing
Returns the document routing.
31 32 33 |
# File 'lib/esse/document.rb', line 31 def routing nil end |
#routing? ⇒ Boolean
Returns whether the document has routing.
36 37 38 |
# File 'lib/esse/document.rb', line 36 def routing? !routing.nil? end |
#source ⇒ Hash
Override this method to return the document source
Returns the document source.
53 54 55 |
# File 'lib/esse/document.rb', line 53 def source {} end |
#to_bulk(data: true, operation: nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/esse/document.rb', line 68 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.
58 59 60 61 62 63 64 65 66 |
# File 'lib/esse/document.rb', line 58 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.
20 21 22 |
# File 'lib/esse/document.rb', line 20 def type nil end |
#type? ⇒ Boolean
Returns whether the document has type.
25 26 27 |
# File 'lib/esse/document.rb', line 25 def type? !type.nil? end |