Class: BSON::Document

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

Overview

Injects behaviour for BSON::Document.

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#dig_or_get(*args, &block) ⇒ Object

Retrieves the value object corresponding to the each key objects repeatedly. Will normalize symbol keys into strings. Will returns &block.call if the returned value is nil

Since:

  • 1.0.0



15
16
17
18
19
20
21
# File 'lib/bson/document.rb', line 15

def dig_or_get(*args, &block)
  value = self.dig *args
  if value.nil? && !block.nil?
    value = block.call
  end
  value
end

#get_embedded(path, *args, &block) ⇒ Object

Get embedded value.

Since:

  • 1.0.0



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bson/document.rb', line 26

def get_embedded(path, *args, &block)
  keys = path.split '.'
  value = self
  keys.each do |key|
    if value.is_a? BSON::Document
      value = value[key]
    elsif value.is_a? Array
      if /^\d+$/.match key
        value = value[key.to_i]
      else
        return block.nil? ? (args.empty? ? nil : args.first) : block.call
      end
    else
      return block.nil? ? (args.empty? ? nil : args.first) : block.call
    end
  end
  if value.nil?
    return block.nil? ? (args.empty? ? nil : args.first) : block.call
  end
  return value
end