class SwiftGenerator::SpecfileParser

Attributes

specfile_path[RW]

Public Class Methods

new( specfile_path ) click to toggle source
# File lib/swift_generator/specfile_parser.rb, line 16
def initialize( specfile_path )
  @specfile_path = specfile_path
end

Public Instance Methods

add_property( swift_class, prop_spec ) click to toggle source
# File lib/swift_generator/specfile_parser.rb, line 77
def add_property( swift_class, prop_spec )
    property_name         = prop_spec[ "name" ]
    type_symbol           = prop_spec[ "type" ].to_sym
    is_persistent         = prop_spec[ "isPersistent" ]
    mutability            = prop_spec[ "mutability" ]   || "let"
    initialization_value  = prop_spec[ "initializationValue" ]
    collection_type       = prop_spec[ "collectionType" ]
    required              = prop_spec[ "required" ]     || true
    rest_omit             = prop_spec[ "restOmit" ]
    json_key              = prop_spec[ "jsonKey" ]      # only used by SwiftPersistentProperty

    mutability = mutability.to_sym

    if( is_persistent )
      SwiftPersistentProperty.new( swift_class,
                                   property_name,
                                   type_symbol,
                                   mutability,
                                   initialization_value,
                                   collection_type:collection_type,
                                   # required = required, ???
                                   json_key:json_key,
                                   rest_omit:rest_omit )
    else
      SwiftProperty.new( swift_class,
                         property_name,
                         property_type:type_symbol,
                         mutability:mutability,
                         initialization_value:initialization_value,
                         collection_type:collection_type,
                         required:required,
                         rest_omit:rest_omit )
    end
end
process_specfile( ) click to toggle source
# File lib/swift_generator/specfile_parser.rb, line 20
def process_specfile( )
  swift_definition_set = read_specfile
  swift_definition_set.run_generation_sequence

  SwiftGenerator::write_files_for_definition_set( swift_definition_set )

end
read_specfile( ) click to toggle source
# File lib/swift_generator/specfile_parser.rb, line 28
def read_specfile( )
  file_data = @specfile_path.read
  spec_hash = JSON.parse(file_data)

  generated_source_root = spec_hash[ "sourceRoot" ]
  generated_user_source_root = spec_hash[ "userClassSourceRoot" ]
  generated_test_root = spec_hash[ "testRoot" ]

  definition_set = SwiftDefinitionSet.new( generated_root:generated_source_root,
                                           generated_user_root:generated_user_source_root,
                                           generated_test_root:generated_test_root)

  characteristics_by_name = spec_hash[ "characteristicSets" ] || {}
  characteristics_by_name.each do |name, characteristics|
    #TODO validate
  end

  classes = spec_hash[ "classes" ]
  classes.each do | class_spec |
    specified_type_name     = class_spec[ "typeName" ]
    inheritance_list        = class_spec[ "inheritanceList" ]
    file_name               = class_spec[ "fileName" ]
    characteristics_name    = class_spec[ "characteristics" ]
    is_test_element         = class_spec[ "isTestElement" ] || false
    is_user_editable        = class_spec[ "isUserEditable" ] || false

    characteristics = characteristics_by_name[ characteristics_name ]
    characteristics ||= $default_swift_class_characteristics

    # def initialize (definition_set, specified_type_name, inheritance_list=[], file_name: nil,
    #                 characteristics:default_swift_class_characteristics, is_test_element: false, is_user_editable: false)
    swift_class = SwiftClass.new(
        definition_set,
        specified_type_name,
        inheritance_list,
        file_name: file_name,
        characteristics: characteristics,
        is_test_element: is_test_element,
        is_user_editable: is_user_editable )

    properties = class_spec[ "properties" ] || []
    properties.each do | prop_spec |
      add_property( swift_class, prop_spec )
    end
  end

  return definition_set
end