5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
61
62
|
# File 'lib/mongoid-dsl/doc.rb', line 5
def mongoid_relations_dump_parse(obj)
case obj
when Class
return {
obj.to_s => {
'relations' => {
'one' => obj.relations.map{ |name,attributes|
if attributes[:relation].to_s.downcase =~ /embedded::one$/
mongoid_relations_dump_parse name.convert_model_name
end
}.compact,
'many' => obj.relations.map{ |name,attributes|
if attributes[:relation].to_s.downcase =~ /embedded::many$/
mongoid_relations_dump_parse name.convert_model_name
elsif attributes[:relation].to_s.downcase =~ /referenced::many$/
name.convert_model_name.to_s
end
}.compact,
'manytomany' => obj.relations.map{ |name,attributes|
if attributes[:relation].to_s.downcase =~ /manytomany$/
name.convert_model_name.to_s
end
}.compact
}.select{ |k,v| !v.empty? },
'attributes' => obj.properties.reduce({}){|m,o| m.merge!(o[0] => o[1].to_s) ;m}
}.select{ |k,v| !v.empty? }
}
when Hash
return obj
when Array
obj.reduce({}) do |m,e|
case e
when Class
m.merge!(mongoid_relations_dump_parse(e))
when Hash
m.merge!(e)
else
m.merge!(e => nil)
end;m
end
else
return obj
end
end
|