diff --git a/angel_serialize_generator/lib/angel_serialize_generator.dart b/angel_serialize_generator/lib/angel_serialize_generator.dart index be7a2e39..fb22821e 100644 --- a/angel_serialize_generator/lib/angel_serialize_generator.dart +++ b/angel_serialize_generator/lib/angel_serialize_generator.dart @@ -69,6 +69,7 @@ class JsonModelGenerator extends GeneratorForAnnotation { } generateConstructor(ctx, clazz, file); + generateChangeMethod(ctx, clazz, file); })); } @@ -86,4 +87,15 @@ class JsonModelGenerator extends GeneratorForAnnotation { } })); } + + /// Generate a `change` method. + void generateChangeMethod( + BuildContext ctx, ClassBuilder clazz, FileBuilder file) { + clazz.methods.add(new Method((method) { + method + ..name = 'change' + ..returns = ctx.modelClassType + ..requiredParameters.add(new Parameter()); + })); + } } diff --git a/angel_serialize_generator/lib/context.dart b/angel_serialize_generator/lib/context.dart index 457d7fc6..9bf66a9a 100644 --- a/angel_serialize_generator/lib/context.dart +++ b/angel_serialize_generator/lib/context.dart @@ -1,10 +1,12 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:angel_serialize/angel_serialize.dart'; +import 'package:code_builder/code_builder.dart'; import 'package:recase/recase.dart'; /// A base context for building serializable classes. class BuildContext { ReCase _modelClassNameRecase; + TypeReference _modelClassType; /// A map of field names to resolved names from `@Alias()` declarations. final Map aliases = {}; @@ -30,7 +32,11 @@ class BuildContext { : originalClassName; /// A [ReCase] instance reflecting on the [modelClassName]. - ReCase get modelClassNameRecase => _modelClassNameRecase ??= new ReCase(modelClassName); + ReCase get modelClassNameRecase => + _modelClassNameRecase ??= new ReCase(modelClassName); + + TypeReference get modelClassType => + _modelClassType ??= new TypeReference((b) => b.symbol = modelClassName); /// The [FieldElement] pointing to the primary key. FieldElement get primaryKeyField =>