Module: Mongoid::Persistable::Settable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Mongoid::Persistable
- Defined in:
- lib/mongoid/persistable/settable.rb
Overview
Defines behaviour for $set operations.
Instance Method Summary collapse
-
#set(setters) ⇒ Document
Perform a $set operation on the provided field/value pairs and set the values in the document in memory.
Instance Method Details
#set(setters) ⇒ Document
Perform a $set operation on the provided field/value pairs and set the values in the document in memory.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mongoid/persistable/settable.rb', line 22 def set(setters) prepare_atomic_operation do |ops| process_atomic_operations(setters) do |field, value| field_and_value_hash = hasherizer(field.split('.'), value) field = field_and_value_hash.keys.first.to_s if fields[field] && fields[field].type == Hash && attributes.key?(field) merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 } value = attributes[field].merge(field_and_value_hash[field], &merger) process_attribute(field.to_s, value) else process_attribute(field.to_s, field_and_value_hash[field]) end unless relations.include?(field.to_s) ops[atomic_attribute_name(field)] = attributes[field] end end { "$set" => ops } unless ops.empty? end end |