module SwiftGenerator

TODO: Use jamesbrooks/hash_validator to check for required and valid values

Constants

VERSION

Public Class Methods

writeGeneratedFile( f ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 374
def writeGeneratedFile( f )
    return if ( f.is_user_file && File.exist?( f.file_path ))

    Pathname.new( f.file_path ).parent().mkpath()

Ribosome.output( f.file_path )

Ribosome.dot("//", binding)
Ribosome.dot("//  @{ Pathname.new( f.file_name ).basename }", binding)
Ribosome.dot("//  @{f.company_name}", binding)
if $definition_file
Ribosome.dot("//", binding)
Ribosome.dot("//  Generated from @{$definition_file} on @{Time.now.strftime(\"%d/%m/%Y %H:%M\")}", binding)
end
if f.include_editing_warnings
    if !f.is_user_file
Ribosome.dot("//", binding)
Ribosome.dot("//  WARNING: This entire file is generated. DO NOT EDIT.", binding)
    else
Ribosome.dot("//", binding)
Ribosome.dot("//  This is a user-editable generated file. Delete or rename this file to have a new empty file generated", binding)
    end
end
Ribosome.dot("//", binding)
Ribosome.dot("//  Copyright (c) @{Time.now.strftime(\"\")} @{f.company_name}, Inc. All rights reserved.", binding)
Ribosome.dot("//", binding)

    for import_statement in f.import_statements.sort
Ribosome.dot("&{import_statement}", binding)
    end

  for element in f.elements
    SwiftGenerator::write_element(element)
  end
end
write_category( c ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 456
def write_category( c )
Ribosome.dot("", binding)
Ribosome.dot("", binding)
Ribosome.dot("&{c.access_control_modifier}extension @{c.categorized_class_name} /*@{c.type_name || ' '}*/ {", binding)
  SwiftGenerator::write_class_body( c )
Ribosome.dot("}", binding)
end
write_class( c ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 441
def write_class( c )
Ribosome.dot("", binding)
Ribosome.dot("", binding)
Ribosome.dot("&{c.access_control_modifier}class @{c.type_name}", binding)
    if c.inheritance_list.count > 0
Ribosome.add(" : @{c.inheritance_list.join( \", \" )}", binding)
    end
Ribosome.add("  {", binding)

  SwiftGenerator::write_class_body( c )
Ribosome.dot("}", binding)
end
write_class_body( c ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 465
def write_class_body( c )
    c.top_inner_comment_block().each do |line|
Ribosome.dot("    &{line}", binding)
    end

  # SwiftGenerator::write_property_declarations( c.transient_properties, "Transient" )
  # SwiftGenerator::write_property_declarations( c.persistent_properties, "Persistent" )
  SwiftGenerator::write_property_declarations( c.transient_properties )
  SwiftGenerator::write_property_declarations( c.persistent_properties )

  SwiftGenerator::write_methods( c.initializers )
  SwiftGenerator::write_methods( c.methods )
end
write_element( element ) click to toggle source

Templated Output Methods

# File lib/swift_generator/code_generation/swift_file_template.rb, line 360
def write_element( element )
  if( element.kind_of? SwiftCategory )
    SwiftGenerator::write_category( element )
  elsif( element.kind_of? SwiftProtocol )
    SwiftGenerator::write_protocol( element )
  elsif( element.kind_of? SwiftEnum )
    SwiftGenerator::write_enum( element )
  elsif( element.kind_of? SwiftClass )      # Must be last as it is the superclass
    SwiftGenerator::write_class( element )
  end
end
write_enum( e ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 413
def write_enum( e )
Ribosome.dot("", binding)
Ribosome.dot("", binding)
Ribosome.dot("&{e.access_control_modifier}enum @{e.type_name}", binding)
    if e.inheritance_list.count > 0
Ribosome.add(" : @{e.inheritance_list.join( \", \" )}", binding)
    end
Ribosome.add("  {", binding)

  e.enum_cases.each_with_index do |enum_case, _|
    enum_case.declaration_lines().each do |declLine|
Ribosome.dot("    &{declLine}", binding)
    end
  end

  e.properties.each_with_index do |prop, i|
Ribosome.dot("", binding)
    prop.declaration_lines().each do |declLine|
Ribosome.dot("    &{declLine}", binding)
    end
  end

  SwiftGenerator::write_methods( e.methods )
Ribosome.dot(" }", binding)
end
write_files_for_definition_set( definition_set ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 348
def write_files_for_definition_set( definition_set )
    for _, f in definition_set.output_files
        SwiftGenerator::writeGeneratedFile( f )
    end
end
write_methods( methods, for_protocol=false ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 514
def write_methods( methods, for_protocol=false )
    methods.each_with_index do |m, i|
        overrideString = m.override ? 'override ' : ''
Ribosome.dot("", binding)
    if ! m.comment.nil?
Ribosome.dot("   &{m.comment}", binding)
    end
    args = m.argStr.nil? || m.argStr.empty? ? m.argStr : ' ' + m.argStr + ' '
    access_control_modifier_str = m.access_control_modifiers.length > 0 ? m.access_control_modifiers.join( ", " ) + " " : ""
Ribosome.dot("    &{overrideString}&{access_control_modifier_str}&{m.func_fragment + \" \" unless m.func_fragment.length == 0}@{m.name}(&{args})", binding)
    if ! (m.returns.nil? || m.returns.length == 0 )
Ribosome.add(" -> @{m.returns}", binding)
    end
    unless for_protocol
Ribosome.add(" {", binding)
        m.bodyLines.each do |line|
Ribosome.dot("           &{line}", binding)
        end
Ribosome.dot("    }", binding)
    end
  end
end
write_property_declarations( properties, property_type_label=nil ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 499
def write_property_declarations( properties, property_type_label=nil )
  properties.each_with_index do |prop, i|
    if i == 0 && ! property_type_label.nil?
Ribosome.dot("", binding)
Ribosome.dot("    // MARK: @{property_type_label} Properties", binding)
    end
    prop.declaration_lines().each do |declLine|
Ribosome.dot("    &{declLine}", binding)
    end
  end
end
write_protocol( p ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 480
def write_protocol( p )
Ribosome.dot("", binding)
Ribosome.dot("", binding)
Ribosome.dot("&{p.access_control_modifier}protocol @{p.type_name}", binding)
    if p.inheritance_list.count > 0
Ribosome.add(" : @{p.inheritance_list.join( \", \" )}", binding)
    end
Ribosome.add("  {", binding)
  # SwiftGenerator::write_property_declarations( c.transient_properties, "Transient" )
  # SwiftGenerator::write_property_declarations( c.persistent_properties, "Persistent" )
  SwiftGenerator::write_property_declarations( p.transient_properties )
  SwiftGenerator::write_property_declarations( p.persistent_properties )

  SwiftGenerator::write_methods( p.initializers, for_protocol=true )
  SwiftGenerator::write_methods( p.methods, for_protocol=true )
Ribosome.dot("}", binding)
end

Private Instance Methods

writeGeneratedFile( f ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 374
def writeGeneratedFile( f )
    return if ( f.is_user_file && File.exist?( f.file_path ))

    Pathname.new( f.file_path ).parent().mkpath()

Ribosome.output( f.file_path )

Ribosome.dot("//", binding)
Ribosome.dot("//  @{ Pathname.new( f.file_name ).basename }", binding)
Ribosome.dot("//  @{f.company_name}", binding)
if $definition_file
Ribosome.dot("//", binding)
Ribosome.dot("//  Generated from @{$definition_file} on @{Time.now.strftime(\"%d/%m/%Y %H:%M\")}", binding)
end
if f.include_editing_warnings
    if !f.is_user_file
Ribosome.dot("//", binding)
Ribosome.dot("//  WARNING: This entire file is generated. DO NOT EDIT.", binding)
    else
Ribosome.dot("//", binding)
Ribosome.dot("//  This is a user-editable generated file. Delete or rename this file to have a new empty file generated", binding)
    end
end
Ribosome.dot("//", binding)
Ribosome.dot("//  Copyright (c) @{Time.now.strftime(\"\")} @{f.company_name}, Inc. All rights reserved.", binding)
Ribosome.dot("//", binding)

    for import_statement in f.import_statements.sort
Ribosome.dot("&{import_statement}", binding)
    end

  for element in f.elements
    SwiftGenerator::write_element(element)
  end
end
write_category( c ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 456
def write_category( c )
Ribosome.dot("", binding)
Ribosome.dot("", binding)
Ribosome.dot("&{c.access_control_modifier}extension @{c.categorized_class_name} /*@{c.type_name || ' '}*/ {", binding)
  SwiftGenerator::write_class_body( c )
Ribosome.dot("}", binding)
end
write_class( c ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 441
def write_class( c )
Ribosome.dot("", binding)
Ribosome.dot("", binding)
Ribosome.dot("&{c.access_control_modifier}class @{c.type_name}", binding)
    if c.inheritance_list.count > 0
Ribosome.add(" : @{c.inheritance_list.join( \", \" )}", binding)
    end
Ribosome.add("  {", binding)

  SwiftGenerator::write_class_body( c )
Ribosome.dot("}", binding)
end
write_class_body( c ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 465
def write_class_body( c )
    c.top_inner_comment_block().each do |line|
Ribosome.dot("    &{line}", binding)
    end

  # SwiftGenerator::write_property_declarations( c.transient_properties, "Transient" )
  # SwiftGenerator::write_property_declarations( c.persistent_properties, "Persistent" )
  SwiftGenerator::write_property_declarations( c.transient_properties )
  SwiftGenerator::write_property_declarations( c.persistent_properties )

  SwiftGenerator::write_methods( c.initializers )
  SwiftGenerator::write_methods( c.methods )
end
write_element( element ) click to toggle source

Templated Output Methods

# File lib/swift_generator/code_generation/swift_file_template.rb, line 360
def write_element( element )
  if( element.kind_of? SwiftCategory )
    SwiftGenerator::write_category( element )
  elsif( element.kind_of? SwiftProtocol )
    SwiftGenerator::write_protocol( element )
  elsif( element.kind_of? SwiftEnum )
    SwiftGenerator::write_enum( element )
  elsif( element.kind_of? SwiftClass )      # Must be last as it is the superclass
    SwiftGenerator::write_class( element )
  end
end
write_enum( e ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 413
def write_enum( e )
Ribosome.dot("", binding)
Ribosome.dot("", binding)
Ribosome.dot("&{e.access_control_modifier}enum @{e.type_name}", binding)
    if e.inheritance_list.count > 0
Ribosome.add(" : @{e.inheritance_list.join( \", \" )}", binding)
    end
Ribosome.add("  {", binding)

  e.enum_cases.each_with_index do |enum_case, _|
    enum_case.declaration_lines().each do |declLine|
Ribosome.dot("    &{declLine}", binding)
    end
  end

  e.properties.each_with_index do |prop, i|
Ribosome.dot("", binding)
    prop.declaration_lines().each do |declLine|
Ribosome.dot("    &{declLine}", binding)
    end
  end

  SwiftGenerator::write_methods( e.methods )
Ribosome.dot(" }", binding)
end
write_files_for_definition_set( definition_set ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 348
def write_files_for_definition_set( definition_set )
    for _, f in definition_set.output_files
        SwiftGenerator::writeGeneratedFile( f )
    end
end
write_methods( methods, for_protocol=false ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 514
def write_methods( methods, for_protocol=false )
    methods.each_with_index do |m, i|
        overrideString = m.override ? 'override ' : ''
Ribosome.dot("", binding)
    if ! m.comment.nil?
Ribosome.dot("   &{m.comment}", binding)
    end
    args = m.argStr.nil? || m.argStr.empty? ? m.argStr : ' ' + m.argStr + ' '
    access_control_modifier_str = m.access_control_modifiers.length > 0 ? m.access_control_modifiers.join( ", " ) + " " : ""
Ribosome.dot("    &{overrideString}&{access_control_modifier_str}&{m.func_fragment + \" \" unless m.func_fragment.length == 0}@{m.name}(&{args})", binding)
    if ! (m.returns.nil? || m.returns.length == 0 )
Ribosome.add(" -> @{m.returns}", binding)
    end
    unless for_protocol
Ribosome.add(" {", binding)
        m.bodyLines.each do |line|
Ribosome.dot("           &{line}", binding)
        end
Ribosome.dot("    }", binding)
    end
  end
end
write_property_declarations( properties, property_type_label=nil ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 499
def write_property_declarations( properties, property_type_label=nil )
  properties.each_with_index do |prop, i|
    if i == 0 && ! property_type_label.nil?
Ribosome.dot("", binding)
Ribosome.dot("    // MARK: @{property_type_label} Properties", binding)
    end
    prop.declaration_lines().each do |declLine|
Ribosome.dot("    &{declLine}", binding)
    end
  end
end
write_protocol( p ) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 480
def write_protocol( p )
Ribosome.dot("", binding)
Ribosome.dot("", binding)
Ribosome.dot("&{p.access_control_modifier}protocol @{p.type_name}", binding)
    if p.inheritance_list.count > 0
Ribosome.add(" : @{p.inheritance_list.join( \", \" )}", binding)
    end
Ribosome.add("  {", binding)
  # SwiftGenerator::write_property_declarations( c.transient_properties, "Transient" )
  # SwiftGenerator::write_property_declarations( c.persistent_properties, "Persistent" )
  SwiftGenerator::write_property_declarations( p.transient_properties )
  SwiftGenerator::write_property_declarations( p.persistent_properties )

  SwiftGenerator::write_methods( p.initializers, for_protocol=true )
  SwiftGenerator::write_methods( p.methods, for_protocol=true )
Ribosome.dot("}", binding)
end