Class: Immutabler::Template::HeaderTemplate

Inherits:
BaseTemplate show all
Defined in:
lib/immutabler/template/header_template.rb

Instance Attribute Summary collapse

Attributes inherited from BaseTemplate

#template_file

Instance Method Summary collapse

Methods inherited from BaseTemplate

#handlebars, #helper, #raw_template, #template

Constructor Details

#initialize(models, links, module_links, enums) ⇒ HeaderTemplate

Returns a new instance of HeaderTemplate.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/immutabler/template/header_template.rb', line 8

def initialize(models, links, module_links, enums)
  self.template_file = File.expand_path("#{__dir__}/../../../templates/header_template.hbs")
  self.links = links
  self.module_links = build_module_imports(module_links)
  self.models = models
  self.enums = build_enums(enums)

  helper(:interface) do |context, arg, block|
    [
      "@interface #{arg[:name]} : #{arg[:base_class]}",
      '',
      "#{block.fn(context)}",
      '@end',
      ''
    ].join("\n")
  end

  helper(:declare_props) do |context, arg, block|
    arg.map do |prop|
      options = block[:hash]
      access_type = options[:readOnly] ? 'readonly' : 'readwrite'
      asterisk = prop[:is_ref] && !prop[:is_id] ? '*' : ''
      asterisk_and_prefix = "#{asterisk}#{prop[:name_prefix]}"
      memory_management = prop[:is_ref] ? prop[:ref_type] : 'assign';
      prop_arguments = "@property(nonatomic, #{memory_management}, #{access_type})"
      prop_field = "#{prop[:type]} #{asterisk_and_prefix}#{prop[:name]}"
      "#{prop_arguments} #{prop_field};"
    end.join("\n")
  end

  helper(:enum) do |context, arg, block|
    [
      'typedef enum {',
      arg[:attributes].map(&:name).join(",\n"),
      "} #{arg[:name]};",
    ].join("\n")
  end

  helper(:module_import) do |context, arg, block|
    [
      "#import <#{arg[:module_name]}/#{arg[:class_name]}.h>\n"
    ].join("\n")
  end
end

Instance Attribute Details

#enumsObject

Returns the value of attribute enums.



6
7
8
# File 'lib/immutabler/template/header_template.rb', line 6

def enums
  @enums
end

Returns the value of attribute links.



6
7
8
# File 'lib/immutabler/template/header_template.rb', line 6

def links
  @links
end

#modelsObject

Returns the value of attribute models.



6
7
8
# File 'lib/immutabler/template/header_template.rb', line 6

def models
  @models
end

Returns the value of attribute module_links.



6
7
8
# File 'lib/immutabler/template/header_template.rb', line 6

def module_links
  @module_links
end

Instance Method Details

#build_enums(enums) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/immutabler/template/header_template.rb', line 62

def build_enums(enums)
  enums.map do |enum|
    attributes = enum.attributes.map { |attr| {name: attr.name} }

    {
      name: enum.name,
      attributes: attributes
    }
  end
end

#build_module_imports(module_links) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/immutabler/template/header_template.rb', line 53

def build_module_imports(module_links)
  module_links.map do |module_link|
    dict_mapping = {
      module_name: module_link.module_name,
      class_name: module_link.class_name,
    }
  end
end

#renderObject



73
74
75
# File 'lib/immutabler/template/header_template.rb', line 73

def render
  template.call(models: models, links: links, module_links:module_links, enums: enums)
end