forked from Instagram/IGListKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerating-your-models-using-remodel.html
458 lines (423 loc) · 20.3 KB
/
generating-your-models-using-remodel.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<!DOCTYPE html>
<html lang="en">
<head>
<title>Generating your models using remodel Reference</title>
<link rel="stylesheet" type="text/css" href="css/jazzy.css" />
<link rel="stylesheet" type="text/css" href="css/highlight.css" />
<meta charset='utf-8'>
<script src="js/jquery.min.js" defer></script>
<script src="js/jazzy.js" defer></script>
<script src="js/lunr.min.js" defer></script>
<script src="js/typeahead.jquery.js" defer></script>
<script src="js/jazzy.search.js" defer></script>
</head>
<body>
<a title="Generating your models using remodel Reference"></a>
<header>
<div class="content-wrapper">
<p><a href="index.html">IGListKit 4.1.0 Docs</a> (93% documented)</p>
<p class="header-right"><a href="https://github.com/Instagram/IGListKit"><img src="img/gh.png" alt="GitHub"/>View on GitHub</a></p>
<div class="header-right">
<form role="search" action="search.json">
<input type="text" placeholder="Search documentation" data-typeahead>
</form>
</div>
</div>
</header>
<div class="content-wrapper">
<p id="breadcrumbs">
<a href="index.html">IGListKit Reference</a>
<img id="carat" src="img/carat.png" alt=""/>
Generating your models using remodel Reference
</p>
</div>
<div class="content-wrapper">
<nav class="sidebar">
<ul class="nav-groups">
<li class="nav-group-name">
<a href="Guides.html">Guides</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="best-practices-and-faq.html">Best Practices and FAQ</a>
</li>
<li class="nav-group-task">
<a href="generating-your-models-using-remodel.html">Generating your models using remodel</a>
</li>
<li class="nav-group-task">
<a href="getting-started.html">Getting Started</a>
</li>
<li class="nav-group-task">
<a href="iglistdiffable-and-equality.html">IGListDiffable and Equality</a>
</li>
<li class="nav-group-task">
<a href="installation.html">Installation</a>
</li>
<li class="nav-group-task">
<a href="migration.html">Migration</a>
</li>
<li class="nav-group-task">
<a href="modeling-and-binding.html">Modeling and Binding</a>
</li>
<li class="nav-group-task">
<a href="vision.html">VISION</a>
</li>
<li class="nav-group-task">
<a href="working-with-core-data.html">Working with Core Data</a>
</li>
<li class="nav-group-task">
<a href="working-with-uicollectionview.html">Working with UICollectionView</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Classes.html">Classes</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Classes/IGListAdapter.html">IGListAdapter</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListAdapterUpdater.html">IGListAdapterUpdater</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListBatchUpdateData.html">IGListBatchUpdateData</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListBindingSectionController.html">IGListBindingSectionController</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListCollectionView.html">IGListCollectionView</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListCollectionViewLayout.html">IGListCollectionViewLayout</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListGenericSectionController.html">IGListGenericSectionController</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListIndexPathResult.html">IGListIndexPathResult</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListIndexSetResult.html">IGListIndexSetResult</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListMoveIndex.html">IGListMoveIndex</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListMoveIndexPath.html">IGListMoveIndexPath</a>
</li>
<li class="nav-group-task">
<a href="Classes.html#/c:objc(cs)IGListReloadDataUpdater">IGListReloadDataUpdater</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListSectionController.html">IGListSectionController</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListSingleSectionController.html">IGListSingleSectionController</a>
</li>
<li class="nav-group-task">
<a href="Classes/IGListTransitionData.html">IGListTransitionData</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Constants.html">Constants</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Constants.html#/c:@IGListKitVersionNumber">IGListKitVersionNumber</a>
</li>
<li class="nav-group-task">
<a href="Constants.html#/c:@IGListKitVersionString">IGListKitVersionString</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Enums.html">Enumerations</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Enums/IGListAdapterUpdateType.html">IGListAdapterUpdateType</a>
</li>
<li class="nav-group-task">
<a href="Enums/IGListDiffOption.html">IGListDiffOption</a>
</li>
<li class="nav-group-task">
<a href="Enums/IGListExperiment.html">IGListExperiment</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Protocols.html">Protocols</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Protocols/IGListAdapterDataSource.html">IGListAdapterDataSource</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListAdapterDelegate.html">IGListAdapterDelegate</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListAdapterMoveDelegate.html">IGListAdapterMoveDelegate</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListAdapterPerformanceDelegate.html">IGListAdapterPerformanceDelegate</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListAdapterUpdateListener.html">IGListAdapterUpdateListener</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListAdapterUpdaterDelegate.html">IGListAdapterUpdaterDelegate</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListBatchContext.html">IGListBatchContext</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListBindable.html">IGListBindable</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListBindingSectionControllerDataSource.html">IGListBindingSectionControllerDataSource</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListBindingSectionControllerSelectionDelegate.html">IGListBindingSectionControllerSelectionDelegate</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListCollectionContext.html">IGListCollectionContext</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListCollectionViewDelegateLayout.html">IGListCollectionViewDelegateLayout</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListCollectionViewLayoutCompatible.html">IGListCollectionViewLayoutCompatible</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListDiffable.html">IGListDiffable</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListDisplayDelegate.html">IGListDisplayDelegate</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListScrollDelegate.html">IGListScrollDelegate</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListSingleSectionControllerDelegate.html">IGListSingleSectionControllerDelegate</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListSupplementaryViewSource.html">IGListSupplementaryViewSource</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListTransitionDelegate.html">IGListTransitionDelegate</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListUpdatingDelegate.html">IGListUpdatingDelegate</a>
</li>
<li class="nav-group-task">
<a href="Protocols/IGListWorkingRangeDelegate.html">IGListWorkingRangeDelegate</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Type%20Definitions.html">Type Definitions</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Type%20Definitions/IGListCollectionScrollingTraits.html">IGListCollectionScrollingTraits</a>
</li>
<li class="nav-group-task">
<a href="Type%20Definitions.html#/c:IGListUpdatingDelegate.h@T@IGListCollectionViewBlock">IGListCollectionViewBlock</a>
</li>
<li class="nav-group-task">
<a href="Type%20Definitions.html#/c:IGListUpdatingDelegate.h@T@IGListDataSourceChangeBlock">IGListDataSourceChangeBlock</a>
</li>
<li class="nav-group-task">
<a href="Type%20Definitions.html#/c:IGListUpdatingDelegate.h@T@IGListItemUpdateBlock">IGListItemUpdateBlock</a>
</li>
<li class="nav-group-task">
<a href="Type%20Definitions.html#/c:IGListUpdatingDelegate.h@T@IGListObjectTransitionBlock">IGListObjectTransitionBlock</a>
</li>
<li class="nav-group-task">
<a href="Type%20Definitions.html#/c:IGListUpdatingDelegate.h@T@IGListReloadUpdateBlock">IGListReloadUpdateBlock</a>
</li>
<li class="nav-group-task">
<a href="Type%20Definitions.html#/c:IGListSingleSectionController.h@T@IGListSingleSectionCellConfigureBlock">IGListSingleSectionCellConfigureBlock</a>
</li>
<li class="nav-group-task">
<a href="Type%20Definitions.html#/c:IGListSingleSectionController.h@T@IGListSingleSectionCellSizeBlock">IGListSingleSectionCellSizeBlock</a>
</li>
<li class="nav-group-task">
<a href="Type%20Definitions.html#/c:IGListUpdatingDelegate.h@T@IGListToObjectBlock">IGListToObjectBlock</a>
</li>
<li class="nav-group-task">
<a href="Type%20Definitions.html#/c:IGListUpdatingDelegate.h@T@IGListTransitionDataApplyBlock">IGListTransitionDataApplyBlock</a>
</li>
<li class="nav-group-task">
<a href="Type%20Definitions.html#/c:IGListUpdatingDelegate.h@T@IGListTransitionDataBlock">IGListTransitionDataBlock</a>
</li>
<li class="nav-group-task">
<a href="Type%20Definitions.html#/c:IGListAdapter.h@T@IGListUpdaterCompletion">IGListUpdaterCompletion</a>
</li>
<li class="nav-group-task">
<a href="Type%20Definitions.html#/c:IGListUpdatingDelegate.h@T@IGListUpdatingCompletion">IGListUpdatingCompletion</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Functions.html">Functions</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Functions.html#/c:@F@IGListDiff">IGListDiff</a>
</li>
<li class="nav-group-task">
<a href="Functions.html#/c:@F@IGListDiffPaths">IGListDiffPaths</a>
</li>
<li class="nav-group-task">
<a href="Functions.html#/c:IGListExperiments.h@F@IGListExperimentEnabled">IGListExperimentEnabled</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Structs.html">Structures</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Structs/IGListCollectionScrollingTraits.html">IGListCollectionScrollingTraits</a>
</li>
</ul>
</li>
</ul>
</nav>
<article class="main-content">
<section>
<section class="section">
<h1 id='generating-your-iglistdiffable-models-using-remodel' class='heading'>Generating your IGListDiffable models using remodel</h1>
<p>With the <code><a href="Protocols/IGListDiffable.html">IGListDiffable</a></code> plugin for <a href="https://github.com/facebook/remodel">remodel by facebook</a>, you can automatically generate models conforming to the <code><a href="Protocols/IGListDiffable.html">IGListDiffable</a></code>.</p>
<p>This will automatically implement <code>hash</code>, <code>isEqual:</code> and <code>description</code>, as well as <code>diffIdentifier</code> and <code>isEqualToDiffableObject:</code> for you. Remodel is also capable to generate additional code, like conforming to <code>NSCoding</code> or additional Builder classes for your model object. It will make creating and updating models much easier, faster and safer.</p>
<p>In <code>/remodel-plugin</code>, you can find the source files to build the <code><a href="Protocols/IGListDiffable.html">IGListDiffable</a></code> plugin locally.</p>
<h2 id='installation' class='heading'>Installation</h2>
<h3 id='1-remodel-installation' class='heading'>1. Remodel installation</h3>
<p>Please follow the <a href="https://github.com/facebook/remodel">installation instructions</a> in the main remodel repository.</p>
<p>tl;dr: Either clone the original repository, or use an npm installation. In the latter case you can run <code>which remodel-gen</code> to find out the path of your installation.</p>
<h3 id='2-plugin-installation' class='heading'>2. Plugin installation</h3>
<p>Copy the following files & folders within <code>/remodel-plugin</code> into your local remodel checkout:</p>
<ul>
<li><code>/src/plugins/iglistdiffable.ts</code> - the actual plugin</li>
<li><code>/src/__tests__/plugins/iglistdiffable-test.ts</code> - unit tests</li>
<li><code>/features/iglistdiffable.feature</code> - integration tests</li>
</ul>
<p>And then register the new plugin with the system:</p>
<ul>
<li>Edit <code>/remodel/src/value-object-default-config.ts</code> and add <code>iglistdiffable</code> to the list of basePlugins:</li>
</ul>
<pre class="highlight plaintext"><code>// value-object-default-config.ts
basePlugins: List.of(
'assert-nullability',
'assume-nonnull',
'builder',
'coding',
'copying',
'description',
'equality',
'fetch-status',
'immutable-properties',
'init-new-unavailable',
'use-cpp',
'iglistdiffable'
)
</code></pre>
<h3 id='3-build-plugin' class='heading'>3. Build plugin:</h3>
<p>Once you copied them over and registered the plugin, you have to compile the typescript files into javascript. Do do so run this command from the remodel directory:</p>
<ul>
<li><code>./bin/build</code></li>
</ul>
<h3 id='4-run-tests-optional' class='heading'>4. Run tests (optional)</h3>
<p>To run the unit/integration tests, you can run the following commands:</p>
<ul>
<li><code>./bin/runUnitTests</code></li>
<li><code>./bin/runAcceptanceTests</code></li>
</ul>
<p>This is especially useful if you plan to change/extend the plugin in any way.</p>
<h3 id='5-use-the-plugin' class='heading'>5. Use the plugin</h3>
<p>Now you are ready to generate your <code><a href="Protocols/IGListDiffable.html">IGListDiffable</a></code> conforming models! To generate a model, create a new <code>.value</code> file. Here’s an example:</p>
<pre class="highlight plaintext"><code># PersonModel.value
PersonModel includes(IGListDiffable) {
NSString *firstName
NSString *lastName
%diffIdentifier
NSString *uniqueId
}
</code></pre>
<p>To generate your Objective-C models, run the generation tool like this:</p>
<p><code>./bin/generate path/to/your/PersonModel.value</code></p>
<p>This will generate the following Objective-C files in the same directory:</p>
<pre class="highlight plaintext"><code>// PersonModel.h
@interface PersonModel : NSObject <IGListDiffable, NSCopying>
@property (nonatomic, readonly, copy) NSString *firstName;
@property (nonatomic, readonly, copy) NSString *lastName;
@property (nonatomic, readonly, copy) NSString *uniqueId;
- (instancetype)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName uniqueId:(NSString *)uniqueId;
@end
</code></pre>
<p>and</p>
<pre class="highlight plaintext"><code>// PersonModel.m
@implementation PersonModel
- (instancetype)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName uniqueId:(NSString *)uniqueId
{
if ((self = [super init])) {
_firstName = [firstName copy];
_lastName = [lastName copy];
_uniqueId = [uniqueId copy];
}
return self;
}
- (id<NSObject>)diffIdentifier
{
return _uniqueId;
}
- (BOOL)isEqualToDiffableObject:(nullable id<IGListDiffable>)object
{
return [self isEqual:object];
}
- (BOOL)isEqual:(PersonModel *)object
{
if (self == object) {
return YES;
} else if (self == nil || object == nil || ![object isKindOfClass:[self class]]) {
return NO;
}
return
(_firstName == object->_firstName ? YES : [_firstName isEqual:object->_firstName]) &&
(_lastName == object->_lastName ? YES : [_lastName isEqual:object->_lastName]) &&
(_uniqueId == object->_uniqueId ? YES : [_uniqueId isEqual:object->_uniqueId]);
}
- (id)copyWithZone:(nullable NSZone *)zone
{
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@ - \n\t firstName: %@; \n\t lastName: %@; \n\t uniqueId: %@; \n", [super description], _firstName, _lastName, _uniqueId];
}
- (NSUInteger)hash
{
NSUInteger subhashes[] = {[_firstName hash], [_lastName hash], [_uniqueId hash]};
NSUInteger result = subhashes[0];
for (int ii = 1; ii < 3; ++ii) {
unsigned long long base = (((unsigned long long)result) << 32 | subhashes[ii]);
base = (~base) + (base << 18);
base ^= (base >> 31);
base *= 21;
base ^= (base >> 11);
base += (base << 6);
base ^= (base >> 22);
result = base;
}
return result;
}
@end
</code></pre>
<h2 id='documentation' class='heading'>Documentation</h2>
<p>Please see the main remodel repository for <a href="https://github.com/facebook/remodel">additional documentation</a></p>
</section>
</section>
<section id="footer">
<p>© 2023 <a class="link" href="https://twitter.com/MetaOpenSource" target="_blank" rel="external noopener">Instagram</a>. All rights reserved. (Last updated: 2023-04-07)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
</section>
</article>
</div>
</body>
</html>