Class: Immutabler::Template::HeaderTemplate
- Inherits:
-
BaseTemplate
- Object
- BaseTemplate
- Immutabler::Template::HeaderTemplate
- Defined in:
- lib/immutabler/template/header_template.rb
Instance Attribute Summary collapse
-
#enums ⇒ Object
Returns the value of attribute enums.
-
#links ⇒ Object
Returns the value of attribute links.
-
#models ⇒ Object
Returns the value of attribute models.
-
#module_links ⇒ Object
Returns the value of attribute module_links.
Attributes inherited from BaseTemplate
Instance Method Summary collapse
- #build_enums(enums) ⇒ Object
- #build_module_imports(module_links) ⇒ Object
-
#initialize(models, links, module_links, enums) ⇒ HeaderTemplate
constructor
A new instance of HeaderTemplate.
- #render ⇒ Object
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.("#{__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| = block[:hash] access_type = [: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
#enums ⇒ Object
Returns the value of attribute enums.
6 7 8 |
# File 'lib/immutabler/template/header_template.rb', line 6 def enums @enums end |
#links ⇒ Object
Returns the value of attribute links.
6 7 8 |
# File 'lib/immutabler/template/header_template.rb', line 6 def links @links end |
#models ⇒ Object
Returns the value of attribute models.
6 7 8 |
# File 'lib/immutabler/template/header_template.rb', line 6 def models @models end |
#module_links ⇒ Object
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 |
#render ⇒ Object
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 |