Sha256: 3d11e7dc275394eca65ce7b820111fb21b958e1304fbc8ea77e42e63c547700a

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

require_relative 'template'

module Immutabler
  module Template
    class BodyTemplate < BaseTemplate
      attr_accessor :name, :models

      def initialize(name, models)
        self.template_file = File.expand_path("#{__dir__}/../../../templates/body_template.hbs")
        self.name = name
        self.models = models

        helper(:dict) do |context, arg, block|
          body = "@{\n"

          if arg
            body << arg.map{|m|
              "@\"#{m.destination_name}\" : @\"#{m.origin_name}\""
            }.join(",\n")
          end

          body << "\n};"
        end

        helper(:enum_mapping_dict) do |context, arg, block|
          body = "@{\n"

          if arg
            body << arg.map{|m|
              "@\"#{m.origin_name}\" : @(#{m.destination_name})"
            }.join(",\n")
          end

          body << "\n};"
        end

        helper(:init_with_builder) do |context, arg, block|
          if arg[:base_immutable]
            "    self = [super initWithBuilder:builder modelVersion:modelVersion];\n"
          else
            "    self = [super init];\n"
          end
        end

        helper(:init_with_coder) do |context, arg, block|
          if arg[:base_immutable]
            "    self = [self init];\n"
          else
            "    self = [super init];\n"
          end
        end

        helper(:base_immutable_interface) do |context, arg, block|
          if arg[:base_immutable]
            [
              "#ifndef #{arg[:base_class]}_Private",
              "#define #{arg[:base_class]}_Private",
              "@interface #{arg[:base_class]}(Private)",
              '- (instancetype)initWithBuilder:(id)builder modelVersion:(NSInteger)modelVersion;',
              '@end',
              "#endif"
            ].join("\n")
          else
            ''
          end
        end
      end

      def render
        template.call(models: models, name: name)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
immutabler-0.2.1 lib/immutabler/template/body_template.rb
immutabler-0.2.0 lib/immutabler/template/body_template.rb
immutabler-0.1.9 lib/immutabler/template/body_template.rb