Class: Esse::LazyDocumentHeader

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

Constant Summary collapse

ACCEPTABLE_CLASSES =
[Esse::LazyDocumentHeader, Esse::Document].freeze
ACCEPTABLE_DOC_TYPES =
[nil, '_doc', 'doc'].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, type: nil, routing: nil, **extra_attributes) ⇒ LazyDocumentHeader

Returns a new instance of LazyDocumentHeader.



51
52
53
54
55
56
# File 'lib/esse/lazy_document_header.rb', line 51

def initialize(id:, type: nil, routing: nil, **extra_attributes)
  @id = id
  @type = type
  @routing = routing
  @options = extra_attributes.freeze
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#routingObject (readonly)

Returns the value of attribute routing.



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

def routing
  @routing
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.coerce(value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/esse/lazy_document_header.rb', line 22

def self.coerce(value)
  return unless value

  if value.is_a?(Esse::LazyDocumentHeader)
    value
  elsif value.is_a?(Esse::Document)
    value
  elsif value.is_a?(Hash)
    resp = value.transform_keys do |key|
      case key
      when :_id, :id, '_id', 'id'
        :id
      when :_routing, :routing, '_routing', 'routing'
        :routing
      when :_type, :type, '_type', 'type'
        :type
      else
        key.to_sym
      end
    end
    resp[:id] ||= nil
    new(**resp)
  elsif String === value || Integer === value
    new(id: value)
  end
end

.coerce_each(values) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/esse/lazy_document_header.rb', line 8

def self.coerce_each(values)
  values = Esse::ArrayUtils.wrap(values)
  return values if values.all? do |value|
    ACCEPTABLE_CLASSES.any? { |klass| value.is_a?(klass) }
  end

  arr = []
  values.flatten.map do |value|
    instance = coerce(value)
    arr << instance if instance && !instance.id.nil?
  end
  arr
end

Instance Method Details

#doc_headerObject



69
70
71
72
73
74
# File 'lib/esse/lazy_document_header.rb', line 69

def doc_header
  { _id: id }.tap do |hash|
    hash[:_type] = type if type
    hash[:routing] = routing if routing
  end
end

#document_for_partial_update(source) ⇒ Object



65
66
67
# File 'lib/esse/lazy_document_header.rb', line 65

def document_for_partial_update(source)
  Esse::DocumentForPartialUpdate.new(self, source: source)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


76
77
78
79
80
81
# File 'lib/esse/lazy_document_header.rb', line 76

def eql?(other, **)
  ACCEPTABLE_CLASSES.any? { |klass| other.is_a?(klass) } &&
    id.to_s == other.id.to_s &&
    routing == other.routing &&
    ((ACCEPTABLE_DOC_TYPES.include?(type) && ACCEPTABLE_DOC_TYPES.include?(other.type)) || type == other.type)
end

#to_hObject



58
59
60
61
62
63
# File 'lib/esse/lazy_document_header.rb', line 58

def to_h
  options.merge(_id: id).tap do |hash|
    hash[:_type] = type if type
    hash[:routing] = routing if routing
  end
end