Class: Esse::LazyDocumentHeader

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ LazyDocumentHeader

Returns a new instance of LazyDocumentHeader.



40
41
42
# File 'lib/esse/lazy_document_header.rb', line 40

def initialize(attributes)
  @attributes = attributes
end

Class Method Details

.coerce(value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/esse/lazy_document_header.rb', line 14

def self.coerce(value)
  return unless value

  if value.is_a?(Esse::LazyDocumentHeader)
    value
  elsif value.is_a?(Esse::Document)
    new(value.doc_header)
  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
    new(resp)
  elsif String === value || Integer === value
    new(_id: value)
  end
end

.coerce_each(values) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/esse/lazy_document_header.rb', line 5

def self.coerce_each(values)
  arr = []
  Esse::ArrayUtils.wrap(values).map do |value|
    instance = coerce(value)
    arr << instance if instance&.valid?
  end
  arr
end

Instance Method Details

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

Returns:

  • (Boolean)


68
69
70
# File 'lib/esse/lazy_document_header.rb', line 68

def eql?(other)
  self.class == other.class && @attributes == other.instance_variable_get(:@attributes)
end

#idObject



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

def id
  @attributes.fetch(:_id)
end

#routingObject



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

def routing
  @attributes[:routing]
end

#to_doc(source = {}) ⇒ Object



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

def to_doc(source = {})
  HashDocument.new(source.merge(@attributes))
end

#to_hObject



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

def to_h
  @attributes
end

#typeObject



56
57
58
# File 'lib/esse/lazy_document_header.rb', line 56

def type
  @attributes[:_type]
end

#valid?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/esse/lazy_document_header.rb', line 44

def valid?
  !@attributes[:_id].nil?
end