-require_dependency "manifest/model/attribute"
-require_dependency "manifest/model/association"
-require_dependency "manifest/model/list"
+ManifestBase.require_modules "manifest/model/",
+ %w|attribute association|
module Manifest
- class Model
+ class Model < ManifestBase::Base
+ include ModelModule
- def self.manager manifest, my_manifests
- models = {}
- my_manifests.each {|model_name, model_manifest|
- models[model_name] = self.new(manifest, model_name, model_manifest)
+ attr :attributes, :associations, :table_name
+
+ def set_default
+ super
+ @json['table_name'] ||= ::Manifest.manifest.pluralize(@name)
+ @json['attributes'] ||= {}
+ @json['associations'] ||= {}
+ @json['attributes']['id'] = {
+ 'type' => 'number',
+ 'primary_key' => 1,
+ 'rules' => {
+ 'number' => true
+ }
+ }
+ @json['attributes']['created_at'] = {
+ 'type' => 'datetime',
+ }
+ @json['attributes']['updated_at'] = {
+ 'type' => 'datetime',
}
- models
end
- attr :model_name, :model_manifest,
- :attributes, :associations, :tree, :lists
- def initialize manifest, model_name, model_manifest
- @manifest = manifest
- @model_name = model_name
- @model_manifest = model_manifest
- self.set_default
- self.init
+ def init
+ super
+ @table_name = @json['table_name']
+ @attributes = ManifestBase.load_name_values self, @json, 'attributes', Attribute
+ @associations = ManifestBase.load_value self, @json, 'associations', Association
end
- def set_default
- @model_manifest['attributes'] ||= {}
- @model_manifest['associations'] ||= {}
- @model_manifest['tree'] ||= {}
- @model_manifest['lists'] ||= {}
+ def item_name
+ @name
end
- def init
- @attributes = {}
- @model_manifest['attributes'].each {|attribute_name, attribute_manifest|
- @attributes[attribute_name] = ModelModule::Attribute.new(self, attribute_name, attribute_manifest)
+ def classify
+ ::Manifest.item_name_to_model @name
+ end
+
+ def valid_encode_columns
+ r = []
+ @attributes.each {|attribute_name, attribute|
+ next unless attribute.type == 'text'
+ r << attribute_name
}
- @associations = ModelModule::Association.new(self, @model_manifest['associations'])
- @tree = @model_manifest['tree']
- @lists = {}
- @model_manifest['lists'].each {|list_name, list_manifest|
- @lists[list_name] = ModelModule::ListFactory.factory(self, list_name, list_manifest)
+ r
+ end
+
+ def owner_type
+ return :author if @attributes['author_id']
+ return :artist if @attributes['artist_id']
+ false
+ end
+
+ def content_model?
+ return true if self.owner_type
+ false
+ end
+
+ def child_model_manifests
+ r = []
+ ::Manifest.manifest.items.each {|peta_name, peta_manifest|
+ next unless peta_manifest.element?
+ next unless peta_manifest.parent_item_name == @name
+ r << ::Manifest.manifest.models[peta_name]
}
+ r
end
- def classify
- ::Manifest.item_name_to_model @model_name
+ def child_models
+ self.child_model_manifests.map {|child_model_manifest|
+ child_model_manifest.classify
+ }
+ end
+
+ def child_element_names
+ self.child_models.map {|child_model|
+ self.associations.child_element_name(child_model)
+ }
end
- def tree_model_manifest tree_name
- if tree_name
- if @tree[tree_name]
- r = ::Manifest.manifest.models[@tree[tree_name]]
- raise "undefined tree_model for models > #{@model_name} > tree > #{tree_name}\n" unless r
- r
- else
- nil
- end
- else
- nil
- end
+ def child_element_name item_name
+ self.associations.child_element_name(child_model_manifest)
end
end