Sha256: d3b648308ce560066e6bff8a3aa8205ba4a855e55be04df10c0f688d69566756

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

require_relative 'template'

module Immutabler
  module Template
    class HeaderTemplate < BaseTemplate
      attr_accessor :models, :links, :enums

      def initialize(models, links, enums)
        self.template_file = File.expand_path("#{__dir__}/../../../templates/header_template.hbs")
        self.links = 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
      end

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
immutabler-0.2.6 lib/immutabler/template/header_template.rb
immutabler-0.2.5 lib/immutabler/template/header_template.rb
immutabler-0.2.4 lib/immutabler/template/header_template.rb
immutabler-0.2.3 lib/immutabler/template/header_template.rb
immutabler-0.2.2 lib/immutabler/template/header_template.rb