gyro 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +42 -797
  3. data/bin/gyro +91 -84
  4. data/documentation/enum.png +0 -0
  5. data/documentation/enum_json.png +0 -0
  6. data/documentation/ignored.png +0 -0
  7. data/documentation/json.png +0 -0
  8. data/documentation/primary_key.png +0 -0
  9. data/documentation/read_only.png +0 -0
  10. data/documentation/simple_entity.png +0 -0
  11. data/documentation/support_annotation.png +0 -0
  12. data/documentation/transformer.png +0 -0
  13. data/documentation/transformers.png +0 -0
  14. data/lib/gyro.rb +18 -29
  15. data/lib/gyro/generator/json.rb +30 -0
  16. data/lib/gyro/generator/liquid.rb +22 -0
  17. data/lib/gyro/generator/liquid/filters.rb +53 -0
  18. data/lib/gyro/generator/liquid/liquid.rb +124 -0
  19. data/lib/gyro/generator/liquid/whitespace_patch.rb +61 -0
  20. data/lib/gyro/log.rb +46 -0
  21. data/lib/gyro/parser/xcdatamodel.rb +20 -0
  22. data/lib/gyro/parser/xcdatamodel/attribute.rb +129 -0
  23. data/lib/gyro/parser/xcdatamodel/entity.rb +195 -0
  24. data/lib/gyro/parser/xcdatamodel/relationship.rb +84 -0
  25. data/lib/gyro/parser/xcdatamodel/xcdatamodel.rb +65 -0
  26. data/lib/gyro/template.rb +68 -0
  27. data/lib/gyro/version.rb +19 -0
  28. data/lib/templates/android/README.md +164 -0
  29. data/lib/templates/android/entity.liquid +49 -0
  30. data/lib/templates/android/entity_filename.liquid +1 -0
  31. data/lib/templates/android/enum.liquid +44 -0
  32. data/lib/templates/android/enum_filename.liquid +1 -0
  33. data/lib/templates/android/inc/_attributes_enum.liquid +17 -0
  34. data/lib/templates/android/inc/_attributes_getter_setter.liquid +58 -0
  35. data/lib/templates/android/inc/_attributes_properties.liquid +35 -0
  36. data/lib/templates/android/inc/_enum_getter_setter.liquid +19 -0
  37. data/lib/templates/android/inc/_primitives.liquid +30 -0
  38. data/lib/templates/android/inc/_relationships_enum.liquid +14 -0
  39. data/lib/templates/android/inc/_relationships_getter_setter.liquid +47 -0
  40. data/lib/templates/android/inc/_relationships_properties.liquid +27 -0
  41. data/lib/templates/android/inc/_type_converter.liquid +22 -0
  42. data/lib/templates/android/inc/_wrapper_type_converter.liquid +22 -0
  43. data/lib/templates/decodable/README.md +34 -0
  44. data/lib/templates/decodable/entity.liquid +57 -0
  45. data/lib/templates/decodable/entity_filename.liquid +1 -0
  46. data/lib/templates/decodable/enum.liquid +0 -0
  47. data/lib/templates/decodable/enum_filename.liquid +0 -0
  48. data/lib/templates/object-mapper/README.md +40 -0
  49. data/lib/templates/object-mapper/entity.liquid +66 -0
  50. data/lib/templates/object-mapper/entity_filename.liquid +1 -0
  51. data/lib/templates/object-mapper/enum.liquid +0 -0
  52. data/lib/templates/object-mapper/enum_filename.liquid +0 -0
  53. data/lib/templates/swift3-variant/README.md +60 -0
  54. data/lib/templates/swift3-variant/entity.liquid +12 -0
  55. data/lib/templates/swift3-variant/entity_filename.liquid +1 -0
  56. data/lib/templates/swift3-variant/enum.liquid +15 -0
  57. data/lib/templates/swift3-variant/enum_filename.liquid +1 -0
  58. data/lib/templates/swift3-variant/inc/_attributes_enum.liquid +13 -0
  59. data/lib/templates/swift3-variant/inc/_attributes_properties.liquid +32 -0
  60. data/lib/templates/swift3-variant/inc/_default_value_converter.liquid +14 -0
  61. data/lib/templates/swift3-variant/inc/_enum_attribute_property.liquid +26 -0
  62. data/lib/templates/swift3-variant/inc/_ignored_properties.liquid +24 -0
  63. data/lib/templates/swift3-variant/inc/_indexed_properties.liquid +19 -0
  64. data/lib/templates/swift3-variant/inc/_inverse_properties.liquid +11 -0
  65. data/lib/templates/swift3-variant/inc/_optional_attribute_property.liquid +5 -0
  66. data/lib/templates/swift3-variant/inc/_primary_key.liquid +5 -0
  67. data/lib/templates/swift3-variant/inc/_relationship_properties.liquid +9 -0
  68. data/lib/templates/swift3-variant/inc/_relationships_enum.liquid +17 -0
  69. data/lib/templates/swift3-variant/inc/_type_converter.liquid +20 -0
  70. data/lib/templates/swift3/README.md +60 -0
  71. data/lib/templates/swift3/entity.liquid +12 -0
  72. data/lib/templates/swift3/entity_filename.liquid +1 -0
  73. data/lib/templates/swift3/enum.liquid +15 -0
  74. data/lib/templates/swift3/enum_filename.liquid +1 -0
  75. data/lib/templates/swift3/inc/_attributes_enum.liquid +13 -0
  76. data/lib/templates/swift3/inc/_attributes_properties.liquid +32 -0
  77. data/lib/templates/swift3/inc/_default_value_converter.liquid +14 -0
  78. data/lib/templates/swift3/inc/_enum_attribute_property.liquid +26 -0
  79. data/lib/templates/swift3/inc/_ignored_properties.liquid +24 -0
  80. data/lib/templates/swift3/inc/_indexed_properties.liquid +19 -0
  81. data/lib/templates/swift3/inc/_inverse_properties.liquid +11 -0
  82. data/lib/templates/swift3/inc/_optional_attribute_property.liquid +5 -0
  83. data/lib/templates/swift3/inc/_primary_key.liquid +5 -0
  84. data/lib/templates/swift3/inc/_relationship_properties.liquid +9 -0
  85. data/lib/templates/swift3/inc/_relationships_enum.liquid +17 -0
  86. data/lib/templates/swift3/inc/_type_converter.liquid +20 -0
  87. metadata +134 -31
  88. data/lib/gyro/realm/java/converter.rb +0 -63
  89. data/lib/gyro/realm/java/enum_generator.rb +0 -128
  90. data/lib/gyro/realm/java/generator.rb +0 -183
  91. data/lib/gyro/realm/java/templates.rb +0 -67
  92. data/lib/gyro/realm/objc/converter.rb +0 -63
  93. data/lib/gyro/realm/objc/enum_generator.rb +0 -86
  94. data/lib/gyro/realm/objc/generator.rb +0 -373
  95. data/lib/gyro/realm/objc/json_category_generator.rb +0 -172
  96. data/lib/gyro/realm/objc/protocol_generator.rb +0 -59
  97. data/lib/gyro/realm/objc/templates.rb +0 -100
  98. data/lib/gyro/realm/swift/converter.rb +0 -74
  99. data/lib/gyro/realm/swift/enum_generator.rb +0 -73
  100. data/lib/gyro/realm/swift/generator.rb +0 -265
  101. data/lib/gyro/realm/swift/object_mapper_generator.rb +0 -124
  102. data/lib/gyro/realm/swift/templates.rb +0 -64
  103. data/lib/gyro/utils/file_utils.rb +0 -31
  104. data/lib/gyro/utils/log.rb +0 -68
  105. data/lib/gyro/utils/raise.rb +0 -23
  106. data/lib/gyro/utils/string_xcdatamodel.rb +0 -58
  107. data/lib/gyro/xcdatamodel/parser/attribute.rb +0 -98
  108. data/lib/gyro/xcdatamodel/parser/entity.rb +0 -234
  109. data/lib/gyro/xcdatamodel/parser/relationship.rb +0 -70
  110. data/lib/gyro/xcdatamodel/parser/xcdatamodel.rb +0 -59
