gyro 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30319d692b235808740716342f2e77b76d8cee30
4
+ data.tar.gz: c8f35a028991c3313f8edc72900abfa809f805f7
5
+ SHA512:
6
+ metadata.gz: fa5a072a603e117f45190b741c3c1df3dfa04b248345949318e55caec3ea308ff75c96b173d154a373135bf671e08d55d910ec5e6fdf0c78963b0da3a2f42f56
7
+ data.tar.gz: 446e8e2e43e7c1414bc891c85b0b113ad2298487bf16e118b33034cdb9bcc7dfe0cc168af971c989ad9e658d9185dffbd9ce011627471ffe81daa696f4f0d4ff
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,840 @@
1
+ # Gyro
2
+
3
+ Gyro is a tool to generate [Realm](https://realm.io) model classes, for both Android (Java) and iOS/macOS (ObjC & Swift), from an `.xcdatamodel` file.
4
+
5
+ ---
6
+
7
+
8
+ ## Introduction
9
+
10
+ The `.xcdatamodel` file is usually used to represent Core Data entities in Xcode in a graphical way. It can be created or edited with a graphical user interface in Xcode.
11
+
12
+ But with Gyro, you will now be able to **use an `xcdatamodel` to create a [Realm](https://realm.io) model files as well!**
13
+
14
+ This will allow you to design your model in a visual way (rather than by code), only once (rather than once for Android and once for iOS), and have the code generated for you.
15
+
16
+ ![Simple Entity](documentation/simple_entity.png)
17
+
18
+ The `.xcdatamodel` file is the input of the script.
19
+
20
+
21
+ ## License
22
+
23
+ This tool is under [the Apache 2 License](LICENSE).
24
+
25
+ It has been initially developed by [Niji](http://www.niji.fr) and is in no way affiliated to the [Realm](https://realm.io) company.
26
+
27
+
28
+ ## Installation
29
+
30
+ Simply clone this repository anywhere you want on your machine, then run the `bin/gyro` ruby script with the appropriate options (see below). For example:
31
+
32
+ ```
33
+ ~/Dev/Gyro/bin/gyro -m <model> --ios ~/Dev/MyProject/RealmModel --swift
34
+ ```
35
+
36
+
37
+
38
+
39
+ ## Command line arguments
40
+
41
+ Gyro is a command line tool. The available parameters are as follows. You can also use `-h` do display the usage and available parameters/flags in the Terminal of course.
42
+
43
+ | Short flag | Long flag | Description | Android | iOS |
44
+ | ---------- | --------- | ----------- |:-------:|:---:|
45
+ | `-m` | `--model` | Path to the  `.xcdatamodel` file. If this parameter is not given, Gyro will look for a `.xcdatamodel` | ✅ | ✅ |
46
+ | `-a` | `--android` | Path to the directory where the generated files for Android will be created (e.g.: home/documents/dev/android/realm_project/com/niji/data) | ✅ | ➖ |
47
+ | `-p` | `--package` | Full name of the Android "data" package (e.g.: com.niji.data) | ✅ | ➖ |
48
+ | `-i` | `--ios` | Path to the directory where the generated files for iOS/macOS will be created | ➖ | ✅ |
49
+ | `-j` | `--json` | Create the Realm-JSON categories (https://github.com/matthewcheok/Realm-JSON) | ➖ | ☑️ |
50
+ | `-f` | `--framework` | Tells whether the project uses CocoaPods Frameworks | ➖ | ☑️ |
51
+ | `-s` | `--swift` | Use Swift for the iOS/macOS generation | ➖ | ☑️ |
52
+ | `-n` | `--nsnumber` | Generate `NSNumber`s instead of Int/BOOL/Float types | ➖ | ☑️ |
53
+ | `-w` | `--wrappers` | Use type wrappers for Java (Integer, Double, …) for optional attributes instead of primitive types (int, double, …) | ☑️ | ➖ |
54
+ | `-x` | `--annotations` | Annotate the getters/setters of the generated classes with `@Nullable` for any optional attribute/relationship, and with `@NonNull` for any non-optional attribute/relationship | ☑️ | ➖ |
55
+ | `-h` | `--help` | Show help | ☑️ | ☑️ |
56
+ | `-v` | `--version` | Show the current version number of Gyro | ☑️ | ☑️ |
57
+
58
+ _Caption: ✅ Mandatory flag for this platform / ☑️ Optional flag usable for this platform / ➖ Not applicable for this platform_
59
+
60
+
61
+
62
+ ## Annotating your `xcdatamodel`
63
+
64
+ The `.xcdatamodel` Xcode editor allows you to add "user infos" to your entities, attributes or relationships. Each "user info" entry is an arbitrary key/value pair.
65
+
66
+ _To define a User Info key in Xcode's xcdatamodel editor, select the entity or attribute you want to add a User Info to, then select the 3rd tab in the inspector on the right ("Data Model Inspector", or Cmd-Alt-3), and fill the information you want in the "User Info" section there._
67
+
68
+ With the help of these "user infos", you will be able to give Gyro extra information about your model classes. For example, you can tell which attribute is the primary key, the attributes to ignore, the JSON mappings, …
69
+
70
+ Below are details about how to annotate your `.xcdatamodel` entities and attributes to be able to leverage each Realm features when generating your Realm models with Gyro.
71
+
72
+
73
+ ---
74
+
75
+
76
+ ### Primary key
77
+
78
+ To tell which attribute will be used as a primary key, add the following 'user info' to **the entity**:
79
+
80
+ | Key | Value |
81
+ |-----|-------|
82
+ | `identityAttribute` | `name_of_the_attribute` |
83
+
84
+
85
+ __Example__: On the "FidelityCard" entity:
86
+
87
+ ![Primary Key](documentation/primary_key.png)
88
+
89
+
90
+ <details>
91
+ <summary>📑 Sample of the generated code in Java (Android)</summary>
92
+
93
+ ```java
94
+ package com.niji.data;
95
+
96
+ import io.realm.RealmObject;
97
+ import io.realm.annotations.PrimaryKey;
98
+
99
+ /* DO NOT EDIT | Generated by gyro */
100
+
101
+ public class FidelityCard extends RealmObject {
102
+
103
+ @PrimaryKey
104
+ private short identifier;
105
+ private int points;
106
+ private User user;
107
+ [...]
108
+ }
109
+ ```
110
+ </details>
111
+
112
+ <details>
113
+ <summary>📑 Sample of the generated code in Objective-C (iOS)</summary>
114
+
115
+ ```objc
116
+ // DO NOT EDIT | Generated by gyro
117
+
118
+ ////////////////////////////////////////////////////////////////////////////////
119
+
120
+ #pragma mark - Imports
121
+
122
+ #import "RLMFidelityCard.h"
123
+
124
+ ////////////////////////////////////////////////////////////////////////////////
125
+
126
+ #pragma mark - Implementation
127
+
128
+ @implementation RLMFidelityCard
129
+
130
+ #pragma mark - Superclass Overrides
131
+
132
+ + (NSString *)primaryKey
133
+ {
134
+ return @"identifier";
135
+ }
136
+
137
+ @end
138
+ ```
139
+ </details>
140
+
141
+
142
+ ---
143
+
144
+
145
+ ### Ignore attribute
146
+
147
+ You can decide to ignore some attributes of the `.xcdatamodel` file. They will not be persisted to Realm. To do so, add the following 'user info' to **the attribute**:
148
+
149
+ | Key | Value |
150
+ |-----|-------|
151
+ | `realmIgnored` | `value` |
152
+
153
+
154
+ __Example__: on the attribute `ignored` of the entity `Shop`:
155
+
156
+ ![Ignored Attribute](documentation/ignored.png)
157
+
158
+
159
+ <details>
160
+ <summary>📑 Sample of the generated code in Java (Android)</summary>
161
+
162
+ ```java
163
+ package com.niji.data;
164
+
165
+ import io.realm.RealmList;
166
+ import io.realm.RealmObject;
167
+ import io.realm.annotations.Ignore;
168
+
169
+ /* DO NOT EDIT | Generated by gyro */
170
+
171
+ public class Shop extends RealmObject {
172
+
173
+ private String name;
174
+ private String readOnly;
175
+ private RealmList<Product> products;
176
+ @Ignore
177
+ private String ignored;
178
+ [...]
179
+ }
180
+ ```
181
+ </details>
182
+
183
+ <details>
184
+ <summary>📑 Sample of the generated code in Objective-C (iOS)</summary>
185
+
186
+ ```objc
187
+ // DO NOT EDIT | Generated by gyro
188
+
189
+ ////////////////////////////////////////////////////////////////////////////////
190
+
191
+ #pragma mark - Imports
192
+
193
+ #import "RLMShop.h"
194
+
195
+ ////////////////////////////////////////////////////////////////////////////////
196
+
197
+ #pragma mark - Implementation
198
+
199
+ @implementation RLMShop
200
+
201
+ #pragma mark - Superclass Overrides
202
+
203
+ // Specify properties to ignore (Realm won't persist these)
204
+ + (NSArray *)ignoredProperties
205
+ {
206
+ return @[@"ignored"];
207
+ }
208
+ @end
209
+ ```
210
+ </details>
211
+
212
+
213
+ ---
214
+
215
+
216
+ ### Read only
217
+
218
+ On iOS/macOS, you can define attributes which are not persisted and whose value is computed dynamically.
219
+ To do so, add the following 'user info' to **the attribute**
220
+
221
+ | Key | Value |
222
+ |-----|-------|
223
+ | `realmReadOnly` | `the_code_source_to_generate` |
224
+
225
+
226
+ __Example__: On the `readOnly` attribute of the `Shop` entity:
227
+
228
+ ![Read Only](documentation/read_only.png)
229
+
230
+
231
+ <details>
232
+ <summary>📑 Sample of the generated code in Objective-C (iOS)</summary>
233
+
234
+ ```objc
235
+ // DO NOT EDIT | Generated by gyro
236
+
237
+ ////////////////////////////////////////////////////////////////////////////////
238
+
239
+ #pragma mark - Imports
240
+
241
+ #import "RLMShop.h"
242
+
243
+ ////////////////////////////////////////////////////////////////////////////////
244
+
245
+ #pragma mark - Implementation
246
+
247
+ @implementation RLMShop
248
+
249
+ #pragma mark - Superclass Overrides
250
+
251
+ - (NSString *)readOnly
252
+ {
253
+ return self.name;
254
+ }
255
+
256
+ @end
257
+ ```
258
+ </details>
259
+
260
+
261
+ ---
262
+
263
+
264
+ ### Inverse Relationships
265
+
266
+ In realm, when you have both A -> B and B -> A relationships, you have to choose one of those relationships to be the primary one (e.g. A -> B) — that will be stored in Realm — and the other inverse relationship will then be **computed** by code. [For more information, see the related RealmSwift documentation on Inverse Relationships](https://realm.io/docs/swift/latest/#inverse-relationships).
267
+
268
+ To mark a relationship as being an inverse relationship (the B -> A relationship and not the primary A -> B one), the convention in `gyro` is to **suffix the name of the relationship with an underscore `_`** .
269
+
270
+ This will then generate the following code in Swift for that inverse relationship:
271
+
272
+ ```swift
273
+ LinkingObjects(fromType: A.self, property: "b")`
274
+ ```
275
+
276
+ If your inverse relationship is defined to point to a unique object (inverse of a `1-*` relationship for exemple, and not a `*-*` one), the generated code will contain both the plural form of the computed variable and a singular form returning its first element, for convenience:
277
+
278
+ ```swift
279
+ let owners = LinkingObjects(fromType: Person.self, property: "dogs")`
280
+ var owner: Person? { return owners.first }
281
+ ```
282
+
283
+
284
+ ---
285
+
286
+
287
+ ### Optionnals fields and wrapper types
288
+
289
+ On Android, the `-w`/`--wrappers` flag allows you to use wrapper types (`Double`, `Short`, …) for optional fields instead of primitive types (`double`, `short`, …).
290
+
291
+ <details>
292
+ <summary>📑 Sample of the generated code in Java (Android)</summary>
293
+
294
+ ```java
295
+ package com.niji.data;
296
+
297
+ import io.realm.RealmObject;
298
+ import io.realm.annotations.PrimaryKey;
299
+
300
+ /* DO NOT EDIT | Generated by gyro */
301
+
302
+ public class FidelityCard extends RealmObject {
303
+
304
+ @PrimaryKey
305
+ private short identifier; // "optional" checkbox not checked in the xcdatamodel
306
+ private Integer points; // "optional" checkbox checked in the xcdatamodel
307
+ private User user;
308
+ [...]
309
+ }
310
+ ```
311
+ </details>
312
+
313
+
314
+ ---
315
+
316
+
317
+ ### Support Annotations
318
+
319
+ On Android, the flag `-x`/`--annotations` allows you to annotate class attributes' getters & setters with `@Nullable` (if the attribute is optional) or `@NonNull` (if it isn't) attributes.
320
+ This option can be combined with the `-w` wrapper flag to generate a safer and more secure code in Android Studio, generating proper warnings if misused.
321
+
322
+ <details>
323
+ <summary>📑 Sample of the generated code in Java (Android)</summary>
324
+
325
+ ```java
326
+ package com.niji.data;
327
+
328
+ import io.realm.RealmObject;
329
+ import io.realm.annotations.PrimaryKey;
330
+
331
+ /* DO NOT EDIT | Generated by gyro */
332
+
333
+ public class FidelityCard extends RealmObject {
334
+
335
+ @PrimaryKey
336
+ private short identifier; // "optional" checkbox not checked in the xcdatamodel
337
+ private Integer points; // "optional" checkbox checked in the xcdatamodel
338
+ private User user;
339
+ [...]
340
+
341
+ @android.support.annotation.Nullable
342
+ public Integer getPoints() {
343
+ return points;
344
+ }
345
+
346
+ public void setPoints(@android.support.annotation.Nullable final Integer points) {
347
+ this.points = points;
348
+ }
349
+
350
+ }
351
+ ```
352
+ </details>
353
+
354
+ Furthermore, it's possible to add custom annotations to your fields.
355
+ To do that, simply add the key/value pair to the UserInfos of the attribute to annotate:
356
+
357
+ | Key | Value |
358
+ |-----|-------|
359
+ | `supportAnnotation` | `AnnotationToAdd` |
360
+
361
+
362
+ __Example__: If you wish to add the `IntRange(from=0,to=255)` annotation to an attribute, use the following:
363
+
364
+ ![Support Annotation](documentation/support_annotation.png)
365
+
366
+
367
+ <details>
368
+ <summary>📑 Sample of the generated code in Java (Android)</summary>
369
+
370
+ ```java
371
+ package com.gyro.tests;
372
+
373
+ import io.realm.RealmObject;
374
+
375
+ /* DO NOT EDIT | Generated by gyro */
376
+
377
+ public class FidelityCard extends RealmObject {
378
+
379
+ public interface Attributes {
380
+ String IDENTIFIER = "identifier";
381
+ String POINTS = "points";
382
+ }
383
+
384
+ private short identifier;
385
+ @android.support.annotation.IntRange(from=0,to=255)
386
+ private int points;
387
+
388
+ public short getIdentifier() {
389
+ return identifier;
390
+ }
391
+
392
+ public void setIdentifier(final short identifier) {
393
+ this.identifier = identifier;
394
+ }
395
+
396
+ @android.support.annotation.IntRange(from=0,to=255)
397
+ public int getPoints() {
398
+ return points;
399
+ }
400
+
401
+ public void setPoints(@android.support.annotation.IntRange(from=0,to=255) final int points) {
402
+ this.points = points;
403
+ }
404
+ }
405
+ ```
406
+ </details>
407
+
408
+
409
+ ---
410
+
411
+
412
+ ### Handling enums
413
+
414
+ Sometimes, an `Int` attribute in the model actually represents an `enum` member in your model. To deal with this case, you can add the following two key/value pairs to this **attribute**:
415
+
416
+ | Key | Value |
417
+ |-----|-------|
418
+ | `enumType` | `my_type` |
419
+ | `enumValues` | `my_value_1, my_value_2, my_value_3` |
420
+
421
+ > _Note: If you also add the `JSONKeyPath` User Info key to your attribute in addition to enums, you'll have to add the `JSONValues` to also tell the mapping between the `enumValues` and the matching possible values found in the JSON. See the [JSON Mapping](#json-mapping) below for more details._
422
+
423
+ __Example__: On the attribute `type` of the `Shop` entity.
424
+
425
+ ![enum](documentation/enum.png)
426
+
427
+
428
+ <details>
429
+ <summary>📑 Sample of the generated code in Java (Android)</summary>
430
+
431
+ `Shop.java`:
432
+
433
+ ```java
434
+ package com.niji.data;
435
+
436
+ import io.realm.RealmObject;
437
+
438
+ /* DO NOT EDIT | Generated by gyro */
439
+
440
+ public class Shop extends RealmObject {
441
+ private String name;
442
+ private Type type;
443
+ [...]
444
+ }
445
+ ```
446
+
447
+ `Type.java`:
448
+
449
+ ```java
450
+ package com.niji.data;
451
+
452
+ /* DO NOT EDIT | Generated by gyro */
453
+
454
+ public enum Type {
455
+ TYPE_ONE,
456
+ TYPE_TWO,
457
+ TYPE_THREE
458
+ }
459
+ ```
460
+ </details>
461
+
462
+ <details>
463
+ <summary>📑 Sample of the generated code in Objective-C (iOS)</summary>
464
+
465
+ `RLMShop.h`:
466
+
467
+ ```objc
468
+ // DO NOT EDIT | Generated by gyro
469
+
470
+ ////////////////////////////////////////////////////////////////////////////////
471
+
472
+ #pragma mark - Imports
473
+
474
+ #import <Realm/Realm.h>
475
+ #import "RLMTypes.h"
476
+
477
+ ////////////////////////////////////////////////////////////////////////////////
478
+
479
+ #pragma mark - Interface
480
+
481
+ @interface RLMShop : RLMObject
482
+
483
+ #pragma mark - Properties
484
+
485
+ @property NSString *name;
486
+ @property RLMType type;
487
+
488
+ @end
489
+ ```
490
+
491
+ `RLMTypes.h`:
492
+
493
+ ```objc
494
+ // DO NOT EDIT | Generated by gyro
495
+
496
+ ////////////////////////////////////////////////////////////////////////////////
497
+
498
+ #pragma mark - Types
499
+
500
+ typedef NS_ENUM(int, RLMType) {
501
+ RLMTypeOne = 0,
502
+ RLMTypeTwo,
503
+ RLMTypeThree
504
+ };
505
+ ```
506
+ </details>
507
+
508
+ <details>
509
+ <summary>📑 Sample of the generated code in Swift (iOS)</summary>
510
+
511
+ `Shop.swift`:
512
+
513
+ ```swift
514
+ /* DO NOT EDIT | Generated by gyro */
515
+
516
+ import RealmSwift
517
+
518
+ final class Shop: Object {
519
+
520
+ enum Attributes: String {
521
+ case Name = "name"
522
+ case OptionalValue = "optionalValue"
523
+ case Type = "type"
524
+ }
525
+
526
+ dynamic var name: String = ""
527
+ dynamic var optionalValue: String? = nil
528
+ var optionalValueEnum: OptValue? {
529
+ get {
530
+ guard let optionalValue = optionalValue,
531
+ let enumValue = OptValue(rawValue: optionalValue)
532
+ else { return nil }
533
+ return enumValue
534
+ }
535
+ set { optionalValue = newValue?.rawValue ?? nil }
536
+ }
537
+
538
+ dynamic var type: String = ""
539
+ var typeEnum: Type? {
540
+ get {
541
+ guard let enumValue = Type(rawValue: type) else { return nil }
542
+ return enumValue
543
+ }
544
+ set { type = newValue?.rawValue ?? "" }
545
+ }
546
+ }
547
+ ```
548
+
549
+ `Type.swift`:
550
+
551
+ ```swift
552
+ /* DO NOT EDIT | Generated by gyro */
553
+
554
+ enum Type: String {
555
+ case TypeOne = "type_one"
556
+ case TypeTwo = "type_two"
557
+ case TypeThree = "type_three"
558
+ }
559
+ ```
560
+
561
+ `OptValue.swift`
562
+
563
+ ```swift
564
+ /* DO NOT EDIT | Generated by gyro */
565
+
566
+ enum OptValue: String {
567
+ case OptValueOne = "opt_value_one"
568
+ case OptValueTwo = "opt_value_two"
569
+ case OptValueThree = "opt_value_three"
570
+ }
571
+ ```
572
+ </details>
573
+
574
+ > **Note**: For Android and Swift, each enum is created in a separate file. For ObjC, all the enums are created in the file RLMTypes.h
575
+
576
+
577
+ ---
578
+
579
+
580
+ ### Add comments to the generated classes
581
+
582
+ To make the generated code more readable, it's possible to add comments on an entity — e.g. to provide a short description of what this entity is supposed to represent.
583
+
584
+ To do so, simply add the following key/value pair to your **entity** in your `.xcdatamodel`:
585
+
586
+ | Key | Value |
587
+ |-----|-------|
588
+ | `comment` | `the_comment_text_here` |
589
+
590
+ A code commend (`/** … */`) will then be generated (in the `.h` (ObjC), `.swift` (Swift) or `.java` (Android)) just before the class declaration, e.g. to help the developer understand what this class is for.
591
+
592
+
593
+ ---
594
+
595
+
596
+ ### JSON Mapping
597
+
598
+ You can also add the json mapping for each **attribute** or **relationship** with the following key/value pair:
599
+
600
+ | Key | Value |
601
+ |-----|-------|
602
+ | `JSONKeyPath` | `json_field_name` |
603
+
604
+ This key is only used when using the `--json` flag.
605
+
606
+ Currently, this will then generate:
607
+
608
+ * Code for `ObjectMapper` on iOS (in the future we plan to generate `Sourcery` annotations instead so that people can choose whatever JSON library they prefer).
609
+ * `GSON` annotations (`@SerializedName(…)`) for Android
610
+
611
+ __Example__: On the 'name' attribute of the 'Shop' entity:
612
+
613
+ ![JSONKeyPath](documentation/json.png)
614
+
615
+
616
+ <details>
617
+ <summary>📑 Sample of the generated code in Java (Android)</summary>
618
+
619
+ Sur Android, nous utilisons la librairie GSON
620
+
621
+ ```java
622
+ package com.niji.data;
623
+
624
+ import com.google.gson.annotations.SerializedName;
625
+
626
+ import io.realm.RealmList;
627
+ import io.realm.RealmObject;
628
+
629
+ /* DO NOT EDIT | Generated by gyro */
630
+
631
+ public class Shop extends RealmObject {
632
+
633
+ @SerializedName("json_name")
634
+ private String name;
635
+ private RealmList<Product> products;
636
+ [...]
637
+ }
638
+ ```
639
+ </details>
640
+
641
+ <details>
642
+ <summary>📑 Sample of the generated code in Objective-C (iOS)</summary>
643
+
644
+ On iOS, we use the Realm-JSON library and generate them in `MyEntity+JSON.m` category files.
645
+
646
+ `RLMShop+JSON.m`:
647
+
648
+ ```objc
649
+ // DO NOT EDIT | Generated by gyro
650
+
651
+ ////////////////////////////////////////////////////////////////////////////////
652
+
653
+ #pragma mark - Imports
654
+
655
+ #import "RLMShop+JSON.h"
656
+
657
+ ////////////////////////////////////////////////////////////////////////////////
658
+
659
+ #pragma mark - Implementation
660
+
661
+ @implementation RLMShop (JSON)
662
+
663
+ + (NSDictionary *)JSONInboundMappingDictionary
664
+ {
665
+ return @{
666
+ @"json_name" : @"name"
667
+ };
668
+ }
669
+
670
+ + (NSDictionary *)JSONOutboundMappingDictionary
671
+ {
672
+ return @{
673
+ @"name" : @"json_name"
674
+ };
675
+ }
676
+
677
+ @end
678
+ ```
679
+ </details>
680
+
681
+ #### Combine JSONKeyPath and enums
682
+
683
+ Note that you can **combine that `JSONKeyPath` key with enums** (see [Handling enums](#handling-enums) above). If you declared the User Info keys to make your attribute an enum (`enumType` + `enumValues`) in addition to `JSONKeyPath`, you'll have to also add the `JSONValues` key to list the corresponding values in the JSON for those `enumValues`.
684
+
685
+ | Key | Value |
686
+ |-----|-------|
687
+ | `JSONValues` | `valeur_json_1,valeur_json_2,valeur_json_3` |
688
+
689
+ The number of items listed for that `JSONValues` key must be the same as the number of items listed for the `enumValues` keys, obviously.
690
+
691
+ __Example__:
692
+
693
+ ![enum_json](documentation/enum_json.png)
694
+
695
+
696
+ <details>
697
+ <summary>📑 Sample of the generated code in Java (Android)</summary>
698
+
699
+ ```java
700
+ package com.niji.data;
701
+
702
+ import com.google.gson.annotations.SerializedName;
703
+
704
+ /* DO NOT EDIT | Generated by gyro */
705
+
706
+ public enum Type {
707
+
708
+ @SerializedName("json_type_one")TYPE_ONE,
709
+ @SerializedName("json_type_two")TYPE_TWO,
710
+ @SerializedName("json_type_three")TYPE_THREE
711
+ }
712
+ ```
713
+ </details>
714
+
715
+ <details>
716
+ <summary>📑 Sample of the generated code in Objective-C (iOS)</summary>
717
+
718
+ `RLMShop+JSON.m`:
719
+
720
+ ```objc
721
+ // DO NOT EDIT | Generated by gyro
722
+
723
+ ////////////////////////////////////////////////////////////////////////////////
724
+
725
+ #pragma mark - Imports
726
+
727
+ #import "RLMShop+JSON.h"
728
+ #import "RLMTypes.h"
729
+
730
+ ////////////////////////////////////////////////////////////////////////////////
731
+
732
+ #pragma mark - Implementation
733
+
734
+ @implementation RLMShop (JSON)
735
+
736
+ + (NSValueTransformer *)typeJSONTransformer
737
+ {
738
+ return [MCJSONValueTransformer valueTransformerWithMappingDictionary:@{
739
+ @"json_type_one" : @(RLMTypeOne),
740
+ @"json_type_two" : @(RLMTypeTwo),
741
+ @"json_type_three" : @(RLMTypeThree)
742
+ }];
743
+ }
744
+
745
+ @end
746
+ ```
747
+ </details>
748
+
749
+
750
+ ---
751
+
752
+
753
+ ### Custom ValueTransformers
754
+
755
+ Only available on iOS (as Android uses the GSON library), custom `ValueTransformers` allows you to e.g. convrt a `String` into an `Int` or a `Date` when parsing the JSON. They are only used when using the `--json` flag.
756
+
757
+ To create a specific `ValueTransformer` for a field:
758
+
759
+ * Create your `ValueTransformer` custom class inheriting `NSValueTransformer` and add it to your project
760
+ * Select the attribute that will need this transformer, and in the UserInfo field, add a pair for the **transformer** key whose value should be the name of the `ValueTransformer` class to use:
761
+
762
+ | Key | Value |
763
+ |-----|-------|
764
+ | `transformer` | `NameOfTheTransformerClass` |
765
+
766
+ __Example__:
767
+
768
+ ![transformer](documentation/transformer.png)
769
+
770
+
771
+ <details>
772
+ <summary>📑 Sample of the generated code in Objective-C (iOS)</summary>
773
+
774
+ `gyro` will produce the following code. (In this example, attributes `attrDouble` and `attrInteger32` don't have a **transformer** key set in their UserInfo).
775
+
776
+ ```objc
777
+ // DO NOT EDIT | Generated by gyro
778
+
779
+ ////////////////////////////////////////////////////////////////////////////////
780
+
781
+ #pragma mark - Imports
782
+
783
+ #import "RLMShop+JSON.h"
784
+ #import "MPDecimalTransformer.h"
785
+ #import "MPIntegerTransformer.h"
786
+
787
+ ////////////////////////////////////////////////////////////////////////////////
788
+
789
+ #pragma mark - Implementation
790
+
791
+ @implementation RLMShop (JSON)
792
+
793
+ + (NSDictionary *)JSONInboundMappingDictionary
794
+ {
795
+ return @{
796
+ @"attrDecimal" : @"attrDecimal",
797
+ @"attrDouble" : @"attrDouble",
798
+ @"attrFloat" : @"attrFloat",
799
+ @"attrInteger16" : @"attrInteger16",
800
+ @"attrInteger32" : @"attrInteger32",
801
+ @"attrInteger64" : @"attrInteger64"
802
+ };
803
+ }
804
+
805
+ + (NSDictionary *)JSONOutboundMappingDictionary
806
+ {
807
+ return @{
808
+ @"attrDecimal" : @"attrDecimal",
809
+ @"attrDouble" : @"attrDouble",
810
+ @"attrFloat" : @"attrFloat",
811
+ @"attrInteger16" : @"attrInteger16",
812
+ @"attrInteger32" : @"attrInteger32",
813
+ @"attrInteger64" : @"attrInteger64"
814
+ };
815
+ }
816
+
817
+ + (NSValueTransformer *)attrDecimalJSONTransformer
818
+ {
819
+ return [[MPDecimalTransformer alloc] init];
820
+ }
821
+
822
+ + (NSValueTransformer *)attrFloatJSONTransformer
823
+ {
824
+ return [[MPDecimalTransformer alloc] init];
825
+ }
826
+
827
+ + (NSValueTransformer *)attrInteger16JSONTransformer
828
+ {
829
+ return [[MPIntegerTransformer alloc] init];
830
+ }
831
+
832
+ + (NSValueTransformer *)attrInteger64JSONTransformer
833
+ {
834
+ return [[MPIntegerTransformer alloc] init];
835
+ }
836
+
837
+ @end
838
+
839
+ ```
840
+ </details>