OSDN Git Service

fix remove tree
[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 model_name
26         @parent.name
27       end
28       
29       def each_parent_model
30         @belongs_to.each do |name, belongs_to_manifest|
31           next unless @parent.classify.element?
32           if @parent.classify.my_peta.type == 'element'
33             next unless belongs_to_manifest.model.my_peta.type == 'root'
34           end
35           if @parent.classify.my_peta.type == 'leaf'
36             next unless belongs_to_manifest.model.my_peta.type == 'binder'
37           end
38  p  [@parent.classify  , belongs_to_manifest.model]
39           yield belongs_to_manifest.model
40         end
41       end
42       
43       def child_element_name child_model
44         if @has_one[child_model.item_name]
45           child_model.item_name
46         else
47           child_model.table_name
48         end
49       end
50       
51     end
52     
53   end
54 end