Class: SwiftGenerator::SwiftProperty

Inherits:
Object
  • Object
show all
Defined in:
lib/swift_generator/code_generation/swift_class_generation.rb

Direct Known Subclasses

SwiftPersistentProperty

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(swift_class, property_name, property_type_symbol, mutability = :let, initialization_value: nil, collection_type: nil, required: true, rest_omit: nil) ⇒ SwiftProperty

Returns a new instance of SwiftProperty.



872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 872

def initialize(swift_class, property_name, property_type_symbol, mutability= :let, initialization_value:nil, collection_type: nil, required: true, rest_omit:nil )
	@swift_class = swift_class
	@property_name = property_name
	@property_type_symbol = property_type_symbol
	@property_type = nil
	# @property_type = swift_class.definition_set.property_type_for_symbol(property_type)
	@mutability_type = SwiftDefinitionSet.mutability_types[mutability]
	@is_persistent = false
	@collection_type = collection_type
	@required = required

	#@access_control_modifier = 'public '
	@access_control_modifier = ''
	@property_qualifiers = nil

	@initialization_value = initialization_value
	@getter_body = nil
	@setter_body = nil
	@rest_omit = rest_omit

	swift_class.properties << self
end

Instance Attribute Details

#collection_typeObject

Returns the value of attribute collection_type.



863
864
865
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 863

def collection_type
  @collection_type
end

#getter_bodyObject

Returns the value of attribute getter_body.



867
868
869
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 867

def getter_body
  @getter_body
end

#initialization_valueObject

Returns the value of attribute initialization_value.



866
867
868
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 866

def initialization_value
  @initialization_value
end

#is_persistentObject

Returns the value of attribute is_persistent.



862
863
864
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 862

def is_persistent
  @is_persistent
end

#mutability_typeObject

Returns the value of attribute mutability_type.



860
861
862
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 860

def mutability_type
  @mutability_type
end

#property_nameObject

Returns the value of attribute property_name.



857
858
859
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 857

def property_name
  @property_name
end

#property_qualifiersObject

Returns the value of attribute property_qualifiers.



861
862
863
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 861

def property_qualifiers
  @property_qualifiers
end

#property_typeObject

Returns the value of attribute property_type.



859
860
861
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 859

def property_type
  @property_type
end

#property_type_symbolObject

Returns the value of attribute property_type_symbol.



858
859
860
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 858

def property_type_symbol
  @property_type_symbol
end

#requiredObject

Returns the value of attribute required.



864
865
866
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 864

def required
  @required
end

#rest_omitObject

Returns the value of attribute rest_omit.



869
870
871
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 869

def rest_omit
  @rest_omit
end

#setter_bodyObject

Returns the value of attribute setter_body.



868
869
870
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 868

def setter_body
  @setter_body
end

#swift_classObject

Returns the value of attribute swift_class.



856
857
858
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 856

def swift_class
  @swift_class
end

Instance Method Details

#declaration_linesObject



895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 895

def declaration_lines

	qualifiers = []
	qualifiers += [*@property_qualifiers] if !@property_qualifiers.nil?
	qualifiers << @mutability_type.mutability

	declaration = "#{qualifiers.join(' ')} #{@property_name} : #{full_type_specifier()}"

	# Initial Value
	initial_value = @initialization_value
	if !initial_value.nil?
		if( collection_type == :array )
			if( @mutability_type.mutability_id == :optional )
				# Initialize variable arrays to empty by default
				initial_value = "[]"
			end

		end
	end

	if (!initial_value.nil?)
		declaration += " = #{initial_value}"
	end

	# Computed Properties
	if !( @getter_body.nil? && @setter_body.nil? )
		declaration = [declaration + " {"]

		if !@getter_body.nil?
			declaration << "\tget {"
			declaration.concat([*@getter_body].map { |line| "\t\t" + line })
			declaration << "\t}"
		end

		if !@setter_body.nil?
			declaration << "" unless @getter_body.nil?
			declaration << "\tset {"
			declaration.concat([*@setter_body].map { |line| "\t\t" + line })
			declaration << "\t}"
		end

		declaration << "}"
	end

	return [*declaration]
end

#full_type_specifierObject



942
943
944
945
946
947
948
949
950
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 942

def full_type_specifier
	# Apply Collection
	full_type_name = @property_type.swift_type_name
	if @collection_type == :array
		full_type_name = "[#{full_type_name}]"
	end

	"#{full_type_name}#{@mutability_type.declaration_wrapping}"
end

#is_array_of_nsobjectObject

Utility



972
973
974
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 972

def is_array_of_nsobject
	(@collection_type == :array) && (@property_type.swift_kind == :class)
end

#is_optionalObject



976
977
978
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 976

def is_optional
	return @mutability_type.mutability_id == :optional
end

#make_test_value(index) ⇒ Object



959
960
961
962
963
964
965
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 959

def make_test_value(index)
	if @collection_type.nil?
		return @property_type.make_test_value(index)
	else
		return '[]'
	end
end

#property_declared_typeObject



967
968
969
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 967

def property_declared_type
	@property_type.swift_type_name + @mutability_type.declaration_wrapping
end

#resolve_typeObject



953
954
955
956
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 953

def resolve_type()
	@property_type = @swift_class.definition_set.property_type_for_symbol(@property_type_symbol)
	abort( "No property type found for #{@property_type_symbol.to_s}") if @property_type.nil?
end