OSDN Git Service

rename model name
[pettanr/pettanr.git] / lib / manifest / model / association.rb
1 ManifestBase.require_modules "manifest/model/association/", 
2   %w|belongs_to has_many has_one|
3
4 module Manifest
5   module ModelModule
6     class Association < ManifestBase::Values
7       include AssociationModule
8       
9       attr :belongs_to, :has_many, :has_one
10       
11       def set_default
12         super
13         @values['belongs_to'] ||= {}
14         @values['has_many'] ||= {}
15         @values['has_one'] ||= {}
16       end
17       
18       def init
19         super
20         @belongs_to = ManifestBase.load_name_values self, @values, 'belongs_to', BelongsTo
21         @has_many = ManifestBase.load_name_values self, @values, 'has_many', HasMany
22         @has_one = ManifestBase.load_name_values self, @values, 'has_one', HasOne
23       end
24       
25       def item_name
26         @parent.item_name
27       end
28       
29       def each_parent_model
30         @belongs_to.each do |name, belongs_to_manifest|
31           pm = @parent.classify
32           next unless pm.element?
33           if pm.my_peta.type == 'element'
34             next unless belongs_to_manifest.model.my_peta.type == 'root'
35           end
36           if pm.my_peta.type == 'leaf'
37             next unless belongs_to_manifest.model.my_peta.type == 'binder'
38           end
39           yield belongs_to_manifest.model
40         end
41       end
42       
43       def child_element_name child_model
44         if has_one? child_model
45           child_model.item_name
46         else
47           child_model.table_name
48         end
49       end
50       
51       def has_one? child_model
52         if @has_one[child_model.item_name]
53           true
54         else
55           false
56         end
57       end
58       
59     end
60     
61   end
62 end