LCOV - code coverage report
Current view: top level - model/relationship - belongs_to.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 34 34 100.0 %
Date: 2021-06-18 12:41:16 Functions: 0 0 -

          Line data    Source code
       1             : part of flutter_data;
       2             : 
       3             : /// A [Relationship] that models a to-one ownership.
       4             : ///
       5             : /// Example: A book that belongs to an author
       6             : /// ```
       7             : /// class Book with DataModel<Book> {
       8             : ///  @override
       9             : ///  final int id;
      10             : ///  final String title;
      11             : ///  final BelongsTo<Author> author;
      12             : ///
      13             : ///  Todo({this.id, this.title, this.author});
      14             : /// }
      15             : ///```
      16             : class BelongsTo<E extends DataModel<E>> extends Relationship<E, E?> {
      17             :   /// Creates a [BelongsTo] relationship, with an optional initial [E] model.
      18             :   ///
      19             :   /// Example:
      20             :   /// ```
      21             :   /// final author = Author(name: 'JK Rowling');
      22             :   /// final book = Book(id: 1, author: BelongsTo(author));
      23             :   /// ```
      24             :   ///
      25             :   /// See also: [DataModelRelationshipExtension<E>.asBelongsTo]
      26           3 :   BelongsTo([final E? model]) : super(model != null ? {model} : null);
      27             : 
      28           1 :   BelongsTo._(String? key, bool _wasOmitted)
      29           2 :       : super._(key != null ? {key} : {}, _wasOmitted);
      30             : 
      31             :   /// For internal use with `json_serializable`.
      32           1 :   factory BelongsTo.fromJson(final Map<String, dynamic> map) {
      33           2 :     final key = map['_'][0] as String?;
      34             :     if (key == null) {
      35           2 :       final wasOmitted = map['_'][1] as bool;
      36           1 :       return BelongsTo._(null, wasOmitted);
      37             :     }
      38           1 :     return BelongsTo._(key, false);
      39             :   }
      40             : 
      41             :   /// Obtains the single [E] value of this relationship (`null` if not present).
      42           2 :   E? get value => first;
      43             : 
      44             :   /// Sets the single [E] value of this relationship, replacing any previous [value].
      45             :   ///
      46             :   /// Passing in `null` will remove the existing value from the relationship.
      47           1 :   set value(E? value) {
      48           1 :     if (this.value != null || value == null) {
      49           2 :       super.remove(this.value!);
      50             :     }
      51             :     if (value != null) {
      52           1 :       super.add(value);
      53             :     }
      54             :     assert(length <= 1);
      55             :   }
      56             : 
      57             :   /// Returns the [value]'s `key`
      58           1 :   @protected
      59             :   @visibleForTesting
      60           2 :   String? get key => super.keys.safeFirst;
      61             : 
      62           3 :   String? get id => super.ids.safeFirst;
      63             : 
      64             :   @override
      65           1 :   Future<Relationship<E, E?>> initialize(
      66             :       {required final Map<String, RemoteAdapter> adapters,
      67             :       required final DataModel owner,
      68             :       required final String name,
      69             :       final String? inverseName}) async {
      70           1 :     if (isInitialized && inverseName != null) {
      71           1 :       addInverse(inverseName, owner);
      72             :     }
      73           1 :     return super.initialize(
      74             :         adapters: adapters, owner: owner, name: name, inverseName: inverseName);
      75             :   }
      76             : 
      77             :   /// Returns a [StateNotifier] which emits the latest [value] of
      78             :   /// this [BelongsTo] relationship.
      79           1 :   @override
      80             :   DelayedStateNotifier<E?> watch() {
      81           6 :     return _graphEvents.where((e) => e.isNotEmpty).map((e) {
      82           1 :       return [DataGraphEventType.removeNode, DataGraphEventType.removeEdge]
      83           3 :               .contains(e.last.type)
      84             :           ? null
      85           1 :           : value;
      86             :     });
      87             :   }
      88             : 
      89           1 :   void addInverse(String inverseName, DataModel model) {
      90           1 :     if (value != null) {
      91           5 :       final _rels = value!.remoteAdapter.localAdapter.relationshipsFor(value!);
      92           1 :       final inverseMetadata = _rels[inverseName];
      93             :       if (inverseMetadata != null) {
      94           1 :         final inverseRelationship = inverseMetadata['instance'] as Relationship;
      95           1 :         inverseRelationship.add(model);
      96             :       }
      97             :     }
      98             :   }
      99             : 
     100           1 :   @override
     101           2 :   String toString() => 'BelongsTo<$E>($_prop)';
     102             : }
     103             : 
     104             : extension DataModelRelationshipExtension<T extends DataModel<T>>
     105             :     on DataModel<T> {
     106             :   /// Converts a [DataModel<T>] into a [BelongsTo<T>].
     107             :   ///
     108             :   /// Equivalent to using the constructor as `BelongsTo(model)`.
     109           2 :   BelongsTo<T> get asBelongsTo => BelongsTo<T>(this as T);
     110             : }

Generated by: LCOV version 1.15