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,128 +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 Java
24
- module EnumGenerator
25
-
26
- # INCLUDES #############################################################
27
- include Templates
28
-
29
- # PUBLIC METHODS #######################################################
30
-
31
- def generate_enums(path, package, attributes, support_annotations = false)
32
- enums = Array.new
33
- attributes.each do |_, attribute|
34
- if attribute.enum? and !enums.include?(attribute.enum_type)
35
- enum_type = attribute.enum_type.delete_objc_prefix
36
- enums.push(enum_type)
37
- generate_enum(path, package, enum_type, attribute.enum_values, attribute.json_values, support_annotations)
38
- end
39
- end
40
- end
41
-
42
- def generate_enum_getter_and_setter(enum_type, attribute_name, support_annotations)
43
- getter = String.new
44
- setter = String.new
45
- getter << ' ' + '@android.support.annotation.Nullable' + "\n" if support_annotations
46
- getter << ' public ' + enum_type + ' get'+ attribute_name.capitalize_first_letter + 'Enum() {' + "\n" +
47
- ' ' + 'return '+ enum_type + '.get(get' + attribute_name.capitalize_first_letter + '());' + "\n" +
48
- ' ' + '}' + "\n"
49
- setter << ' ' + 'public void set'+ attribute_name.capitalize_first_letter + 'Enum('
50
- setter << '@android.support.annotation.NonNull ' if support_annotations
51
- setter << 'final ' + enum_type + ' ' + attribute_name +') {' + "\n" +
52
- ' ' + 'this.' + attribute_name + ' = ' + attribute_name + '.getJsonValue();' + "\n" +
53
- ' ' + '}' + "\n"
54
- getter + "\n" + setter
55
- end
56
-
57
- private #################################################################
58
-
59
- def generate_enum(path, package, enum_name, enum_values, json_values, support_annotations)
60
- enum_file = String.new
61
- enum_file << PACKAGE_TEMPLATE%[package] + "\n\n"
62
- enum_file << GENERATED_MESSAGE + "\n\n"
63
- enum_file << ENUM_TEMPLATE%[enum_name] + "\n\n"
64
- json_values = get_json_values(enum_values, json_values)
65
- if enum_values.length != 0
66
- (0..enum_values.length - 1).each { |idx|
67
- gson_value = json_values[idx]
68
- enum_value = generate_enum_string(enum_values[idx], gson_value)
69
- enum_file << (idx != enum_values.length - 1 ? enum_value + ",\n" : enum_value + ";\n")
70
- }
71
- enum_file << "\n" ' ' + FINAL_ATTRIBUTE_TEMPLATE%%w(String jsonValue) + "\n\n"
72
- enum_file << generate_enum_gson_constructor(enum_name) + "\n"
73
- enum_file << generate_static_gson_getter(enum_name, support_annotations) + "\n"
74
- enum_file << generate_gson_getter(support_annotations)
75
- enum_file << '}' + "\n"
76
- Gyro.write_file_with_name(path, JAVA_FILE_TEMPLATE%[enum_name], enum_file)
77
- end
78
- end
79
-
80
- def generate_enum_string(enum_value, gson_value)
81
- enum_value = enum_value.delete_objc_prefix.camel_case
82
- gson_annotation = gson_value.empty? ? '' : ENUM_JSON_VALUE%[gson_value]
83
- ' ' + enum_value + gson_annotation
84
- end
85
-
86
- def generate_enum_gson_constructor(enum_name)
87
- ' ' + enum_name +'(final String jsonValue) {' + "\n" +
88
- ' ' + 'this.jsonValue = jsonValue;' + "\n" +
89
- ' ' + '}' + "\n"
90
- end
91
-
92
- # Methods to bypass enum restriction in Realm
93
- def generate_static_gson_getter(enum_name, support_annotations)
94
- getter = String.new
95
- getter << ' @android.support.annotation.Nullable' + "\n" if support_annotations
96
- getter << ' public static ' + enum_name + ' get(final String jsonValue) {' + "\n" +
97
- ' ' + 'for (final ' + enum_name + ' type : ' + enum_name + '.values()) {' + "\n" +
98
- ' ' + 'if (type.getJsonValue().equals(jsonValue)) {' + "\n" +
99
- ' ' + 'return type;' + "\n" +
100
- ' ' + '}'+ "\n" +
101
- ' ' + '}' + "\n" +
102
- ' ' + 'return null;' + "\n" +
103
- ' ' + '}' + "\n"
104
- getter
105
- end
106
-
107
- def generate_gson_getter(support_annotations)
108
- getter = String.new
109
- getter << ' @android.support.annotation.NonNull' + "\n" if support_annotations
110
- getter << ' ' + 'public String getJsonValue() {' + "\n" +
111
- ' ' + 'return jsonValue;' + "\n" +
112
- ' ' + '}' + "\n"
113
- getter
114
- end
115
-
116
- def get_json_values(enum_values, json_values)
117
- if json_values.empty?
118
- enum_values.each { |value|
119
- json_values << value.delete_objc_prefix.underscore
120
- }
121
- end
122
- json_values
123
- end
124
-
125
- end
126
- end
127
- end
128
- end
@@ -1,183 +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
-
25
- module Gyro
26
- module Realm
27
- module Java
28
-
29
- class Generator
30
-
31
- # INCLUDES #############################################################
32
-
33
- include Gyro::XCDataModel::Parser
34
- include Converter
35
- include EnumGenerator
36
- include Templates
37
-
38
- # PUBLIC METHODS #######################################################
39
-
40
- def initialize(path, package_name, xcdatamodel, use_wrappers = false, support_annotations = false)
41
- puts "\n"
42
- Gyro::Log::title('Android Realm')
43
- xcdatamodel.entities.each do |_, entity|
44
- unless entity.abstract?
45
- Gyro::Log::success("Generating entity #{entity.name}...")
46
- generate_class(path, package_name, entity, use_wrappers, support_annotations)
47
- end
48
- end
49
- end
50
-
51
- private ################################################################
52
-
53
- def generate_class(path, package, entity, use_wrappers, support_annotations)
54
- class_file = String.new
55
- entity.name = entity.name.delete_objc_prefix
56
- generate_header(class_file, package, entity)
57
- generate_attributes(class_file, entity.attributes, entity.relationships, entity.identity_attribute, use_wrappers, support_annotations)
58
- generate_footer(class_file)
59
- Gyro.write_file_with_name(path, JAVA_FILE_TEMPLATE%[entity.name], class_file)
60
- generate_enums(path, package, entity.attributes, support_annotations)
61
- end
62
-
63
- def generate_header(class_file, package, entity)
64
- class_file << PACKAGE_TEMPLATE%[package] + "\n\n"
65
- class_file << IMPORT_GSON + "\n\n" if entity.has_json_key_path?
66
- class_file << IMPORT_DATE + "\n" if entity.has_date_attribute?
67
- class_file << IMPORT_LIST + "\n" if entity.has_list_relationship?
68
- class_file << "\n" if entity.has_date_attribute? or entity.has_list_relationship?
69
- class_file << IMPORT_REALM_LIST + "\n" if entity.has_list_attributes?
70
- class_file << IMPORT_REALM_OBJECT + "\n"
71
- class_file << IMPORT_REALM_IGNORE + "\n" if entity.has_ignored? or entity.has_enum_attributes?
72
- class_file << IMPORT_REALM_INDEX + "\n" if entity.has_indexed_attributes?
73
- class_file << IMPORT_REALM_PRIMARY_KEY + "\n" if entity.has_primary_key?
74
- class_file << "\n" if class_file != PACKAGE_TEMPLATE%[package]
75
- class_file << GENERATED_MESSAGE + "\n\n"
76
- class_file << CLASS_COMMENT_TEMPLATE%[entity.comment] + "\n" unless entity.comment.empty?
77
- class_file << CLASS_TEMPLATE%[entity.name] + "\n\n"
78
- class_file << generate_constants(entity)
79
- end
80
-
81
- def generate_constants(entity)
82
- attribute_constants = String.new
83
- unless entity.attributes.empty?
84
- attribute_constants << ' ' + ATTRIBUTE_CONSTANTS + "\n"
85
- entity.attributes.each do |_, attribute|
86
- unless attribute.realm_ignored? or attribute.read_only?
87
- attribute_constants << ' ' + ATTRIBUTE_COMMENT_TEMPLATE%[attribute.comment] + "\n" unless attribute.comment.empty?
88
- attribute_constants << ' ' + CONSTANT_TEMPLATE%[attribute.name.underscore.upcase, attribute.name] + "\n"
89
- end
90
- end
91
- attribute_constants << ' ' + '}'+ "\n\n"
92
- end
93
- relationship_constants = String.new
94
- if not entity.relationships.empty? and not entity.has_only_inverse?
95
- relationship_constants << ' ' + RELATIONSHIP_CONSTANTS + "\n"
96
- entity.relationships.each do |_, relationship|
97
- relationship_constants << ' ' + CONSTANT_TEMPLATE%[relationship.name.underscore.upcase, relationship.name] + "\n" unless relationship.inverse?
98
- end
99
- relationship_constants << ' ' + '}'+ "\n\n"
100
- end
101
- attribute_constants + relationship_constants
102
- end
103
-
104
- def generate_footer(class_file)
105
- class_file << '}' + "\n"
106
- end
107
-
108
- def generate_attributes(class_file, attributes, relationships, primary_key, use_wrappers, support_annotations)
109
- # "NORMAL" ATTRIBUTES
110
- (attributes_string, getters_and_setters_string) = write_attributes(attributes, primary_key, use_wrappers, support_annotations)
111
- # "RELATIONSHIP" ATTRIBUTES
112
- relationships.each do |_, relationship|
113
- unless relationship.inverse?
114
- if relationship.destination.empty?
115
- type_without_prefix = relationship.inverse_type.delete_objc_prefix
116
- type = relationship.type == :to_many ? REALM_LIST_TEMPLATE%[type_without_prefix] : type_without_prefix
117
- name = relationship.name
118
- else
119
- type = LIST_TEMPLATE%[relationship.destination]
120
- name = relationship.name
121
- end
122
- attributes_string << ' ' + IGNORED_ANNOTATION + "\n" if relationship.realm_ignored?
123
- attributes_string << ' ' + GSON_ANNOTATION%[relationship.json_key_path]+ "\n" unless relationship.json_key_path.empty?
124
- attributes_string << ' ' + ATTRIBUTE_TEMPLATE%[type, name] + "\n"
125
- getters_and_setters_string << "\n" unless getters_and_setters_string.empty?
126
- getters_and_setters_string << generate_getter_and_setter(type, name, (support_annotations and relationship.optional), (support_annotations and !relationship.optional), relationship.support_annotation)
127
- end
128
- end
129
- class_file << attributes_string + "\n" + getters_and_setters_string
130
- end
131
-
132
- def write_attributes(attributes, primary_key, use_wrappers, support_annotations)
133
- attributes_string = String.new
134
- getters_and_setters_string = String.new
135
- attributes.each_with_index do |(_, attribute), idx|
136
- unless attribute.read_only?
137
- name = attribute.name
138
- type = attribute.enum? ? 'String' : convert_type(attribute.type, (use_wrappers and attribute.optional))
139
- if type
140
- # Realm annotations
141
- attributes_string << ' ' + PRIMARY_KEY_ANNOTATION + "\n" if name == primary_key
142
- attributes_string << ' ' + INDEXED_ANNOTATION + "\n" if attribute.indexed
143
- attributes_string << ' ' + IGNORED_ANNOTATION + "\n" if attribute.realm_ignored? or attribute.enum?
144
- if attribute.enum?
145
- ignored_type = attribute.enum? ? attribute.enum_type.delete_objc_prefix : type
146
- ignored_name = attribute.enum? ? name + 'Enum' : name
147
- attributes_string << ' ' + ATTRIBUTE_TEMPLATE%[ignored_type, ignored_name] + "\n"
148
- end
149
- attributes_string << ' ' + GSON_ANNOTATION%[attribute.json_key_path]+ "\n" unless attribute.json_key_path.empty?
150
- attributes_string << ' ' + SUPPORT_ANNOTATION%[attribute.support_annotation] + "\n" unless attribute.support_annotation.empty?
151
- attributes_string << ' ' + ATTRIBUTE_TEMPLATE%[type, name] + "\n"
152
- end
153
- getters_and_setters_string << generate_getter_and_setter(type, name, (support_annotations and ((use_wrappers and attribute.optional) or (attribute.enum? and attribute.optional))), (support_annotations and ((!is_primitive(type) and !attribute.optional) or (attribute.enum? and !attribute.optional))), attribute.support_annotation)
154
- getters_and_setters_string << "\n" + generate_enum_getter_and_setter(attribute.enum_type.delete_objc_prefix, name, support_annotations) if attribute.enum?
155
- getters_and_setters_string << "\n" if idx != attributes.count - 1
156
- end
157
- end
158
- return attributes_string, getters_and_setters_string
159
- end
160
-
161
- def generate_getter_and_setter(type, name, nullable, nonnull, support_annotation)
162
- getter_setter = String.new
163
- getter_setter << ' ' + SUPPORT_ANNOTATION%['Nullable'] + "\n" if nullable
164
- getter_setter << ' ' + SUPPORT_ANNOTATION%['NonNull'] + "\n" if nonnull
165
- getter_setter << ' ' + SUPPORT_ANNOTATION%[support_annotation] + "\n" unless support_annotation.empty?
166
- getter_setter << ' ' + 'public ' + type + ' get' + name.capitalize_first_letter + '() {' + "\n"
167
- getter_setter << ' ' + 'return '+ name + ';' + "\n"
168
- getter_setter << ' ' + '}' + "\n\n"
169
- getter_setter << ' ' + 'public void set' + name.capitalize_first_letter + '('
170
- getter_setter << SUPPORT_ANNOTATION%['Nullable'] + ' ' if nullable
171
- getter_setter << SUPPORT_ANNOTATION%['NonNull'] + ' ' if nonnull
172
- getter_setter << SUPPORT_ANNOTATION%[support_annotation] + ' ' unless support_annotation.empty?
173
- getter_setter << 'final ' + type + ' ' + name + ') {' + "\n"
174
- getter_setter << ' ' + 'this.' + name + ' = ' + name + ';' + "\n"
175
- getter_setter << ' ' + '}' + "\n"
176
- getter_setter
177
- end
178
-
179
- end
180
-
181
- end
182
- end
183
- end
@@ -1,67 +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 Java
20
- module Templates
21
-
22
- # ANNOTATIONS
23
- PRIMARY_KEY_ANNOTATION = '@PrimaryKey'
24
- INDEXED_ANNOTATION = '@Index'
25
- IGNORED_ANNOTATION = '@Ignore'
26
- GSON_ANNOTATION = "@SerializedName(\"%s\")"
27
- SUPPORT_ANNOTATION = "@android.support.annotation.%s"
28
-
29
- # COMMONS
30
- PACKAGE_TEMPLATE = 'package %s;'
31
- JAVA_FILE_TEMPLATE = '%s.java'
32
- GENERATED_MESSAGE = '/* DO NOT EDIT | Generated by gyro */'
33
-
34
- # IMPORTS
35
- IMPORT_DATE = 'import java.util.Date;'
36
- IMPORT_LIST = 'import java.util.List;'
37
- IMPORT_GSON = 'import com.google.gson.annotations.SerializedName;'
38
- IMPORT_REALM_LIST = 'import io.realm.RealmList;'
39
- IMPORT_REALM_OBJECT = 'import io.realm.RealmObject;'
40
- IMPORT_REALM_IGNORE = 'import io.realm.annotations.Ignore;'
41
- IMPORT_REALM_INDEX = 'import io.realm.annotations.Index;'
42
- IMPORT_REALM_PRIMARY_KEY = 'import io.realm.annotations.PrimaryKey;'
43
-
44
- # ENUM
45
- ENUM_TEMPLATE = 'public enum %s {'
46
- ENUM_JSON_VALUE = "(\"%s\")"
47
-
48
- # CLASS
49
- CLASS_TEMPLATE = 'public class %s extends RealmObject {'
50
- ATTRIBUTE_TEMPLATE = 'private %s %s;'
51
- FINAL_ATTRIBUTE_TEMPLATE = 'private final %s %s;'
52
- REALM_LIST_TEMPLATE = 'RealmList<%s>'
53
- LIST_TEMPLATE = 'List<%s>'
54
-
55
- # CONSTANTS
56
- ATTRIBUTE_CONSTANTS = 'public interface Attributes {'
57
- RELATIONSHIP_CONSTANTS = 'public interface Relationships {'
58
- CONSTANT_TEMPLATE = 'String %s = "%s";'
59
-
60
- # COMMENTS
61
- CLASS_COMMENT_TEMPLATE = "/**\n * %s\n */"
62
- ATTRIBUTE_COMMENT_TEMPLATE = '/** %s */'
63
-
64
- end
65
- end
66
- end
67
- end
@@ -1,63 +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 Converter
21
-
22
- TYPES = {
23
- :integer_16 => 'int',
24
- :integer_32 => 'long',
25
- :integer_64 => 'long long',
26
- :decimal => 'double',
27
- :double => 'double',
28
- :float => 'float',
29
- :string => 'NSString *',
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 => '@(NO)',
44
- :date => '[NSDate date]',
45
- :binary => '[NSData new]'
46
- }
47
-
48
- def is_number_type?(type)
49
- [:integer_16, :integer_32, :integer_64, :decimal, :double, :float].include?(type)
50
- end
51
-
52
- def convert_type(type, use_nsnumber = false)
53
- use_nsnumber && is_number_type?(type) ? 'NSNumber *' : TYPES[type]
54
- end
55
-
56
- def convert_default(type)
57
- DEFAULTS[type]
58
- end
59
-
60
- end
61
- end
62
- end
63
- end
@@ -1,86 +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 EnumGenerator
26
-
27
- # INCLUDES #############################################################
28
-
29
- include Templates
30
- include Converter
31
-
32
- # PUBLIC METHODS #######################################################
33
-
34
- def generate_enum_file(path, xcdatamodel)
35
- enums = []
36
- enum_file = String.new
37
- enum_file << GENERATED_MESSAGE + "\n"
38
- enum_file << "\n" + SEPARATOR + "\n\n"
39
- enum_file << PRAGMA_MARK_TYPES + "\n\n"
40
- xcdatamodel.entities.each do |_, entity|
41
- entity.attributes.each do |_, attribute|
42
- if attribute.enum? and !enums.include?(attribute.enum_type)
43
- int_type = convert_type(attribute.type)
44
- enum_type = attribute.enum_type
45
- enum_file << "\n" if enums.length > 0
46
- enums.push(enum_type)
47
- enum = generate_enum(int_type, enum_type, attribute)
48
- enum_file << enum
49
- end
50
- end
51
- end
52
- if enums.size != 0
53
- Gyro.write_file_with_name(path, HEADER_TEMPLATE%[ENUM_FILE_NAME], enum_file)
54
- end
55
- end
56
-
57
- private ################################################################
58
-
59
- def generate_enum(int_type, enum_name, attribute)
60
- enum_string = String.new
61
- enum_string << ENUM_TYPEDEF_TEMPLATE%[int_type, enum_name] + "\n"
62
- enum_values = Array.new
63
- enum_values += %W(#{enum_name}None) if attribute.optional?
64
- enum_values += attribute.enum_values
65
- if enum_values.length != 0
66
- # First one
67
- value = enum_values[0]
68
- enum_string << ' ' + value + ' = 0,' + "\n"
69
- # Others
70
- (1..enum_values.length - 2).each { |i|
71
- value = enum_values[i]
72
- enum_string << ' ' + value + ',' + "\n"
73
- }
74
- # Last one if at least 2 values
75
- if enum_values.length > 1
76
- value = enum_values.last
77
- enum_string << ' ' + value + "\n"
78
- end
79
- end
80
- enum_string << '};' + "\n"
81
- end
82
-
83
- end
84
- end
85
- end
86
- end