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,373 +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('templates', File.dirname(__FILE__))
23
- require File.expand_path('protocol_generator', File.dirname(__FILE__))
24
- require File.expand_path('enum_generator', File.dirname(__FILE__))
25
- require File.expand_path('json_category_generator', File.dirname(__FILE__))
26
-
27
- module Gyro
28
- module Realm
29
- module ObjC
30
-
31
- class Generator
32
-
33
- # INCLUDES #############################################################
34
-
35
- include Gyro::XCDataModel::Parser
36
- include Converter
37
- include Templates
38
- include ProtocolGenerator
39
- include EnumGenerator
40
- include JSONCategoryGenerator
41
-
42
- # PUBLIC METHODS #######################################################
43
-
44
- def initialize(path, xcdatamodel, json = false, framework = false, use_nsnumber = false)
45
- generate_class_files(path, xcdatamodel, use_nsnumber)
46
- generate_protocol_file(path, xcdatamodel)
47
- generate_enum_file(path, xcdatamodel)
48
- generate_objc_categories(path, xcdatamodel, framework) if json
49
- end
50
-
51
- private ################################################################
52
-
53
- def generate_class_files(path, xcdatamodel, use_nsnumber)
54
- puts "\n"
55
- Gyro::Log::title('Objc Realm')
56
- xcdatamodel.entities.each do |_, entity|
57
- unless entity.abstract?
58
- Gyro::Log::success("Generating entity #{entity.name}...")
59
- generate_class(path, entity, use_nsnumber)
60
- end
61
- end
62
- end
63
-
64
- def generate_class(path, entity, use_nsnumber)
65
- header_file = generate_header_file(entity, use_nsnumber)
66
- source_file = generate_source_file(entity, use_nsnumber)
67
- Gyro.write_file_with_name(path, HEADER_TEMPLATE%[entity.name], header_file)
68
- Gyro.write_file_with_name(path, SOURCE_TEMPLATE%[entity.name], source_file)
69
- end
70
-
71
- def generate_header_file(entity, use_nsnumber)
72
- header_file = String.new
73
- header_file << GENERATED_MESSAGE + "\n"
74
- header_file << "\n" + SEPARATOR + "\n\n"
75
- header_file << PRAGMA_MARK_IMPORTS + "\n\n"
76
- header_file << IMPORT_REALM + "\n"
77
- header_file << IMPORT_HEADER%[ENUM_FILE_NAME] + "\n" if require_enum_import(entity)
78
- header_file << generate_import_protocols(entity)
79
- header_file << generate_class_types(entity)
80
- header_file << generate_header_constants(entity)
81
- header_file << "\n" + SEPARATOR + "\n\n"
82
- header_file << PRAGMA_MARK_INTERFACE + "\n\n"
83
- header_file << CLASS_COMMENT_TEMPLATE%[entity.comment] + "\n" unless entity.comment.empty?
84
- header_file << INTERFACE_TEMPLATE%[entity.name] + "\n"
85
- header_file << generate_properties(entity, use_nsnumber)
86
- header_file << "\n" << END_CODE + "\n"
87
- end
88
-
89
- def generate_properties(entity, use_nsnumber)
90
- header_file = String.new
91
- header_file << "\n" + PRAGMA_MARK_PROPERTIES + "\n\n"
92
- entity.attributes.each do |_, attribute|
93
- header_file << PROPERTY_COMMENT_TEMPLATE%[attribute.comment] + "\n" unless attribute.comment.empty?
94
- name = attribute.name
95
- use_nsnumber_wrapper = use_nsnumber && require_nsnumber_wrapper(attribute)
96
- type = attribute.enum? ? attribute.enum_type : convert_type(attribute.type, use_nsnumber_wrapper)
97
- if type.nil?
98
- Gyro::Log::error("The property #{name} of entity #{entity.name} has an Undefined type.")
99
- type = 'id'
100
- end
101
- type = '(readonly) ' + type unless attribute.realm_read_only.empty?
102
- header_file << SIMPLE_PROPERTY_TEMPLATE%[type, type.end_with?('*') ? name : ' ' + name] + "\n"
103
- if use_nsnumber_wrapper
104
- num_type = convert_type(attribute.type)
105
- header_file << NUMBER_ACCESSOR_DECL_TEMPLATES%[num_type, name, name.capitalize_first_letter, num_type]
106
- end
107
- end
108
- entity.relationships.each do |_, relationship|
109
- is_list = relationship.type == :to_many
110
- if relationship.inverse?
111
- type = '(readonly) ' + (is_list ? 'NSArray' : relationship.inverse_type)
112
- name = relationship.name.delete_inverse_suffix
113
- else
114
- if relationship.destination.empty?
115
- type = is_list ? REALM_LIST_TEMPLATE%[relationship.inverse_type] : relationship.inverse_type
116
- else
117
- type = LIST_TEMPLATE%[convert_type(relationship.destination.downcase.to_sym)]
118
- end
119
- name = relationship.name
120
- end
121
- header_file << OBJECT_PROPERTY_TEMPLATE%[type, name] + "\n"
122
- end
123
- header_file
124
- end
125
-
126
- def require_nsnumber_wrapper(attribute)
127
- !attribute.enum? && attribute.realm_read_only.empty? && is_number_type?(attribute.type)
128
- end
129
-
130
- def require_enum_import(entity)
131
- entity.attributes.each do |_, attribute|
132
- if attribute.enum?
133
- return true
134
- end
135
- end
136
- false
137
- end
138
-
139
- def generate_import_protocols(entity)
140
- entity.has_list_attributes?(true) ? IMPORT_HEADER%[PROTOCOL_FILE_NAME] + "\n" : ''
141
- end
142
-
143
- def generate_class_types(entity)
144
- class_types = String.new
145
- entity.relationships.each do |_, relationship|
146
- class_types << CLASS_TEMPLATE%[relationship.inverse_type] + "\n" if relationship.inverse_type != entity.name && relationship.destination.empty?
147
- end
148
- class_types.empty? ? class_types : "\n" + PRAGMA_MARK_TYPES + "\n\n" + class_types
149
- end
150
-
151
- def generate_header_constants(entity)
152
- constants = String.new
153
- unless entity.attributes.empty?
154
- name = CONSTANT_ATTRIBUTES_NAME%[entity.name]
155
- constants << CONSTANT_HEADER_ATTRIBUTES%[name, name] + "\n"
156
- entity.attributes.each do |_, attribute|
157
- constants << ' ' + CONSTANT_HEADER_ITEM%[attribute.name] + "\n"
158
- end
159
- constants << "} #{name};\n"
160
- end
161
- if entity.has_no_inverse_relationship?
162
- constants << "\n" unless constants.empty?
163
- name = CONSTANT_RELATIONSHIPS_NAME%[entity.name]
164
- constants << CONSTANT_HEADER_RELATIONSHIPS%[name] + "\n"
165
- entity.relationships.each do |_, relationship|
166
- constants << ' ' + CONSTANT_HEADER_ITEM%[relationship.name] + "\n" unless relationship.inverse?
167
- end
168
- constants << "} #{name};\n"
169
- end
170
- constants.empty? ? constants : "\n" + PRAGMA_MARK_CONSTANTS + "\n\n"+ constants
171
- end
172
-
173
- def generate_source_file(entity, use_nsnumber)
174
- source_file = String.new
175
- source_file << GENERATED_MESSAGE + "\n"
176
- source_file << "\n" + SEPARATOR + "\n\n"
177
- source_file << PRAGMA_MARK_IMPORTS + "\n\n"
178
- source_file << IMPORT_HEADER%[entity.name] + "\n"
179
- source_file << generate_source_constants(entity)
180
- source_file << "\n" + SEPARATOR + "\n\n"
181
- source_file << PRAGMA_MARK_IMPLEMENTATION + "\n\n"
182
- source_file << IMPLEMENTATION_TEMPLATE%[entity.name] + "\n"
183
- source_file << generate_numbers_accessors(entity) if use_nsnumber
184
- if require_overriding(entity)
185
- source_file << "\n" + PRAGMA_MARK_SUPER + "\n"
186
- source_file << generate_primary_key(entity)
187
- source_file << generate_required_properties(entity)
188
- source_file << generate_default_values(entity)
189
- source_file << generate_ignored_properties(entity)
190
- source_file << generate_read_only_properties(entity, use_nsnumber)
191
- source_file << generate_inverse_properties(entity)
192
- end
193
- source_file << "\n" << END_CODE + "\n"
194
- end
195
-
196
- def generate_source_constants(entity)
197
- constants = String.new
198
- unless entity.attributes.empty?
199
- name = CONSTANT_ATTRIBUTES_NAME%[entity.name]
200
- constants << CONSTANT_SOURCE_ATTRIBUTES%[name, name] + "\n"
201
- entity.attributes.each_with_index do |(_, attribute), idx|
202
- constants << ',' + "\n" unless idx == 0
203
- constants << ' ' + CONSTANT_SOURCE_ITEM%[attribute.name, attribute.name]
204
- constants << "\n" if idx == entity.attributes.length - 1
205
- end
206
- constants << '};' + "\n"
207
- end
208
- if entity.has_no_inverse_relationship?
209
- constants << "\n" unless constants.empty?
210
- name = CONSTANT_RELATIONSHIPS_NAME%[entity.name]
211
- constants << CONSTANT_SOURCE_RELATIONSHIPS%[name, name] + "\n"
212
- has_first = false
213
- entity.relationships.each_with_index do |(_, relationship), idx|
214
- unless relationship.inverse?
215
- constants << ',' + "\n" if has_first
216
- constants << ' ' + CONSTANT_SOURCE_ITEM%[relationship.name, relationship.name]
217
- has_first = true
218
- end
219
- constants << "\n" if idx == entity.relationships.length - 1
220
- end
221
- constants << '};' + "\n"
222
- end
223
- constants.empty? ? constants : "\n" + PRAGMA_MARK_CONSTANTS + "\n\n" + constants
224
- end
225
-
226
- def generate_numbers_accessors(entity)
227
- number_accessors = String.new
228
- entity.attributes.each do |_, attribute|
229
- if require_nsnumber_wrapper(attribute)
230
- type = convert_type(attribute.type)
231
- name = attribute.name
232
- selector = type.gsub(/ /, '') + 'Value'
233
- number_accessors << NUMBER_ACCESSOR_SOURCE_TEMPLATES%[type, name, name, selector, name.capitalize_first_letter, type, name] + "\n"
234
- end
235
- end
236
- number_accessors.empty? ? "" : "\n" + PRAGMA_MARK_NUMBER_ACCESSORS + "\n\n" + number_accessors
237
- end
238
-
239
- def require_overriding(entity)
240
- if entity.has_primary_key? or entity.attributes.count > 0
241
- return true
242
- end
243
- false
244
- end
245
-
246
- def generate_primary_key(entity)
247
- primary_key = String.new
248
- if entity.has_primary_key?
249
- primary_key << "\n" + '+ (NSString *)primaryKey' + "\n"
250
- primary_key << '{' + "\n"
251
- primary_key << ' ' + "return @\"" + entity.identity_attribute + "\";" + "\n"
252
- primary_key << '}' + "\n"
253
- end
254
- primary_key
255
- end
256
-
257
- def generate_required_properties(entity)
258
- required_properties = String.new
259
- if entity.has_required?
260
- required_properties << "\n" + '// Specify required properties' + "\n"
261
- required_properties << '+ (NSArray *)requiredProperties' + "\n"
262
- required_properties << '{' + "\n"
263
- required_properties << ' ' + 'return @['
264
- entity.attributes.each do |_, attribute|
265
- required_properties << ARRAY_TEMPLATE%[attribute.name.add_quotes] if entity.is_required?(attribute)
266
- end
267
- required_properties = required_properties[0..required_properties.length - 3] # delete last coma
268
- required_properties << '];' + "\n"
269
- required_properties << '}' + "\n"
270
- end
271
- required_properties
272
- end
273
-
274
- def generate_default_values(entity)
275
- default_values = String.new
276
- if entity.has_required?
277
- default_values << "\n" + '// Specify default values for required properties' + "\n"
278
- default_values << '+ (NSDictionary *)defaultPropertyValues' + "\n"
279
- default_values << '{' + "\n"
280
- default_values << ' ' + 'return @{'
281
- entity.attributes.each do |_, attribute|
282
- if entity.is_required?(attribute)
283
- default_value = convert_default(attribute.type)
284
- if entity.has_default_value?(attribute) && !attribute.default.empty?
285
- default_value = attribute.type == :string ? "@\"#{attribute.default})\"" : "@(#{attribute.default})"
286
- end
287
- default_values << DICTIONARY_DEFAULT%[attribute.name.add_quotes, default_value] + ' '
288
- end
289
- end
290
- default_values = default_values[0..default_values.length - 3] # delete last coma
291
- default_values << '};' + "\n"
292
- default_values << '}' + "\n"
293
- end
294
- default_values
295
- end
296
-
297
- def generate_ignored_properties(entity)
298
- ignored_properties = String.new
299
- if entity.has_ignored?
300
- ignored_properties << "\n" + "// Specify properties to ignore (Realm won't persist these)" + "\n"
301
- ignored_properties << '+ (NSArray *)ignoredProperties' + "\n"
302
- ignored_properties << '{' + "\n"
303
- ignored_properties << ' ' + 'return @['
304
- entity.attributes.each do |_, attribute|
305
- ignored_properties << ARRAY_TEMPLATE%[attribute.name.add_quotes] if attribute.realm_ignored?
306
- end
307
- entity.relationships.each do |_, relationship|
308
- ignored_properties << ARRAY_TEMPLATE%[relationship.name.add_quotes] if relationship.realm_ignored?
309
- end
310
- ignored_properties = ignored_properties[0..ignored_properties.length - 3] # delete last coma
311
- ignored_properties << '];' + "\n"
312
- ignored_properties << '}' + "\n"
313
- end
314
- ignored_properties
315
- end
316
-
317
- def generate_read_only_properties(entity, use_nsnumber)
318
- read_only_properties = String.new
319
- entity.attributes.each do |_, attribute|
320
- unless attribute.realm_read_only.empty?
321
- type = attribute.enum? ? attribute.enum_type : convert_type(attribute.type, use_nsnumber)
322
- read_only_properties << "\n" + READ_ONLY_DEF_TEMPLATE%[type, attribute.name] + "\n"
323
- read_only_properties << '{' + "\n"
324
- read_only_properties << ' ' + attribute.realm_read_only + "\n"
325
- read_only_properties << '}' + "\n"
326
- end
327
- end
328
- read_only_properties
329
- end
330
-
331
- def generate_inverse_properties(entity)
332
- inverse_properties = String.new
333
- entity.relationships.each do |_, relationship|
334
- if relationship.inverse?
335
- if relationship.type == :to_many
336
- definition = INVERSE_DEF_TEMPLATE%['NSArray', relationship.name.delete_inverse_suffix]
337
- value = INVERSE_MANY_TEMPLATE%[relationship.inverse_type, relationship.inverse_name]
338
- else
339
- definition = INVERSE_DEF_TEMPLATE%[relationship.inverse_type, relationship.name.delete_inverse_suffix]
340
- value = INVERSE_ONE_TEMPLATE%[relationship.inverse_type, relationship.inverse_name]
341
- end
342
- inverse_properties << "\n" + definition + "\n"
343
- inverse_properties << '{' + "\n"
344
- inverse_properties << ' ' + value + "\n"
345
- inverse_properties << '}' + "\n"
346
- end
347
- end
348
- inverse_properties
349
- end
350
-
351
- def generate_indexed_properties(entity)
352
- indexed_properties = String.new
353
- if entity.has_indexed_attributes?
354
- indexed_properties << "\n" + '// Specify properties to index' + "\n"
355
- indexed_properties << '+ (NSArray *)ignoredProperties' + "\n"
356
- indexed_properties << '{' + "\n"
357
- indexed_properties << ' ' + 'return @['
358
- entity.attributes.each do |_, attribute|
359
- if attribute.indexed
360
- indexed_properties << ARRAY_TEMPLATE%[attribute.name.add_quotes]
361
- end
362
- end
363
- indexed_properties = indexed_properties[0..indexed_properties.length - 3] # delete last coma
364
- indexed_properties << '];' + "\n"
365
- indexed_properties << '}' + "\n"
366
- end
367
- indexed_properties
368
- end
369
-
370
- end
371
- end
372
- end
373
- end
@@ -1,172 +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/file_utils', File.dirname(__FILE__))
19
- require File.expand_path('../../utils/string_xcdatamodel', File.dirname(__FILE__))
20
-
21
- module Gyro
22
- module Realm
23
- module ObjC
24
- module JSONCategoryGenerator
25
-
26
- # INCLUDES #############################################################
27
-
28
- include Templates
29
-
30
- # PUBLIC METHODS #######################################################
31
-
32
- def generate_objc_categories(path, xcdatamodel, framework = false)
33
- json_path = File.join(path, 'JSON')
34
- Dir.mkdir(json_path) unless Dir.exists?(json_path)
35
- xcdatamodel.entities.each do |_, entity|
36
- generate_json_category_file(json_path, entity, framework)
37
- end
38
- end
39
-
40
- private #################################################################
41
-
42
- def generate_json_category_file(path, entity, framework)
43
- if !entity.attributes.empty? || !entity.relationships.empty?
44
- source_file = generate_source_category_file(entity)
45
- header_file = generate_header_category_file(entity, framework)
46
- file_name = JSON_CATEGORY_NAME%[entity.name]
47
- Gyro.write_file_with_name(path, HEADER_TEMPLATE%[file_name], header_file) unless header_file.empty?
48
- Gyro.write_file_with_name(path, SOURCE_TEMPLATE%[file_name], source_file) unless source_file.empty?
49
- end
50
- end
51
-
52
- def generate_header_category_file(entity, framework)
53
- header_file = String.new
54
- header_file << GENERATED_MESSAGE + "\n"
55
- header_file << "\n" + SEPARATOR + "\n\n"
56
- header_file << PRAGMA_MARK_IMPORTS + "\n\n"
57
- header_file << (framework ? IMPORT_REALM_JSON_FRAMEWORK : IMPORT_REALM_JSON_LIBRARY) + "\n"
58
- header_file << IMPORT_HEADER%[entity.name] + "\n"
59
- header_file << "\n" + SEPARATOR + "\n\n"
60
- header_file << PRAGMA_MARK_INTERFACE + "\n\n"
61
- header_file << JSON_CATEGORY_INTERFACE%[entity.name] + "\n\n"
62
- header_file << END_CODE + "\n"
63
- header_file
64
- end
65
-
66
- def generate_source_category_file(entity)
67
- source_file = String.new
68
- source_file << GENERATED_MESSAGE + "\n"
69
- source_file << "\n" + SEPARATOR + "\n\n"
70
- source_file << PRAGMA_MARK_IMPORTS + "\n\n"
71
- source_file << IMPORT_HEADER%[JSON_CATEGORY_NAME%[entity.name]] + "\n"
72
- source_file << IMPORT_HEADER%[ENUM_FILE_NAME] + "\n" if entity.has_enum_attributes?
73
- entity.transformers.each do |name|
74
- source_file << IMPORT_HEADER%[name]+"\n"
75
- end
76
- source_file << "\n" + SEPARATOR + "\n\n"
77
- source_file << PRAGMA_MARK_IMPLEMENTATION + "\n\n"
78
- source_file << JSON_CATEGORY_IMPLEMENTATION%[entity.name] + "\n\n"
79
- source_file << generate_mapping(entity)
80
- source_file << "\n" + generate_json_transformers(entity) if entity.need_transformer?
81
- source_file << "\n" + END_CODE + "\n"
82
- source_file
83
- end
84
-
85
- def generate_mapping(entity)
86
- inbound_mapping_string = '+ (NSDictionary *)JSONInboundMappingDictionary' + "\n"
87
- inbound_mapping_string << '{' + "\n"
88
- inbound_mapping_string << ' ' + 'return @{' + "\n"
89
- outbound_mapping_string = '+ (NSDictionary *)JSONOutboundMappingDictionary' + "\n"
90
- outbound_mapping_string << '{' + "\n"
91
- outbound_mapping_string << ' '+ 'return @{' + "\n"
92
-
93
- entity.attributes.each do |_, attribute|
94
- json_value = attribute.json_key_path.empty? ? attribute.name.add_quotes : attribute.json_key_path.add_quotes
95
- inbound_mapping_string << ' ' + DICTIONARY_JSON%[json_value, attribute.name.add_quotes] + "\n"
96
- outbound_mapping_string << ' ' + DICTIONARY_JSON%[attribute.name.add_quotes, json_value] + "\n"
97
- end
98
-
99
- entity.relationships.each do |_, relationship|
100
- json_value = relationship.json_key_path.empty? ? relationship.name.add_quotes : relationship.json_key_path.add_quotes
101
- inbound_mapping_string << ' ' + DICTIONARY_JSON%[json_value, relationship.name.add_quotes] + "\n"
102
- outbound_mapping_string << ' ' + DICTIONARY_JSON%[relationship.name.add_quotes, json_value] + "\n"
103
- end
104
-
105
- #delete last coma
106
- inbound_mapping_string = inbound_mapping_string[0..inbound_mapping_string.length - 3] + "\n"
107
- inbound_mapping_string << ' ' +'};' + "\n"
108
- inbound_mapping_string << '}' + "\n\n"
109
- outbound_mapping_string = outbound_mapping_string[0..outbound_mapping_string.length - 3] + "\n"
110
- outbound_mapping_string << ' ' + '};' + "\n"
111
- outbound_mapping_string << '}' + "\n"
112
- inbound_mapping_string + outbound_mapping_string
113
- end
114
-
115
- def generate_json_transformers(entity)
116
- json_transformer_string = String.new
117
- first_transformer = true
118
- entity.attributes.each do |(_, attribute)|
119
- if attribute.need_transformer?
120
- json_transformer_string << "\n" unless first_transformer
121
- first_transformer = false
122
- json_transformer_string << JSON_TRANSFORMER_DEF%[attribute.name] + "\n"
123
- json_transformer_string << '{'+ "\n"
124
- if !attribute.enum_type.empty? or attribute.type == :boolean # Enum | Boolean
125
- json_transformer_string << ' ' + 'return [MCJSONValueTransformer valueTransformerWithMappingDictionary:@{' + "\n"
126
- if attribute.type == :boolean
127
- json_values = TRANSFORMER_BOOL_JSON
128
- model_values = TRANSFORMER_BOOL_MODEL
129
- else
130
- if attribute.json_values.empty?
131
- Raise::error("The attribute \"%s\" from \"%s\" is enum without JSONValues - please fix it"%[attribute.name, attribute.entity_name])
132
- end
133
- json_values = TRANSFORMER_ENUM_JSON + attribute.json_values.map { |enum| '@' + enum.add_quotes }
134
- enums = attribute.enum_values
135
- if attribute.optional?
136
- default = attribute.enum_type + 'None'
137
- else
138
- default = enums[attribute.default.to_i]
139
- end
140
- model_values = [default, default, default] + enums
141
- model_values = [default, default, default, default] + enums
142
- end
143
- json_transformer_string << format_json_transformers(json_values, model_values)
144
-
145
- # delete last coma
146
- json_transformer_string = json_transformer_string[0..json_transformer_string.length - 3] + "\n"
147
- json_transformer_string << ' ' + '}];' + "\n"
148
- else # custom transformer, or a default one
149
- transformer = attribute.transformer
150
- transformer = 'ISO8601DateTransform' if transformer.empty? && attribute.type == :date # default one for dates if none provided
151
- json_transformer_string << ' ' + TRANSFORMER%[transformer] + "\n" unless transformer.empty?
152
- end
153
- json_transformer_string << '}' + "\n"
154
- end
155
- end
156
- json_transformer_string
157
- end
158
-
159
- def format_json_transformers(json_values, model_values)
160
- json_transformer_string = String.new
161
- (0..model_values.length - 1).each { |enum|
162
- json_value = json_values[enum]
163
- enum_value = model_values[enum].add_parentheses
164
- json_transformer_string << ' ' + DICTIONARY_JSON_CATEGORY%[json_value, enum_value] + "\n"
165
- }
166
- json_transformer_string
167
- end
168
-
169
- end
170
- end
171
- end
172
- end