Class: Esse::Document

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

Direct Known Subclasses

HashDocument, NullDocument, Serializer

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/esse/document.rb', line 5

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/esse/document.rb', line 5

def options
  @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 && meta == other.meta && source == other.source
  )
end

#doc_headerObject



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

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


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

Returns:

  • (Boolean)


83
84
85
# File 'lib/esse/document.rb', line 83

def ignore_on_delete?
  id.nil?
end

#ignore_on_index?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/esse/document.rb', line 79

def ignore_on_index?
  id.nil?
end

#inspectObject



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

#metaHash

This method is abstract.

Override this method to return the document meta

Returns the document meta.

Returns:

  • (Hash)

    the document meta



47
48
49
# File 'lib/esse/document.rb', line 47

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

#routingString?

This method is abstract.

Override this method to return the document routing

Returns the document routing.

Returns:

  • (String, nil)

    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.

Returns:

  • (Boolean)

    whether the document has routing



36
37
38
# File 'lib/esse/document.rb', line 36

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



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

#to_hHash

Returns the document data.

Returns:

  • (Hash)

    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!(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



20
21
22
# File 'lib/esse/document.rb', line 20

def type
  nil
end

#type?Boolean

Returns whether the document has type.

Returns:

  • (Boolean)

    whether the document has type



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

def type?
  !type.nil?
end