module MmNoEmpties

Constants

VERSION

Public Instance Methods

attributes(options = {}) click to toggle source
# File lib/mm_no_empties.rb, line 42
def attributes(options = {})

  include_all     = options[:include_all]
  stringify_bson  = options[:stringify_bson]
  recursive       = options[: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(options) if v and recursive
      when BSON::ObjectId, BSON::Binary
        v = v.to_s if stringify_bson
      end
      attrs[name] = v   
    end

    embedded_associations.each do |association|
      if documents = instance_variable_get(association.ivar)
        
        val = if association.is_a?(MongoMapper::Plugins::Associations::OneAssociation)
          documents.to_mongo(options)
        else
          documents.map { |document| document.to_mongo(options) }
        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
Also aliased as: to_mongo
to_mongo(options = {})
Alias for: attributes