Class: MetaModel::Installer::Renderer

Inherits:
Object
  • Object
show all
Includes:
Config::Mixin
Defined in:
lib/metamodel/installer/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Config::Mixin

#config

Constructor Details

#initialize(models, associations) ⇒ Renderer

Returns a new instance of Renderer.



13
14
15
16
17
# 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

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



11
12
13
# File 'lib/metamodel/installer/renderer.rb', line 11

def associations
  @associations
end

#modelsObject (readonly)

Returns the value of attribute models.



10
11
12
# File 'lib/metamodel/installer/renderer.rb', line 10

def models
  @models
end

#projectObject (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

#remove_previous_files_referenecesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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!Object



19
20
21
22
23
24
25
26
27
28
# 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_filesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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_filesObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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