Skip to content

Commit

Permalink
docs: Documentation modifications, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Nov 10, 2022
1 parent 6de2af7 commit 6598484
Show file tree
Hide file tree
Showing 13 changed files with 1,859 additions and 184 deletions.
886 changes: 858 additions & 28 deletions packages/katana_model/README.md

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions packages/katana_model/lib/adapter/local_model_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ class LocalModelAdapter extends ModelAdapter {
},
);

@override
void setRawData(Map<String, DynamicMap> rawData) {
throw UnsupportedError("This adapter prohibits direct writing of RawData.");
}

@override
Future<DynamicMap> loadDocument(ModelAdapterDocumentQuery query) async {
final data = await database.loadDocument(query);
Expand Down
21 changes: 10 additions & 11 deletions packages/katana_model/lib/adapter/runtime_model_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,16 @@ class RuntimeModelAdapter extends ModelAdapter {
/// Designated database. Please use for testing purposes, etc.
///
/// 指定のデータベース。テスト用途などにご利用ください。
NoSqlDatabase get database => _database ?? sharedDatabase;
NoSqlDatabase get database {
final database = _database ?? sharedDatabase;
if (rawData.isNotEmpty && database._data.isEmpty) {
for (final tmp in rawData!.entries) {
database.setRawData(tmp.key, tmp.value);
}
}
return database;
}

final NoSqlDatabase? _database;

/// A common database throughout the application.
Expand All @@ -57,16 +66,6 @@ class RuntimeModelAdapter extends ModelAdapter {
/// モックアップとして利用する際の実データ。
final Map<String, DynamicMap>? rawData;

@override
void setRawData(Map<String, DynamicMap> rawData) {
if (rawData.isEmpty) {
return;
}
for (final tmp in rawData.entries) {
database.setRawData(tmp.key, tmp.value);
}
}

@override
Future<DynamicMap> loadDocument(ModelAdapterDocumentQuery query) async {
final data = await database.loadDocument(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ mixin SearchableCollectionMixin<TModel extends SearchableDocumentMixin>

/// Search within a collection with [searchText].
///
/// It is [load] internally and the search results are returned in [CollectionBase].
/// It is [reload] internally and the search results are returned in [CollectionBase].
///
/// It is not possible to filter or sort and return values using [CollectionModelQuery] keys in conjunction with a search.
///
/// Sorting, etc., should be done on the client side to sort the search results before returning them.
///
/// [searchText]でコレクション内の検索を行います。
///
/// 内部で[load]され検索結果が[CollectionBase]で返されます。
/// 内部で[reload]され検索結果が[CollectionBase]で返されます。
///
/// 検索と併せて[CollectionModelQuery]のキーを用いたフィルタリングやソートして値を返すことはできません。
///
Expand All @@ -142,7 +142,7 @@ mixin SearchableCollectionMixin<TModel extends SearchableDocumentMixin>
limit: modelQuery.limit,
adapter: modelQuery.adapter,
);
return await load();
return await reload();
}
}

Expand Down
51 changes: 46 additions & 5 deletions packages/katana_model/lib/extension/searchable_document_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,55 @@ part of katana_model;
/// }
/// ```
///
/// Documents targeted for search will be searchable in the collection. The search is made possible by passing a query using [ModelQuery.searchText].
/// A collection that performs searches should have [SearchableCollectionMixin] mixed in so that it can be searched.
/// Then use [SearchableCollectionMixin.search] to perform the search.
///
/// 検索対象にされたドキュメントはコレクションで検索可能になります。検索の際は[ModelQuery.searchText]を用いてクエリを渡すことで検索可能になります。
/// 検索を行うコレクションには[SearchableCollectionMixin]をミックスインして検索できるようにします。
/// その後、[SearchableCollectionMixin.search]を利用して検索を行います。
///
/// ```dart
/// const query = ModelQuery("collection_path", key: "name", search: "mike");
/// final collection = TestCollection(query);
/// await collection.load(false); // search results
/// class SearchableRuntimeMapDocumentModel extends DocumentBase<DynamicMap>
/// with SearchableDocumentMixin<DynamicMap> {
/// SearchableRuntimeMapDocumentModel(super.query, super.value);
///
/// @override
/// DynamicMap fromMap(DynamicMap map) {
/// return ModelFieldValue.fromMap(map);
/// }
///
/// @override
/// DynamicMap toMap(DynamicMap value) {
/// return ModelFieldValue.toMap(value);
/// }
///
/// @override
/// String buildSearchText(DynamicMap value) {
/// return value.get("name", "") + value.get("text", "");
/// }
/// }
///
/// class SearchableRuntimeCollectionModel
/// extends CollectionBase<SearchableRuntimeMapDocumentModel>
/// with SearchableCollectionMixin<SearchableRuntimeMapDocumentModel> {
/// SearchableRuntimeCollectionModel(super.query);
///
/// @override
/// SearchableRuntimeMapDocumentModel create([String? id]) {
/// return SearchableRuntimeMapDocumentModel(
/// modelQuery.create(id),
/// {},
/// );
/// }
/// }
///
/// void search() {
/// final query = CollectionModelQuery(
/// "test",
/// );
///
/// final collection = SearchableRuntimeCollectionModel(query);
/// collection.search("test");
/// }
/// ```
mixin SearchableDocumentMixin<T> on DocumentBase<T> {
/// Field key for default search.
Expand Down
6 changes: 3 additions & 3 deletions packages/katana_model/lib/src/document_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ abstract class DocumentBase<T> extends ChangeNotifier
@protected
@mustCallSuper
DynamicMap filterOnLoad(DynamicMap rawData) {
return rawData;
return ModelFieldValue.fromMap(rawData);
}

/// You can filter the data to be saved.
Expand All @@ -457,7 +457,7 @@ abstract class DocumentBase<T> extends ChangeNotifier
@mustCallSuper
DynamicMap filterOnSave(DynamicMap rawData) {
return {
...rawData,
...ModelFieldValue.toMap(rawData),
kUidFieldKey: modelQuery.path.trimQuery().last(),
};
}
Expand Down Expand Up @@ -503,5 +503,5 @@ abstract class DocumentBase<T> extends ChangeNotifier

@override
// ignore: avoid_equals_and_hash_code_on_mutable_classes
int get hashCode => modelQuery.hashCode;
int get hashCode => modelQuery.hashCode ^ value.hashCode;
}
5 changes: 0 additions & 5 deletions packages/katana_model/lib/src/model_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ abstract class ModelAdapter {

static ModelAdapter? _primary;

/// Set [rawData], such as mock data, to the platform set by the adapter.
///
/// モックデータなどの[rawData]をアダプターで設定されたプラットフォームにセットします。
void setRawData(Map<String, DynamicMap> rawData);

/// Pass [query] to the platform set by the adapter to retrieve the document.
///
/// All return values are [DynamicMap].
Expand Down
52 changes: 29 additions & 23 deletions packages/katana_model/lib/src/model_ref.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ abstract class ModelRefLoaderMixin<T> implements DocumentBase<T> {
/// ロード方法の定義を実装した[ModelRefBuilder]
///
/// リストの順番通りにデータのロードと格納が行われます。
List<ModelRefBuilder> get builder;
List<ModelRefBuilder<T>> get builder;

@override
@protected
Expand Down Expand Up @@ -175,24 +175,24 @@ abstract class ModelRefLoaderMixin<T> implements DocumentBase<T> {
///
/// The procedure is;
///
/// 1. Returns a [ModelRef] containing only the relation information stored in [TValue] via [modelRef].
/// 1. Returns a [ModelRef] containing only the relation information stored in [TSource] via [modelRef].
/// 2. Generates and returns a mixed-in [DocumentBase] with [ModelRefMixin] based on [DocumentModelQuery] via [document].
/// 3. Store the [DocumentBase] generated via [value] in [TValue] and return the updated [TValue].
/// 3. Store the [DocumentBase] generated via [value] in [TSource] and return the updated [TSource].
///
/// モデル間のリレーションを付与しデータのロードを行うためのビルダー。
///
/// [ModelRefLoaderMixin]をミックスインしたときに合わせて定義します。
///
/// 手順としては
///
/// 1. [TValue]に保存されているリレーション情報のみ入った[ModelRef][modelRef]経由で返します。
/// 1. [TSource]に保存されているリレーション情報のみ入った[ModelRef][modelRef]経由で返します。
/// 2. [DocumentModelQuery]を元に[ModelRefMixin]をミックスインした[DocumentBase][document]経由で生成し返します。
/// 3. [value]経由で生成された[DocumentBase][TValue]に保存して、更新した[TValue]を返すようにします。
/// 3. [value]経由で生成された[DocumentBase][TSource]に保存して、更新した[TSource]を返すようにします。
///
/// ```dart
/// @override
/// List<ModelRefBuilder> get builder => [
/// ModelRefBuilder<StreamModel, UserModel>(
/// List<ModelRefBuilder<StreamModel>> get builder => [
/// ModelRefBuilder(
/// modelRef: (value) => value.user,
/// document: (query) => UserModelDocument(query),
/// value: (value, document) {
Expand All @@ -202,31 +202,31 @@ abstract class ModelRefLoaderMixin<T> implements DocumentBase<T> {
/// ];
/// ```
@immutable
class ModelRefBuilder<TValue, TResult> {
class ModelRefBuilder<TSource> {
/// Builder for granting relationships between models and loading data.
///
/// Define [ModelRefLoaderMixin] to match the mix-in.
///
/// The procedure is;
///
/// 1. Returns a [ModelRef] containing only the relation information stored in [TValue] via [modelRef].
/// 1. Returns a [ModelRef] containing only the relation information stored in [TSource] via [modelRef].
/// 2. Generates and returns a mixed-in [DocumentBase] with [ModelRefMixin] based on [DocumentModelQuery] via [document].
/// 3. Store the [DocumentBase] generated via [value] in [TValue] and return the updated [TValue].
/// 3. Store the [DocumentBase] generated via [value] in [TSource] and return the updated [TSource].
///
/// モデル間のリレーションを付与しデータのロードを行うためのビルダー。
///
/// [ModelRefLoaderMixin]をミックスインしたときに合わせて定義します。
///
/// 手順としては
///
/// 1. [TValue]に保存されているリレーション情報のみ入った[ModelRef][modelRef]経由で返します。
/// 1. [TSource]に保存されているリレーション情報のみ入った[ModelRef][modelRef]経由で返します。
/// 2. [DocumentModelQuery]を元に[ModelRefMixin]をミックスインした[DocumentBase][document]経由で生成し返します。
/// 3. [value]経由で生成された[DocumentBase][TValue]に保存して、更新した[TValue]を返すようにします。
/// 3. [value]経由で生成された[DocumentBase][TSource]に保存して、更新した[TSource]を返すようにします。
///
/// ```dart
/// @override
/// List<ModelRefBuilder> get builder => [
/// ModelRefBuilder<StreamModel, UserModel>(
/// List<ModelRefBuilder<StreamModel>> get builder => [
/// ModelRefBuilder(
/// modelRef: (value) => value.user,
/// document: (query) => UserModelDocument(query),
/// value: (value, document) {
Expand All @@ -241,29 +241,35 @@ class ModelRefBuilder<TValue, TResult> {
required this.value,
});

/// Callback to retrieve [ModelRef] stored in [TValue].
/// Callback to retrieve [ModelRef] stored in [TSource].
///
/// [TValue]に格納されている[ModelRef]を取得するためのコールバック。
final ModelRef<TResult>? Function(TValue value) modelRef;
/// [TSource]に格納されている[ModelRef]を取得するためのコールバック。
final ModelRef? Function(TSource value) modelRef;

/// Callback to generate a [DocumentBase] that mixes in a [ModelRefMixin] based on a [DocumentModelQuery] obtained from a [ModelRef].
///
/// [ModelRef]から取得された[DocumentModelQuery]を元に[ModelRefMixin]をミックスインした[DocumentBase]を生成するためのコールバック。
final ModelRefMixin<TResult> Function(DocumentModelQuery modelQuery) document;
final ModelRefMixin Function(DocumentModelQuery modelQuery) document;

/// Callback to store the generated [ModelRefMixin] in [TValue].
/// Callback to store the generated [ModelRefMixin] in [TSource].
///
/// [TValue]に生成された[ModelRefMixin]を格納するためのコールバック。
final TValue Function(TValue value, ModelRefMixin<TResult> document) value;
/// [TSource]に生成された[ModelRefMixin]を格納するためのコールバック。
final TSource Function(
TSource value,
ModelRefMixin document,
) value;

Future<TValue?> _build({
required TValue val,
Future<TSource?> _build({
required TSource? val,
required bool listenWhenPossible,
required Map<DocumentModelQuery, DocumentBase> cacheList,
required void Function(DocumentModelQuery query, DocumentBase document)
onDidLoad,
required DocumentModelQuery loaderModelQuery,
}) async {
if (val == null) {
return val;
}
final ref = modelRef(val);
if (ref == null) {
return val;
Expand Down
8 changes: 2 additions & 6 deletions packages/katana_model/test/model_field_value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ class RuntimeMapDocumentModel extends DocumentBase<DynamicMap> {
RuntimeMapDocumentModel(super.query, super.value);

@override
DynamicMap fromMap(DynamicMap map) {
return ModelFieldValue.fromMap(map);
}
DynamicMap fromMap(DynamicMap map) => map;

@override
DynamicMap toMap(DynamicMap value) {
return ModelFieldValue.toMap(value);
}
DynamicMap toMap(DynamicMap value) => value;
}

class RuntimeCollectionModel extends CollectionBase<RuntimeMapDocumentModel> {
Expand Down
9 changes: 3 additions & 6 deletions packages/katana_model/test/runtime_model_test.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:katana_model/katana_model.dart';

class RuntimeMapDocumentModel extends DocumentBase<DynamicMap> {
RuntimeMapDocumentModel(super.query, [super.value]);

@override
DynamicMap fromMap(DynamicMap map) {
return ModelFieldValue.fromMap(map);
}
DynamicMap fromMap(DynamicMap map) => map;

@override
DynamicMap toMap(DynamicMap value) {
return ModelFieldValue.toMap(value);
}
DynamicMap toMap(DynamicMap value) => value;
}

class RuntimeCollectionModel extends CollectionBase<RuntimeMapDocumentModel> {
Expand Down
8 changes: 2 additions & 6 deletions packages/katana_model/test/searchable_runtime_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ class SearchableRuntimeMapDocumentModel extends DocumentBase<DynamicMap>
SearchableRuntimeMapDocumentModel(super.query, super.value);

@override
DynamicMap fromMap(DynamicMap map) {
return ModelFieldValue.fromMap(map);
}
DynamicMap fromMap(DynamicMap map) => map;

@override
DynamicMap toMap(DynamicMap value) {
return ModelFieldValue.toMap(value);
}
DynamicMap toMap(DynamicMap value) => value;

@override
String buildSearchText(DynamicMap value) {
Expand Down
Loading

0 comments on commit 6598484

Please sign in to comment.