Module: MongoMapper::ToDao::InstanceMethods

Defined in:
lib/dao/mongo_mapper.rb

Instance Method Summary collapse

Instance Method Details

#to_dao(*args) ⇒ Object Also known as: to_h



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dao/mongo_mapper.rb', line 33

def to_dao(*args)
  hash = Dao.hash
  model = self.class

  attrs = args.empty? ? model.to_dao : args

  attrs.each do |attr|
    value = send(attr)

    if value.respond_to?(:to_dao)
      hash[attr] = value.to_dao
      next
    end

    if value.is_a?(Array)
      hash[attr] = value.map{|val| val.respond_to?(:to_dao) ? val.to_dao : val}
      next
    end

    hash[attr] = value
  end

  if hash.has_key?(:_id) and not hash.has_key?(:id)
    hash[:id] = hash[:_id]
  end

  hash
end