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.



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

def initialize(object, **options)
  @object = object
  @options = options.freeze
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



23
24
25
# File 'lib/esse/document.rb', line 23

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/esse/document.rb', line 23

def options
  @options
end

Instance Method Details

#doc_headerObject



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

Returns:

  • (Boolean)


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 && 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)


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

Returns:

  • (Boolean)


96
97
98
# File 'lib/esse/document.rb', line 96

def ignore_on_delete?
  id.nil?
end

#ignore_on_index?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/esse/document.rb', line 92

def ignore_on_index?
  id.nil?
end

#metaHash

This method is abstract.

Override this method to return the document meta

Returns the document meta.

Returns:

  • (Hash)

    the document meta



60
61
62
# File 'lib/esse/document.rb', line 60

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



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

#mutationsObject



140
141
142
# File 'lib/esse/document.rb', line 140

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



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

def routing
  nil
end

#routing?Boolean

Returns whether the document has routing.

Returns:

  • (Boolean)

    whether the document has routing



54
55
56
# File 'lib/esse/document.rb', line 54

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



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

#to_hHash

Returns the document data.

Returns:

  • (Hash)

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

#to_sObject



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

#typeString?

This method is abstract.

Override this method to return the document type

Returns the document type.

Returns:

  • (String, nil)

    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.

Returns:

  • (Boolean)

    whether the document has type



43
44
45
# File 'lib/esse/document.rb', line 43

def type?
  !type.nil?
end