body method

  1. @override
String body(
  1. String path,
  2. String baseName,
  3. String className
)
override

Defines the actual body code. path is passed relative to lib, baseName is the filename, and className is the filename converted to Pascal case.

実際の本体コードを定義します。pathlibからの相対パス、baseNameにファイル名が渡され、classNameにファイル名をパスカルケースに変換した値が渡されます。

Implementation

@override
String body(String path, String baseName, String className) {
  final paths = path.split("/");
  if (paths.length.isEven) {
    path = paths.sublist(0, paths.length - 1).join("/");
  }
  return """
/// Value for model.
@freezed
@formValue
@immutable
// TODO: Set the path for the collection.
@CollectionModelPath("\${1:$path}")
class ${className}Model with _\$${className}Model {
const factory ${className}Model({
   // TODO: Set the data schema.
   \${2}
}) = _${className}Model;
const ${className}Model._();

factory ${className}Model.fromJson(Map<String, Object?> json) => _\$${className}ModelFromJson(json);

/// Query for document.
///
/// ```dart
/// appRef.model(${className}Model.document(id));       // Get the document.
/// ref.app.model(${className}Model.document(id))..load();  // Load the document.
/// ```
static const document = _\$${className}ModelDocumentQuery();

/// Query for collection.
///
/// ```dart
/// appRef.model(${className}Model.collection());       // Get the collection.
/// ref.app.model(${className}Model.collection())..load();  // Load the collection.
/// ref.app.model(
///   ${className}Model.collection().data.equal(
///     "data",
///   )
/// )..load(); // Load the collection with filter.
/// ```
static const collection = _\$${className}ModelCollectionQuery();

/// Query for form value.
///
/// ```dart
/// ref.app.form(${className}Model.form(${className}Model()));    // Get the form controller in app scope.
/// ref.page.form(${className}Model.form(${className}Model()));    // Get the form controller in page scope.
/// ```
static const form = _\$${className}ModelFormQuery();
}

/// [Enum] of the name of the value defined in ${className}Model.
typedef ${className}ModelKeys = _\$${className}ModelKeys;

/// Alias for ModelRef<${className}Model>.
///
/// When defining parameters for other Models, you can define them as follows
///
/// ```dart
/// @RefParam(${className}ModelDocument) ${className}ModelRef $baseName
/// ```
typedef ${className}ModelRef = ModelRef<${className}Model>?;

/// It can be defined as an empty ModelRef<${className}Model>.
///
/// ```dart
/// ${className}ModelRefPath("xxx") // Define as a path.
/// ```
typedef ${className}ModelRefPath = _\$${className}ModelRefPath;

/// Class for defining initial values to be passed to `initialValue` of [RuntimeModelAdapter].
///
/// ```dart
/// RuntimeModelAdapter(
///   initialValue: [
///     ${className}ModelInitialCollection(
///       "xxx": ${className}Model(...),
///     ),
///   ],
/// );
/// ```
typedef ${className}ModelInitialCollection = _\$${className}ModelInitialCollection;

/// Document class for storing ${className}Model.
typedef ${className}ModelDocument = _\$${className}ModelDocument;

/// Collection class for storing ${className}Model.
typedef ${className}ModelCollection = _\$${className}ModelCollection;

/// It can be defined as an empty ModelRef<${className}Model>.
///
/// ```dart
/// ${className}ModelMirrorRefPath("xxx") // Define as a path.
/// ```
typedef ${className}ModelMirrorRefPath = _\$${className}ModelMirrorRefPath;

/// Class for defining initial values to be passed to `initialValue` of [RuntimeModelAdapter].
///
/// ```dart
/// RuntimeModelAdapter(
///   initialValue: [
///     ${className}ModelMirrorInitialCollection(
///       "xxx": ${className}Model(...),
///     ),
///   ],
/// );
/// ```
typedef ${className}ModelMirrorInitialCollection = _\$${className}ModelMirrorInitialCollection;

/// Document class for storing ${className}Model.
typedef ${className}ModelMirrorDocument = _\$${className}ModelMirrorDocument;

/// Collection class for storing ${className}Model.
typedef ${className}ModelMirrorCollection = _\$${className}ModelMirrorCollection;
""";
}