Class: BSON::Document
- Inherits:
-
Object
- Object
- BSON::Document
- Defined in:
- lib/bson/document.rb
Overview
Injects behaviour for BSON::Document.
Instance Method Summary collapse
-
#dig_or_get(*args, &block) ⇒ Object
Retrieves the value object corresponding to the each key objects repeatedly.
-
#get_embedded(path, *args, &block) ⇒ Object
Get embedded value.
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
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.
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 (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 |