Class: BSON::Document
- Inherits:
-
Hash
- Object
- Hash
- BSON::Document
- Defined in:
- lib/bson/document.rb
Overview
The specification is: document ::= int32 e_list “x00”
This module provides behaviour for serializing and deserializing entire BSON documents, according to the BSON specification.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get a value from the document for the provided key.
-
#[]=(key, value) ⇒ Object
Set a value on the document.
-
#delete(key, &block) ⇒ Object
Deletes the key-value pair and returns the value from the document whose key is equal to key.
-
#dig(*keys) ⇒ Object, NilClass
Retrieves the value object corresponding to the each key objects repeatedly.
-
#fetch(key, *args) {|key| ... } ⇒ Object
Get a value from the document for the provided key.
-
#has_key?(key) ⇒ true, false
(also: #include?, #key?, #member?)
Returns true if the given key is present in the document.
-
#has_value?(value) ⇒ true, false
(also: #value)
Returns true if the given value is present in the document.
-
#initialize(elements = nil) ⇒ Document
constructor
Instantiate a new Document.
-
#merge(other, &block) ⇒ BSON::Document
Merge this document with another document, returning a new document in the process.
-
#merge!(other) ⇒ BSON::Document
(also: #update)
Merge this document with another document, returning the same document in the process.
-
#slice(*keys) ⇒ BSON::Document
Slices a document to include only the given keys.
Constructor Details
#initialize(elements = nil) ⇒ Document
Instantiate a new Document. Valid parameters for instantiation is a hash only or nothing.
169 170 171 172 |
# File 'lib/bson/document.rb', line 169 def initialize(elements = nil) super() (elements || {}).each_pair{ |key, value| self[key] = value } end |
Instance Method Details
#[](key) ⇒ Object
Get a value from the document for the provided key. Can use string or symbol access, with string access being the faster of the two.
87 88 89 |
# File 'lib/bson/document.rb', line 87 def [](key) super(convert_key(key)) end |
#[]=(key, value) ⇒ Object
Set a value on the document. Will normalize symbol keys into strings.
102 103 104 |
# File 'lib/bson/document.rb', line 102 def []=(key, value) super(convert_key(key), convert_value(value)) end |
#delete(key, &block) ⇒ Object
Deletes the key-value pair and returns the value from the document whose key is equal to key. If the key is not found, returns the default value. If the optional code block is given and the key is not found, pass in the key and return the result of block.
156 157 158 |
# File 'lib/bson/document.rb', line 156 def delete(key, &block) super(convert_key(key), &block) end |
#dig(*keys) ⇒ Object, NilClass
Retrieves the value object corresponding to the each key objects repeatedly. Will normalize symbol keys into strings.
225 226 227 |
# File 'lib/bson/document.rb', line 225 def dig(*keys) super(*keys.map{|key| convert_key(key)}) end |
#fetch(key) ⇒ Object #fetch(key, default) ⇒ Object #fetch(key, &block) ⇒ Object
Get a value from the document for the provided key. Can use string or symbol access, with string access being the faster of the two.
68 69 70 71 |
# File 'lib/bson/document.rb', line 68 def fetch(key, *args, &block) key = convert_key(key) super(key, *args, &block) end |
#has_key?(key) ⇒ true, false Also known as: include?, key?, member?
Returns true if the given key is present in the document. Will normalize symbol keys into strings.
117 118 119 |
# File 'lib/bson/document.rb', line 117 def has_key?(key) super(convert_key(key)) end |
#has_value?(value) ⇒ true, false Also known as: value
Returns true if the given value is present in the document. Will normalize symbols into strings.
136 137 138 |
# File 'lib/bson/document.rb', line 136 def has_value?(value) super(convert_value(value)) end |
#merge(other, &block) ⇒ BSON::Document
Merge this document with another document, returning a new document in the process.
185 186 187 |
# File 'lib/bson/document.rb', line 185 def merge(other, &block) dup.merge!(other, &block) end |
#merge!(other) ⇒ BSON::Document Also known as: update
Merge this document with another document, returning the same document in the process.
200 201 202 203 204 205 206 |
# File 'lib/bson/document.rb', line 200 def merge!(other) other.each_pair do |key, value| value = yield(convert_key(key), self[key], convert_value(value)) if block_given? && self[key] self[key] = value end self end |
#slice(*keys) ⇒ BSON::Document
Slices a document to include only the given keys. Will normalize symbol keys into strings. (this method is backported from ActiveSupport::Hash)
246 247 248 |
# File 'lib/bson/document.rb', line 246 def slice(*keys) super(*keys.map{|key| convert_key(key)}) end |