LCOV - code coverage report
Current view: top level - src/model/relationship - belongs_to.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 21 23 91.3 %
Date: 2020-07-30 22:52:57 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           2 :   BelongsTo([final E model]) : super(model != null ? {model} : null);
      27             : 
      28           1 :   BelongsTo._(String key, bool _wasOmitted)
      29           1 :       : 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 => safeFirst;
      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             :     if (value != null) {
      49           1 :       if (super.isNotEmpty) {
      50           2 :         super.remove(this.value);
      51             :       }
      52           1 :       super.add(value);
      53             :     } else {
      54           2 :       super.remove(this.value);
      55             :     }
      56           2 :     assert(length <= 1);
      57             :   }
      58             : 
      59             :   /// Returns the [value]'s `key`
      60           1 :   @protected
      61             :   @visibleForTesting
      62           2 :   String get key => super.keys.safeFirst;
      63             : 
      64             :   /// Returns a [StateNotifier] which emits the latest [value] of
      65             :   /// this [BelongsTo] relationship.
      66           1 :   @override
      67             :   StateNotifier<E> watch() {
      68           6 :     return _graphEvents.where((e) => e.isNotEmpty).map((e) {
      69           4 :       return e.last.type == DataGraphEventType.removeNode ? null : value;
      70             :     });
      71             :   }
      72             : 
      73           0 :   @override
      74           0 :   String toString() => 'BelongsTo<$E>($value)';
      75             : }
      76             : 
      77             : extension DataModelRelationshipExtension<T extends DataModel<T>>
      78             :     on DataModel<T> {
      79             :   /// Converts a [DataModel<T>] into a [BelongsTo<T>].
      80             :   ///
      81             :   /// Equivalent to using the constructor as `BelongsTo(model)`.
      82           2 :   BelongsTo<T> get asBelongsTo => BelongsTo<T>(this as T);
      83             : }

Generated by: LCOV version 1.14