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.isOdd) {
    if (paths.length <= 1) {
      path = (paths..insert(0, "app")).join("/");
    } else {
      path = paths.sublist(0, paths.length - 1).join("/");
    }
  }
  return """
/// Value for model.
@freezed
@formValue
@immutable
// TODO: Set the path for the document.
@DocumentModelPath("\${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());      // Get the document.
///
/// ref.page.form(CounterModel.form(CounterModel()));    // Get the form controller.(${className}Model.document())..load(); // Load the document.
/// ```
static const document = _\$${className}ModelDocumentQuery();

/// 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() // 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}ModelInitialDocument(
///       ${className}Model(...),
///     ),
///   ],
/// );
/// ```
typedef ${className}ModelInitialDocument = _\$${className}ModelInitialDocument;

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

/// It can be defined as an empty ModelRef<${className}Model>.
///
/// ```dart
/// ${className}ModelMirrorRefPath() // 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}ModelMirrorInitialDocument(
///       ${className}Model(...),
///     ),
///   ],
/// );
/// ```
typedef ${className}ModelMirrorInitialDocument = _\$${className}ModelMirrorInitialDocument;

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