Class: SwiftGenerator::SwiftProperty

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

Direct Known Subclasses

SwiftBlockProperty, 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.



900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 900

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_modifiers = nil
	@property_qualifiers = nil

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

	@protocol_get_set_spec = nil

	swift_class.properties << self
end

Instance Attribute Details

#access_control_modifiersObject

Returns the value of attribute access_control_modifiers.



895
896
897
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 895

def access_control_modifiers
  @access_control_modifiers
end

#collection_typeObject

Returns the value of attribute collection_type.



888
889
890
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 888

def collection_type
  @collection_type
end

#getter_bodyObject

Returns the value of attribute getter_body.



892
893
894
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 892

def getter_body
  @getter_body
end

#initialization_valueObject

Returns the value of attribute initialization_value.



891
892
893
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 891

def initialization_value
  @initialization_value
end

#is_persistentObject

Returns the value of attribute is_persistent.



887
888
889
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 887

def is_persistent
  @is_persistent
end

#mutability_typeObject

Returns the value of attribute mutability_type.



885
886
887
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 885

def mutability_type
  @mutability_type
end

#property_nameObject

Returns the value of attribute property_name.



882
883
884
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 882

def property_name
  @property_name
end

#property_qualifiersObject

Returns the value of attribute property_qualifiers.



886
887
888
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 886

def property_qualifiers
  @property_qualifiers
end

#property_typeObject

Returns the value of attribute property_type.



884
885
886
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 884

def property_type
  @property_type
end

#property_type_symbolObject

Returns the value of attribute property_type_symbol.



883
884
885
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 883

def property_type_symbol
  @property_type_symbol
end

#protocol_get_set_specObject

for declarations in protocols



897
898
899
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 897

def protocol_get_set_spec
  @protocol_get_set_spec
end

#requiredObject

Returns the value of attribute required.



889
890
891
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 889

def required
  @required
end

#rest_omitObject

Returns the value of attribute rest_omit.



894
895
896
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 894

def rest_omit
  @rest_omit
end

#setter_bodyObject

Returns the value of attribute setter_body.



893
894
895
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 893

def setter_body
  @setter_body
end

#swift_classObject

Returns the value of attribute swift_class.



881
882
883
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 881

def swift_class
  @swift_class
end

Instance Method Details

#declaration_linesObject



925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 925

def declaration_lines

	qualifiers = []
	qualifiers += [*@access_control_modifiers] unless @access_control_modifiers.nil?
	qualifiers += [*@property_qualifiers] unless @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

   declaration += " = #{initial_value}" unless initial_value.nil?

   declaration += " #{@protocol_get_set_spec}" unless @protocol_get_set_spec.nil?  # Must be set if part of a protocol definition

	# 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



973
974
975
976
977
978
979
980
981
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 973

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



1003
1004
1005
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1003

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

#is_optionalObject



1007
1008
1009
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1007

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

#make_test_value(index) ⇒ Object



990
991
992
993
994
995
996
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 990

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

#property_declared_typeObject



998
999
1000
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 998

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

#resolve_typeObject



984
985
986
987
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 984

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