Module: MmNoEmpties
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mm_no_empties.rb,
lib/mm_no_empties/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
"0.0.5"
Instance Method Summary collapse
- #attributes(options = {}) ⇒ Object (also: #to_mongo)
Instance Method Details
#attributes(options = {}) ⇒ Object Also known as: to_mongo
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mm_no_empties.rb', line 42 def attributes( = {}) include_all = [:include_all] stringify_bson = [:stringify_bson] recursive = [:recursive] HashWithIndifferentAccess.new.tap do |attrs| persistable_keys = if include_all keys else keys.select do |name, key| val = self[key.name] #key.type == ObjectId or # this is in the original :attributes implementation, but is seems safe to remove it as 'belongs_to: nil' if it's missing not (val.nil? or (val.respond_to?(:empty?) and val.empty?)) end end persistable_keys.each do |name, key| v = key.set(self[key.name]) case v when MongoMapper::Document, MongoMapper::EmbeddedDocument, Hash, Array, Set v = v.to_mongo() if v and recursive when BSON::ObjectId, BSON::Binary v = v.to_s if stringify_bson end attrs[name] = v end .each do |association| if documents = instance_variable_get(association.ivar) val = if association.is_a?(MongoMapper::Plugins::Associations::OneAssociation) documents.to_mongo() else documents.map { |document| document.to_mongo() } end if include_all attrs[association.name] = val else attrs[association.name] = val unless val.nil? or (val.is_a?(Array) and val.empty?) end end end end end |