Module: Mongoid::Extensions::Hash
- Defined in:
- lib/mongoid/extensions/hash.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#__consolidate__(klass) ⇒ Hash
Consolidate the key/values in the hash under an atomic $set.
-
#__evolve_object_id__ ⇒ Hash
Evolves each value in the hash to an object id if it is convertable.
-
#__mongoize_object_id__ ⇒ Hash
Mongoizes each value in the hash to an object id if it is convertable.
-
#__nested__(string) ⇒ Object
Fetch a nested value via dot syntax.
-
#blank_criteria? ⇒ true, false
Check if the hash is part of a blank relation criteria.
-
#delete_id ⇒ Object
Deletes an id value from the hash.
-
#extract_id ⇒ Object
Get the id attribute from this hash, whether it’s prefixed with an underscore or is a symbol.
-
#mongoize ⇒ Hash
Turn the object from the ruby type we deal with to a Mongo friendly type.
-
#resizable? ⇒ true
Can the size of this object change?.
-
#to_criteria ⇒ Criteria
Convert this hash to a criteria.
Instance Method Details
#__consolidate__(klass) ⇒ Hash
Consolidate the key/values in the hash under an atomic $set.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mongoid/extensions/hash.rb', line 42 def __consolidate__(klass) consolidated = {} each_pair do |key, value| if key =~ /\$/ value.each_pair do |_key, _value| value[_key] = (key == "$rename") ? _value.to_s : mongoize_for(key, klass, _key, _value) end (consolidated[key] ||= {}).merge!(value) else (consolidated["$set"] ||= {}).merge!(key => mongoize_for(key, klass, key, value)) end end consolidated end |
#__evolve_object_id__ ⇒ Hash
Evolves each value in the hash to an object id if it is convertable.
14 15 16 |
# File 'lib/mongoid/extensions/hash.rb', line 14 def __evolve_object_id__ update_values(&:__evolve_object_id__) end |
#__mongoize_object_id__ ⇒ Hash
Mongoizes each value in the hash to an object id if it is convertable.
26 27 28 29 30 31 32 |
# File 'lib/mongoid/extensions/hash.rb', line 26 def __mongoize_object_id__ if id = self['$oid'] BSON::ObjectId.from_string(id) else update_values(&:__mongoize_object_id__) end end |
#__nested__(string) ⇒ Object
Fetch a nested value via dot syntax.
104 105 106 107 108 109 110 111 112 |
# File 'lib/mongoid/extensions/hash.rb', line 104 def __nested__(string) keys = string.split(".") value = self keys.each do |key| nested = value[key] || value[key.to_i] value = nested end value end |
#blank_criteria? ⇒ true, false
Check if the hash is part of a blank relation criteria.
65 66 67 |
# File 'lib/mongoid/extensions/hash.rb', line 65 def blank_criteria? self == { "_id" => { "$in" => [] }} end |
#delete_id ⇒ Object
Deletes an id value from the hash.
77 78 79 |
# File 'lib/mongoid/extensions/hash.rb', line 77 def delete_id delete("_id") || delete("id") || delete(:id) || delete(:_id) end |
#extract_id ⇒ Object
Get the id attribute from this hash, whether it’s prefixed with an underscore or is a symbol.
90 91 92 |
# File 'lib/mongoid/extensions/hash.rb', line 90 def extract_id self["_id"] || self["id"] || self[:id] || self[:_id] end |
#mongoize ⇒ Hash
Turn the object from the ruby type we deal with to a Mongo friendly type.
123 124 125 |
# File 'lib/mongoid/extensions/hash.rb', line 123 def mongoize ::Hash.mongoize(self) end |
#resizable? ⇒ true
Can the size of this object change?
135 136 137 |
# File 'lib/mongoid/extensions/hash.rb', line 135 def resizable? true end |
#to_criteria ⇒ Criteria
Convert this hash to a criteria. Will iterate over each keys in the hash which must correspond to method on a criteria object. The hash must also include a “klass” key.
149 150 151 152 153 154 155 |
# File 'lib/mongoid/extensions/hash.rb', line 149 def to_criteria criteria = Criteria.new(delete(:klass) || delete("klass")) each_pair do |method, args| criteria = criteria.__send__(method, args) end criteria end |