Skip to content

Commit

Permalink
Merge pull request #25 from izumin5210/builder
Browse files Browse the repository at this point in the history
Add builder method to generated store class
  • Loading branch information
Masayuki IZUMI committed Nov 29, 2015
2 parents ea9c54e + a1207da commit 6d1fdab
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public FieldSpec apply(ReducerModel input) {

private MethodSpec createBuilderConstructor() {
return MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addModifiers(Modifier.PRIVATE)
.addStatement("$N = new $T<>()", BuilderModel.MIDDLEWARES_VARIABLE_NAME, ArrayList.class)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private TypeSpec createTypeSpec() {
.addSuperinterface(storeModel.getInterfaceName())
.addFields(createFieldSpecs())
.addMethod(createConstructor())
.addMethod(createBuilderMethodSpec())
.addMethods(createGetterMethodSpecs())
.addType(new StoreBuilderClassGenerator(storeModel).createBuilderTypeSpec())
.build();
Expand Down Expand Up @@ -97,6 +98,14 @@ public String apply(StoreImplModel input) {
.build();
}

private MethodSpec createBuilderMethodSpec() {
return MethodSpec.methodBuilder(StoreModel.BUILDER_METHOD_NAME)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(storeModel.getBuilderModel().getClassName())
.addStatement("return new $T()", storeModel.getBuilderModel().getClassName())
.build();
}

private List<MethodSpec> createGetterMethodSpecs() {
return FluentIterable.from(storeModel.getMethodModels())
.transform(new Function<StoreMethodModel, MethodSpec>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class StoreModel {

public static final String MIDDLEWARES_FIELD_NAME = "middlewares";
public static final String ATTACH_MIDDLEWARE_METHOD_NAME = "onAttach";
public static final String BUILDER_METHOD_NAME = "builder";

private static final String CLASS_NAME_PREFIX = "Droidux";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public static class Counter {
" }",
" }",
"",
" public static Builder builder() {",
" return new Builder();",
" }",
"",
" public Counter counter() {",
" return counterStoreImpl.getState();",
" }",
Expand All @@ -143,7 +147,7 @@ public static class Counter {
" private CounterReducer counterReducer;",
" private Counter counter;",
"",
" public Builder() {",
" private Builder() {",
" middlewares = new ArrayList<>();",
" }",
"",
Expand Down Expand Up @@ -222,6 +226,10 @@ public static class CombinedTwoReducers {
" }",
" }",
"",
" public static Builder builder() {",
" return new Builder();",
" }",
"",
" public Counter counter() {",
" return counterStoreImpl.getState();",
" }",
Expand Down Expand Up @@ -249,7 +257,7 @@ public static class CombinedTwoReducers {
" private Counter counter;",
" private TodoList todoList;",
" ",
" public Builder() {",
" private Builder() {",
" middlewares = new ArrayList<>();",
" }",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
store = new DroiduxRootStore.Builder()
store = DroiduxRootStore.builder()
.addMiddleware(new Logger())
.setReducer(new TodoListReducer())
.setInitialState(new TodoList(new ArrayList<>()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
store = new DroiduxRootStore.Builder()
store = DroiduxRootStore.builder()
.addMiddleware(new Logger())
.setReducer(new TodoListReducer())
.setInitialState(new TodoList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AppComponent getComponent() {
}

private void setupStore() {
store = new DroiduxRootStore.Builder()
store = DroiduxRootStore.builder()
.setInitialState(new TodoList())
.setReducer(new TodoListReducer())
.build();
Expand Down

0 comments on commit 6d1fdab

Please sign in to comment.