@@ -1,59 +0,0 @@
1
- =begin
2
- Copyright 2016 - Niji
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- =end
16
-
17
- require File.expand_path('templates', File.dirname(__FILE__))
18
- require File.expand_path('converter', File.dirname(__FILE__))
19
- require File.expand_path('../../utils/string_xcdatamodel', File.dirname(__FILE__))
20
- require File.expand_path('../../utils/file_utils', File.dirname(__FILE__))
21
-
22
- module Gyro
23
- module Realm
24
- module ObjC
25
- module ProtocolGenerator
26
-
27
- # INCLUDES #############################################################
28
-
29
- include Templates
30
- include Converter
31
-
32
- # PUBLIC METHODS #######################################################
33
-
34
- def generate_protocol_file(path, xcdatamodel)
35
- content = String.new
36
- xcdatamodel.entities.each do |_, entity|
37
- unless entity.abstract?
38
- if entity.used_as_list_by_other?(xcdatamodel.entities)
39
- content << protocol_file_template if content.empty?
40
- content << REALM_LIST_TYPE_TEMPLATE%[entity.name, entity.name] + "\n"
41
- end
42
- end
43
- end
44
- Gyro.write_file_with_name(path, HEADER_TEMPLATE%[PROTOCOL_FILE_NAME], content) unless content.empty?
45
- end
46
-
47
- private ################################################################
48
-
49
- def protocol_file_template
50
- content = String.new
51
- content << GENERATED_MESSAGE + "\n"
52
- content << "\n" + SEPARATOR + "\n\n"
53
- content << PRAGMA_MARK_PROTOCOLS + "\n\n"
54
- end
55
-
56
- end
57
- end
58
- end
59
- end
@@ -1,100 +0,0 @@
1
- =begin
2
- Copyright 2016 - Niji
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- =end
16
-
17
- module Gyro
18
- module Realm
19
- module ObjC
20
- module Templates
21
-
22
- # IMPORTS
23
- IMPORT_REALM = '#import <Realm/Realm.h>'
24
- IMPORT_HEADER = '#import "%s.h"'
25
- IMPORT_REALM_JSON_LIBRARY = '#import <Realm+JSON/RLMObject+JSON.h>'
26
- IMPORT_REALM_JSON_FRAMEWORK = '#import "RLMObject+JSON.h"'
27
-
28
-
29
- # PRAGMA
30
- PRAGMA_MARK_IMPORTS = '#pragma mark - Imports'
31
- PRAGMA_MARK_TYPES = '#pragma mark - Types'
32
- PRAGMA_MARK_CONSTANTS = '#pragma mark - Defines & Constants'
33
- PRAGMA_MARK_INTERFACE = '#pragma mark - Interface'
34
- PRAGMA_MARK_PROPERTIES = '#pragma mark - Properties'
35
- PRAGMA_MARK_PROTOCOLS = '#pragma mark - Protocols'
36
- PRAGMA_MARK_IMPLEMENTATION = '#pragma mark - Implementation'
37
- PRAGMA_MARK_NUMBER_ACCESSORS = '#pragma mark - NSNumber Convenience Accessors'
38
- PRAGMA_MARK_SUPER = '#pragma mark - Superclass Overrides'
39
-
40
- # COMMONS
41
- GENERATED_MESSAGE = '// DO NOT EDIT | Generated by gyro'
42
- END_CODE = '@end'
43
- SEPARATOR = '////////////////////////////////////////////////////////////////////////////////'
44
- DICTIONARY_JSON = '@%s : @%s,'
45
- DICTIONARY_DEFAULT = '@%s : %s,'
46
- DICTIONARY_JSON_CATEGORY = '%s : @%s,'
47
-
48
- # HEADER
49
- HEADER_TEMPLATE = '%s.h'
50
- NUMBER_TRANSFORMER_FILE_NAME = 'NFNumberTransformer'
51
- CLASS_COMMENT_TEMPLATE = "/**\n * %s\n */"
52
- CLASS_TEMPLATE = '@class %s;'
53
- INTERFACE_TEMPLATE = '@interface %s : RLMObject'
54
- PROPERTY_COMMENT_TEMPLATE = '/** %s */'
55
- SIMPLE_PROPERTY_TEMPLATE = '@property %s%s;'
56
- OBJECT_PROPERTY_TEMPLATE = '@property %s *%s;'
57
- NUMBER_ACCESSOR_DECL_TEMPLATES = "-(%s)%sValue;\n-(void)set%sValue:(%s)value;\n"
58
- LIST_TEMPLATE = 'NSArray<%s>'
59
- REALM_LIST_TYPE_TEMPLATE = "// This protocol enables typed collections. i.e.: RLMArray<%s>\nRLM_ARRAY_TYPE(%s)"
60
- REALM_LIST_TEMPLATE = 'RLMArray<%s>'
61
-
62
- # COMMONS
63
- CONSTANT_ATTRIBUTES_NAME = '%sAttributes'
64
- CONSTANT_RELATIONSHIPS_NAME = '%sRelationships'
65
- CONSTANT_HEADER_ATTRIBUTES = 'extern const struct %s {'
66
- CONSTANT_HEADER_RELATIONSHIPS = 'extern const struct %s {'
67
- CONSTANT_HEADER_ITEM = '__unsafe_unretained NSString *%s;'
68
- CONSTANT_SOURCE_ATTRIBUTES = 'const struct %s %s = {'
69
- CONSTANT_SOURCE_RELATIONSHIPS = 'const struct %s %s = {'
70
- CONSTANT_SOURCE_ITEM = '.%s = @"%s"'
71
-
72
- # ENUM
73
- ENUM_TYPEDEF_TEMPLATE = 'typedef NS_ENUM(%s, %s) {'
74
- ENUM_FILE_NAME = 'RLMTypes'
75
- PROTOCOL_FILE_NAME = 'RLMProtocols'
76
-
77
- # SOURCE
78
- SOURCE_TEMPLATE = '%s.m'
79
- IMPLEMENTATION_TEMPLATE = '@implementation %s'
80
- ARRAY_TEMPLATE ='@%s, '
81
- READ_ONLY_DEF_TEMPLATE = '- (%s)%s'
82
- NUMBER_ACCESSOR_SOURCE_TEMPLATES = "-(%s)%sValue\n{\n return [self.%s %s];\n}\n-(void)set%sValue:(%s)value\n{\n self.%s = @(value);\n}"
83
- INVERSE_DEF_TEMPLATE = '- (%s *)%s'
84
- INVERSE_MANY_TEMPLATE = 'return [self linkingObjectsOfClass:@"%s" forProperty:@"%s"];'
85
- INVERSE_ONE_TEMPLATE = 'return [[self linkingObjectsOfClass:@"%s" forProperty:@"%s"] objectAtIndex:0];'
86
-
87
- # JSON_CATEGORY
88
- JSON_CATEGORY_NAME = '%s+JSON'
89
- JSON_TRANSFORMER_DEF = '+ (NSValueTransformer *)%sJSONTransformer'
90
- JSON_CATEGORY_IMPLEMENTATION = '@implementation %s (JSON)'
91
- JSON_CATEGORY_INTERFACE = '@interface %s (JSON)'
92
- TRANSFORMER_BOOL_JSON = ['@"null"', '[NSNull null]', '@"<null>"', '@"false"', '@"true"', '@(NO)', '@(YES)']
93
- TRANSFORMER_BOOL_MODEL = %w(NO NO NO NO YES NO YES)
94
- TRANSFORMER_ENUM_JSON = ['@"null"', '[NSNull null]', '@"<null>"', '@""']
95
- TRANSFORMER = 'return [[%s alloc] init];'
96
-
97
- end
98
- end
99
- end
100
- end
@@ -1,74 +0,0 @@
1
- =begin
2
- Copyright 2016 - Niji
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- =end
16
-
17
- module Gyro
18
- module Realm
19
- module Swift
20
- module Converter
21
-
22
- TYPES = {
23
- :integer_16 => 'Int16',
24
- :integer_32 => 'Int32',
25
- :integer_64 => 'Int64',
26
- :decimal => 'Double',
27
- :double => 'Double',
28
- :float => 'Float',
29
- :string => 'String',
30
- :boolean => 'Bool',
31
- :date => 'NSDate',
32
- :binary => 'NSData'
33
- }
34
-
35
- DEFAULTS = {
36
- :integer_16 => '0',
37
- :integer_32 => '0',
38
- :integer_64 => '0',
39
- :decimal => '0.0',
40
- :double => '0.0',
41
- :float => '0.0',
42
- :string => '""',
43
- :boolean => 'false',
44
- :date => 'NSDate()',
45
- :binary => 'NSData()'
46
- }
47
-
48
- def convert_type(type)
49
- TYPES[type]
50
- end
51
-
52
- def convert_default(type, value = nil)
53
- if value.nil?
54
- return DEFAULTS[type]
55
- end
56
- # Do some conversions for some special types
57
- case [type, value]
58
- when [:boolean, 'YES']
59
- return 'true'
60
- when [:boolean, 'NO']
61
- return 'false'
62
- else
63
- return value
64
- end
65
- end
66
-
67
- def is_number?(type)
68
- type != :string && type != :date && type != :binary
69
- end
70
-
71
- end
72
- end
73
- end
74
- end
@@ -1,73 +0,0 @@
1
- =begin
2
- Copyright 2016 - Niji
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- =end
16
-
17
- require File.expand_path('templates', File.dirname(__FILE__))
18
- require File.expand_path('../../utils/string_xcdatamodel', File.dirname(__FILE__))
19
- require File.expand_path('../../utils/file_utils', File.dirname(__FILE__))
20
-
21
- module Gyro
22
- module Realm
23
- module Swift
24
- module EnumGenerator
25
-
26
- # INCLUDES #############################################################
27
-
28
- include Templates
29
-
30
- # PUBLIC METHODS #######################################################
31
-
32
- def generate_enums(path, attributes)
33
- enums = Array.new
34
- attributes.each do |_, attribute|
35
- if attribute.enum? and !enums.include?(attribute.enum_type)
36
- enum_type = attribute.enum_type.delete_objc_prefix
37
- enums.push(enum_type)
38
- generate_enum(path, enum_type, attribute.enum_values, attribute.json_values)
39
- end
40
- end
41
- end
42
-
43
- private ################################################################
44
-
45
- def generate_enum(path, enum_name, enum_values, raw_values)
46
- enum_file = String.new
47
- enum_file << GENERATED_MESSAGE + "\n\n"
48
- enum_file << ENUM_STRING_DEF_TEMPLATE%[enum_name] + "\n"
49
- raw_values = raw_values(enum_values, raw_values)
50
- if !enum_values.empty? and raw_values.length == enum_values.length
51
- (0..enum_values.length - 1).each { |idx|
52
- enum_value = enum_values[idx].delete_objc_prefix
53
- raw_value = raw_values[idx]
54
- enum_file << ' ' + ENUM_STRING_CASE_TEMPLATE%[enum_value, raw_value] + "\n"
55
- }
56
- enum_file << '}' + "\n"
57
- Gyro.write_file_with_name(path, SWIFT_FILE_TEMPLATE%[enum_name], enum_file)
58
- end
59
- end
60
-
61
- def raw_values(enum_values, raw_values)
62
- if raw_values.empty?
63
- raw_values = enum_values.map { |value|
64
- value.delete_objc_prefix.underscore
65
- }
66
- end
67
- raw_values
68
- end
69
-
70
- end
71
- end
72
- end
73
- end
@@ -1,265 +0,0 @@
1
- =begin
2
- Copyright 2016 - Niji
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- =end
16
-
17
- require File.expand_path('../../utils/log', File.dirname(__FILE__))
18
- require File.expand_path('../../utils/string_xcdatamodel', File.dirname(__FILE__))
19
- require File.expand_path('../../utils/file_utils', File.dirname(__FILE__))
20
- require File.expand_path('../../xcdatamodel/parser/relationship', File.dirname(__FILE__))
21
- require File.expand_path('converter', File.dirname(__FILE__))
22
- require File.expand_path('enum_generator', File.dirname(__FILE__))
23
- require File.expand_path('templates', File.dirname(__FILE__))
24
- require File.expand_path('object_mapper_generator', File.dirname(__FILE__))
25
-
26
- module Gyro
27
- module Realm
28
- module Swift
29
-
30
- class Generator
31
-
32
- # INCLUDES #############################################################
33
-
34
- include Gyro::XCDataModel::Parser
35
- include Converter
36
- include EnumGenerator
37
- include Templates
38
- include ObjectMapperGenerator
39
-
40
- # PUBLIC METHODS #######################################################
41
-
42
- def initialize(path, xcdatamodel, json = false)
43
- puts "\n"
44
- Gyro::Log::title('Swift Realm')
45
- xcdatamodel.entities.each do |_, entity|
46
- unless entity.abstract?
47
- Gyro::Log::success("Generating entity #{entity.name}...")
48
- generate_class(path, entity, json)
49
- generate_object_mapper_categories(path, xcdatamodel) if json
50
- end
51
- end
52
- end
53
-
54
- private ################################################################
55
-
56
- def generate_class(path, entity, json)
57
- class_file = String.new
58
- entity.name = entity.name.delete_objc_prefix
59
- class_file << generate_header(entity)
60
- class_file << generate_attributes(entity.attributes, entity.relationships, json)
61
- class_file << generate_inverse_properties(entity)
62
- class_file << generate_primary_key(entity)
63
- class_file << generate_indexed_properties(entity)
64
- class_file << generate_ignored_properties(entity)
65
- class_file << '}' + "\n"
66
- Gyro.write_file_with_name(path, SWIFT_FILE_TEMPLATE%[entity.name], class_file)
67
- generate_enums(path, entity.attributes)
68
- end
69
-
70
- def generate_header(entity)
71
- class_file = String.new
72
- class_file << GENERATED_MESSAGE + "\n\n"
73
- class_file << IMPORT_REALM + "\n\n"
74
- class_file << CLASS_COMMENT_TEMPLATE%[entity.comment] + "\n" unless entity.comment.empty?
75
- class_file << CLASS_TEMPLATE%[entity.name] + "\n\n"
76
- class_file << generate_constants(entity)
77
- end
78
-
79
- def generate_constants(entity)
80
- attribute_constants = String.new
81
- unless entity.attributes.empty?
82
- attribute_constants << ' ' + ENUM_STRING_DEF_TEMPLATE%['Attributes'] + "\n"
83
- entity.attributes.each do |_, attribute|
84
- unless attribute.realm_ignored? or attribute.read_only?
85
- attribute_constants << ' ' + ATTRIBUTE_COMMENT_TEMPLATE%[attribute.comment] + "\n" unless attribute.comment.empty?
86
- attribute_constants << ' ' + ENUM_STRING_CASE_TEMPLATE%[attribute.name.capitalize_first_letter, attribute.name] + "\n"
87
- end
88
- end
89
- attribute_constants << ' ' + '}'+ "\n\n"
90
- end
91
- relationship_constants = String.new
92
- if not entity.relationships.empty? and not entity.has_only_inverse?
93
- relationship_constants << ' ' + ENUM_STRING_DEF_TEMPLATE%['Relationships'] + "\n"
94
- entity.relationships.each do |_, relationship|
95
- relationship_constants << ' ' + ENUM_STRING_CASE_TEMPLATE%[relationship.name.capitalize_first_letter, relationship.name] + "\n" if not relationship.inverse?
96
- end
97
- relationship_constants << ' ' + '}'+ "\n\n"
98
- end
99
- attribute_constants + relationship_constants
100
- end
101
-
102
- def generate_attributes(attributes, relationships, json)
103
- # "NORMAL" ATTRIBUTES
104
- attributes_string = write_attributes(attributes, json)
105
- # "RELATIONSHIP" ATTRIBUTES
106
- relationships.each do |_, relationship|
107
- unless relationship.inverse?
108
- is_list = relationship.type == :to_many
109
- name = relationship.name
110
- type = relationship.inverse_type.delete_objc_prefix
111
- if is_list
112
- if json
113
- attributes_string << ' ' + PROPERTY_LIST_VAR_TEMPLATE%[name, type] + "\n"
114
- else
115
- attributes_string << ' ' + PROPERTY_LIST_TEMPLATE%[name, type] + "\n"
116
- end
117
- else
118
- attributes_string << ' ' + PROPERTY_OBJECT_TEMPLATE%[name, type] + "\n"
119
- end
120
- end
121
- end
122
- attributes_string + "\n"
123
- end
124
-
125
- def write_attributes(attributes, json)
126
- attributes_string = String.new
127
- attributes.each_with_index do |(_, attribute)|
128
- unless attribute.read_only?
129
- if attribute.enum?
130
- attributes_string << write_enum_attribute(attribute, json)
131
- else
132
- if attribute.optional?
133
- attributes_string << write_optional_attribute(attribute, json) + "\n"
134
- else
135
- default_value = convert_default(attribute.type, attribute.has_default? ? attribute.default : nil)
136
- attributes_string << ' ' + PROPERTY_DEFAULT_TEMPLATE%[attribute.name, convert_type(attribute.type), default_value] + "\n"
137
- end
138
- end
139
- end
140
- end
141
- attributes_string
142
- end
143
-
144
- def write_optional_attribute(attribute, json)
145
- optional_string = String.new
146
- type = convert_type(attribute.type)
147
- if attribute.is_number? or attribute.is_bool?
148
- if json
149
- optional_string << ' ' + PROPERTY_OPTIONAL_NUMBER_VAR_TEMPLATE%[attribute.name, type]
150
- else
151
- optional_string << ' ' + PROPERTY_OPTIONAL_NUMBER_TEMPLATE%[attribute.name, type]
152
- end
153
- else
154
- optional_string << ' ' + PROPERTY_OPTIONAL_NON_NUMBER_TEMPLATE%[attribute.name, type]
155
- end
156
- optional_string
157
- end
158
-
159
- def write_enum_attribute(attribute, json)
160
- enum_string = String.new
161
- if attribute.optional?
162
- enum_string << ' ' + PROPERTY_OPTIONAL_ENUM_TEMPLATE%[attribute.name] + "\n"
163
- else
164
- enum_string << ' ' + PROPERTY_ENUM_TEMPLATE%[attribute.name] + "\n"
165
- end
166
- enum_type = attribute.enum_type.delete_objc_prefix
167
- enum_name = attribute.name + 'Enum'
168
-
169
- enum_string << ' ' + PROPERTY_OPTIONAL_COMPUTED_TEMPLATE%[enum_name, enum_type] + "\n"
170
- enum_string << ' ' + 'get {' + "\n"
171
- if attribute.optional?
172
- enum_string << ' ' + "guard let #{attribute.name} = #{attribute.name},\n"
173
- enum_string << ' ' + " let enumValue = #{enum_type}(rawValue: #{attribute.name})\n"
174
- enum_string << ' ' + " else { return nil }" + "\n"
175
- else
176
- enum_string << ' ' + "guard let enumValue = #{enum_type}(rawValue: #{attribute.name}) else { return nil }" + "\n"
177
- end
178
- enum_string << ' ' + "return enumValue" + "\n"
179
- enum_string << ' ' + '}' + "\n"
180
-
181
- if attribute.optional?
182
- enum_string << ' ' + "set { #{attribute.name} = newValue?.rawValue ?? nil }" + "\n"
183
- else
184
- enum_string << ' ' + "set { #{attribute.name} = newValue?.rawValue ?? \"\" }" + "\n"
185
- end
186
- enum_string << ' ' + '}' + "\n\n"
187
- end
188
-
189
- def generate_primary_key(entity)
190
- primary_key = String.new
191
- if entity.has_primary_key?
192
- primary_key << ' ' + 'override static func primaryKey() -> String? {' + "\n"
193
- primary_key << ' ' + "return #{entity.identity_attribute.add_quotes}" + "\n"
194
- primary_key << ' ' + '}' + "\n\n"
195
- end
196
- primary_key
197
- end
198
-
199
- def generate_ignored_properties(entity)
200
- ignored_properties = String.new
201
- if entity.has_ignored?
202
- ignored_properties << ' ' + "// Specify properties to ignore (Realm won't persist these)" + "\n"
203
- ignored_properties << ' ' +'override static func ignoredProperties() -> [String] {' + "\n"
204
- ignored_properties << ' ' + 'return ['
205
- entity.attributes.each do |_, attribute|
206
- ignored_properties << ARRAY_TEMPLATE%[attribute.name.add_quotes] if attribute.realm_ignored?
207
- end
208
- entity.relationships.each do |_, relationship|
209
- ignored_properties << ARRAY_TEMPLATE%[relationship.name.add_quotes] if relationship.realm_ignored?
210
- end
211
- ignored_properties = ignored_properties[0..ignored_properties.length - 3] # delete last coma
212
- ignored_properties << ']' + "\n"
213
- ignored_properties << ' ' + '}' + "\n\n"
214
- end
215
- ignored_properties
216
- end
217
-
218
- def generate_inverse_properties(entity)
219
- inverse_properties = []
220
- entity.relationships.each do |_, relationship|
221
- if relationship.inverse?
222
- if relationship.type == :to_many
223
- inverse_properties << PROPERTY_INVERSE_MANY_TEMPLATE%[
224
- relationship.name.delete_inverse_suffix,
225
- relationship.inverse_type.delete_objc_prefix,
226
- relationship.inverse_name
227
- ]
228
- else
229
- inverse_properties << PROPERTY_INVERSE_MANY_TEMPLATE%[
230
- relationship.name.delete_inverse_suffix + 's', # 's' for plural form
231
- relationship.inverse_type.delete_objc_prefix,
232
- relationship.inverse_name
233
- ]
234
- inverse_properties << PROPERTY_INVERSE_ONE_TEMPLATE%[
235
- relationship.name.delete_inverse_suffix,
236
- relationship.inverse_type.delete_objc_prefix,
237
- relationship.name.delete_inverse_suffix + 's' # 's' for plural form
238
- ]
239
- end
240
- end
241
- end
242
- return '' if inverse_properties.empty?
243
- inverse_properties.map { |value| " #{value}\n" }.join + "\n"
244
- end
245
-
246
- def generate_indexed_properties(entity)
247
- indexed_properties = String.new
248
- if entity.has_indexed_attributes?
249
- indexed_properties << ' ' + '// Specify properties to index' + "\n"
250
- indexed_properties << ' ' +'override static func indexedProperties() -> [String] {' + "\n"
251
- indexed_properties << ' ' + 'return ['
252
- entity.attributes.each do |_, attribute|
253
- indexed_properties << ARRAY_TEMPLATE%[attribute.name.add_quotes] if attribute.indexed?
254
- end
255
- indexed_properties = indexed_properties[0..indexed_properties.length - 3] # delete last coma
256
- indexed_properties << ']' + "\n"
257
- indexed_properties << '}' + "\n\n"
258
- end
259
- indexed_properties
260
- end
261
-
262
- end
263
- end
264
- end
265
- end