Class: Esse::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/esse/document.rb

Constant Summary collapse

MUTATIONS_FALLBACK =
{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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, **options)
  @object = object
  @options = options.freeze
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



7
8
9
# File 'lib/esse/document.rb', line 7

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/esse/document.rb', line 7

def options
  @options
end

Instance Method Details

#doc_headerObject



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: ==

Returns:

  • (Boolean)


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 && meta == other.meta
    )
  end
end

#idString, Number

This method is abstract.

Override this method to return the document ID

Returns the document ID.

Returns:

  • (String, Number)

    the document ID

Raises:

  • (NotImplementedError)


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

Returns:

  • (Boolean)


85
86
87
# File 'lib/esse/document.rb', line 85

def ignore_on_delete?
  id.nil?
end

#ignore_on_index?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/esse/document.rb', line 81

def ignore_on_index?
  id.nil?
end

#inspectObject



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

#metaHash

This method is abstract.

Override this method to return the document meta

Returns the document meta.

Returns:

  • (Hash)

    the document meta



49
50
51
# File 'lib/esse/document.rb', line 49

def meta
  {}
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_sourceObject



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

#mutationsObject



129
130
131
# File 'lib/esse/document.rb', line 129

def mutations
  @__mutations__ || MUTATIONS_FALLBACK
end

#routingString?

This method is abstract.

Override this method to return the document routing

Returns the document routing.

Returns:

  • (String, nil)

    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.

Returns:

  • (Boolean)

    whether the document has routing



38
39
40
# File 'lib/esse/document.rb', line 38

def routing?
  !routing.nil?
end

#sourceHash

This method is abstract.

Override this method to return the document source

Returns the document source.

Returns:

  • (Hash)

    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!(meta)
  end
end

#to_hHash

Returns the document data.

Returns:

  • (Hash)

    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!(meta)
  end
end

#typeString?

This method is abstract.

Override this method to return the document type

Returns the document type.

Returns:

  • (String, nil)

    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.

Returns:

  • (Boolean)

    whether the document has type



27
28
29
# File 'lib/esse/document.rb', line 27

def type?
  !type.nil?
end