class MetaModel::Installer::Renderer

Constants

SWIFT_TEMPLATES_FILES

Attributes

associations[R]
models[R]
project[R]

Public Class Methods

new(models, associations) click to toggle source
# File lib/metamodel/installer/renderer.rb, line 13
def initialize(models, associations)
  @models = models
  @associations = associations
  @project = Xcodeproj::Project.open(Config.instance.metamodel_xcode_project)
end

Public Instance Methods

remove_previous_files_refereneces() click to toggle source
# File lib/metamodel/installer/renderer.rb, line 30
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!() click to toggle source
# File lib/metamodel/installer/renderer.rb, line 19
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() click to toggle source
# File lib/metamodel/installer/renderer.rb, line 68
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')

  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.message '-> '.green + "Using #{file_name} file"
  end
  target.add_file_references file_refs
end
render_model_files() click to toggle source
# File lib/metamodel/installer/renderer.rb, line 46
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.message '-> '.green + "Using #{model.name}.swift file"
  end
  target.add_file_references file_refs
end

Private Instance Methods

belongs_to_association_template() click to toggle source
# File lib/metamodel/installer/renderer.rb, line 115
def belongs_to_association_template
  File.read File.expand_path(File.join(File.dirname(__FILE__), "../template/association/belongs_to_association.swift"))
end
has_many_association_template() click to toggle source
# File lib/metamodel/installer/renderer.rb, line 111
def has_many_association_template
  File.read File.expand_path(File.join(File.dirname(__FILE__), "../template/association/has_many_association.swift"))
end
model_swift_templates() click to toggle source
# File lib/metamodel/installer/renderer.rb, line 102
def model_swift_templates
  [].tap do |templates|
    SWIFT_TEMPLATES_FILES.each do |file_path|
      template = File.read File.expand_path(File.join(File.dirname(__FILE__), "../template/model/#{file_path}.swift"))
      templates << template
    end
  end
end