Class: MetaModel::Installer::Renderer
- Inherits:
-
Object
- Object
- MetaModel::Installer::Renderer
- Includes:
- Config::Mixin
- Defined in:
- lib/metamodel/installer/renderer.rb
Constant Summary collapse
- SWIFT_TEMPLATES_FILES =
%w( file_header table_initialize model_initialize model_update model_query model_delete static_methods helper )
Instance Attribute Summary collapse
-
#associations ⇒ Object
readonly
Returns the value of attribute associations.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Instance Method Summary collapse
-
#initialize(models, associations) ⇒ Renderer
constructor
A new instance of Renderer.
- #model_swift_templates ⇒ Object
- #remove_previous_files_refereneces ⇒ Object
- #render! ⇒ Object
- #render_association_files ⇒ Object
- #render_model_files ⇒ Object
Methods included from Config::Mixin
Constructor Details
Instance Attribute Details
#associations ⇒ Object (readonly)
Returns the value of attribute associations.
11 12 13 |
# File 'lib/metamodel/installer/renderer.rb', line 11 def associations @associations end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
10 11 12 |
# File 'lib/metamodel/installer/renderer.rb', line 10 def models @models end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
8 9 10 |
# File 'lib/metamodel/installer/renderer.rb', line 8 def project @project end |
Instance Method Details
#model_swift_templates ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/metamodel/installer/renderer.rb', line 30 def model_swift_templates [].tap do |templates| SWIFT_TEMPLATES_FILES.each do |file_path| template = File.read File.(File.join(File.dirname(__FILE__), "../template/#{file_path}.swift")) templates << template end end end |
#remove_previous_files_refereneces ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/metamodel/installer/renderer.rb', line 50 def remove_previous_files_refereneces target = @project.targets.first @models.each do |model| target.source_build_phase.files_references.each do |file_ref| target.source_build_phase.remove_file_reference(file_ref) if file_ref && "#{model.name}.swift" == file_ref.name end end @associations.each do |association| target.source_build_phase.files_references.each do |file_ref| target.source_build_phase.remove_file_reference(file_ref) if file_ref && "#{association.class_name}.swift" == file_ref.name end end end |
#render! ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/metamodel/installer/renderer.rb', line 39 def render! remove_previous_files_refereneces UI.section "Generating model files" do render_model_files end UI.section "Generating association files" do render_association_files end @project.save end |
#render_association_files ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/metamodel/installer/renderer.rb', line 88 def render_association_files target = @project.targets.first association_group = @project.main_group.find_subpath('MetaModel/Associations', true) association_group.clear association_group.set_source_tree('SOURCE_ROOT') has_many_association_template = File.read File.(File.join(File.dirname(__FILE__), "../template/has_many_association.swift")) belongs_to_association_template = File.read File.(File.join(File.dirname(__FILE__), "../template/belongs_to_association.swift")) file_refs = [] @associations.each do |association| template = association.relation == :has_many ? has_many_association_template : belongs_to_association_template result = ErbalTemplate::render_from_hash(template, { :association => association }) file_name = "#{association.class_name}.swift" File.write Pathname.new("./metamodel/MetaModel/#{file_name}"), result file_refs << association_group.new_reference(Pathname.new("MetaModel/#{file_name}")) UI. '-> '.green + "Using #{file_name} file" end target.add_file_references file_refs end |
#render_model_files ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/metamodel/installer/renderer.rb', line 66 def render_model_files target = @project.targets.first models_group = @project.main_group.find_subpath('MetaModel/Models', true) models_group.clear models_group.set_source_tree('SOURCE_ROOT') file_refs = [] @models.each do |model| result = model_swift_templates.map { |template| ErbalTemplate::render_from_hash(template, { :model => model }) }.join("\n") model_path = Pathname.new("./metamodel/MetaModel/#{model.name}.swift") File.write model_path, result file_refs << models_group.new_reference(Pathname.new("MetaModel/#{model.name}.swift")) UI. '-> '.green + "Using #{model.name}.swift file" end target.add_file_references file_refs end |