You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Model Citizen is an annotation based model factory for Java, inspired by [FactoryGirl](https://github.com/thoughtbot/factory_girl)
6
4
7
-
A Model is mapped by a _@Blueprint_ using annotated fields. Blueprints contain
8
-
default values and references to other @Blueprinted models. The _ModelFactory_ can
5
+
A Model is mapped by a _@Blueprint_ using annotated fields. Blueprints contain
6
+
default values and references to other @Blueprinted models. The _ModelFactory_ can
9
7
create models based on registered blueprints. A model already created can be
10
8
passed into the _ModelFactory_ as a reference model, which will be used as the basis
11
-
for the new Model.
9
+
for the new Model.
12
10
13
11
14
12
<https://github.com/mguymon/model-citizen>
@@ -24,7 +22,7 @@ for the new Model.
24
22
<version>0.8.2</version>
25
23
</dependency>
26
24
27
-
## Blueprint
25
+
## Blueprint
28
26
29
27
A blueprint is a Class annotated with _@Blueprint( Class )_ and contains annotated fields. Everything else is ignored by the _ModelFactory_.
30
28
Model Citizen [own blueprints](https://github.com/mguymon/model-citizen/tree/master/core/src/test/java/com/tobedevoured/modelcitizen/blueprint) are a great example of how they work.
@@ -39,22 +37,22 @@ Model Citizen [own blueprints](https://github.com/mguymon/model-citizen/tree/mas
39
37
*_force_: [boolean] Force the value of the Default to always be set, even if the target field already has a value. Default is false. This is useful for overriding primatives or Collections.
40
38
41
39
**@Mapped**: The value is mapped to another @Blueprint, default is the blueprint for
42
-
matching field's Class. Mapped class can be set by the _target_ param.
40
+
matching field's Class. Mapped class can be set by the _target_ param.
43
41
44
42
**@MappedList**: Creates a List of Models mapped to another blueprint.
45
43
*_size_: [int] Number of Models to be created by the ModelFactory and added to List, defaults to 1.
46
44
*_target_: [Class] The target Blueprint Class used to create Models
47
45
*_targetList_: [Class] The List created, defaults to ArrayList
48
46
*_ignoreEmpty_: [boolean] If true, do not create Models for an empty Set. Defaults to true.
49
47
*_force_: [boolean] Force the value of the MappedList to always be set, even if the target field already has a value. Default is false.
50
-
48
+
51
49
**@MappedSet**: Creates a Set of Models mapped to another blueprint.
52
50
*_size_: [int] Number of Models to be created by the ModelFactory and added to Set, defaults to 1.
53
51
*_target_: [Class] The target Blueprint Class used to create Models
54
52
*_targetSet_: [Class] The Set created, defaults to HashSet
55
53
*_ignoreEmpty_: [boolean] If true, do not create Models for an empty Set. Defaults to true.
56
54
*_force_: [boolean] Force the value of the MappedSet to always be set, even if the target field already has a value. Default is false.
57
-
55
+
58
56
**@Nullable**: Specifies this field can be null and not to set a value by the ModelFactory.
59
57
60
58
### Callbacks
@@ -73,46 +71,46 @@ A Blueprint will inherit the fields of its parent, except for `ConstructorCallba
73
71
74
72
## Model
75
73
76
-
Presently only supports [template for JavaBean Models](https://github.com/mguymon/model-citizen/blob/master/core/src/main/java/com/tobedevoured/modelcitizen/template/JavaBeanTemplate.java).
74
+
Presently only supports [template for JavaBean Models](https://github.com/mguymon/model-citizen/blob/master/core/src/main/java/com/tobedevoured/modelcitizen/template/JavaBeanTemplate.java).
77
75
For annotations to work with the template, the model must follow the [JavaBean](http://en.wikibooks.org/wiki/Java_Programming/Java_Beans) getter and setters
78
76
for fields.
79
77
80
78
### Working with primitives
81
79
82
-
[Primitive fields are intialized as zero](http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5) and
83
-
will never be null. This will cause the _ModelFactory_ to think that a value has already been assigned to the model and
80
+
[Primitive fields are intialized as zero](http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5) and
81
+
will never be null. This will cause the _ModelFactory_ to think that a value has already been assigned to the model and
84
82
not assign one from the blueprint. To work around this, use the `force=true` in the blueprint to force that value is always assigned from the blueprint, for example:
85
83
86
84
@Blueprint(Car.class)
87
85
public class CarBlueprint {
88
-
86
+
89
87
@Default(force=true)
90
88
float mileage = 100.1f;
91
89
92
90
## ModelFactory
93
91
94
-
Creates new instances of Models based on registered Blueprints. It is possible to
92
+
Creates new instances of Models based on registered Blueprints. It is possible to
95
93
[register Blueprints by package](https://github.com/mguymon/model-citizen/wiki/Register-By-Package).
96
94
97
-
A new instance is always constructed, unless specified by a
95
+
A new instance is always constructed, unless specified by a
A Model with a registered Blueprint can then be created by Class:
106
104
107
105
modelFactory.createModel(Car.class);
108
-
109
-
or by passing a Model directly with override values:
106
+
107
+
or by passing a Model directly with override values:
110
108
111
109
Car car = new Car();
112
110
car.setMake( "Truck" );
113
-
111
+
114
112
# create a new Model using blueprint defaults, but overriding the Make to be Truck.
115
-
car = modelFactory.createModel(car);
113
+
car = modelFactory.createModel(car);
116
114
117
115
## A Simple Example
118
116
@@ -132,56 +130,56 @@ or by passing a Model directly with override values:
132
130
133
131
@Blueprint(Car.class)
134
132
public class CarBlueprint {
135
-
133
+
136
134
@Default
137
135
String make = "car make";
138
-
136
+
139
137
@Default
140
138
String manufacturer = "car manufacturer";
141
-
139
+
142
140
@Default
143
141
Integer mileage = 100;
144
-
142
+
145
143
@Default
146
144
Map status = new HashMap();
147
145
}
148
-
146
+
149
147
### The Car Model
150
148
151
149
public class Car {
152
150
private String make;
153
151
private String manufacturer;
154
152
private Integer mileage;
155
153
private Map status;
156
-
154
+
157
155
public String getMake() {
158
156
return make;
159
157
}
160
-
158
+
161
159
public void setMake(String make) {
162
160
this.make = make;
163
161
}
164
-
162
+
165
163
public String getManufacturer() {
166
164
return manufacturer;
167
165
}
168
-
166
+
169
167
public void setManufacturer(String manufacturer) {
170
168
this.manufacturer = manufacturer;
171
169
}
172
-
170
+
173
171
public Integer getMileage() {
174
172
return mileage;
175
173
}
176
-
174
+
177
175
public void setMileage(Integer mileage) {
178
176
this.milage = mileage;
179
177
}
180
-
178
+
181
179
public Map getStatus() {
182
180
return status;
183
181
}
184
-
182
+
185
183
public void setStatus(Map status) {
186
184
this.status = status;
187
185
}
@@ -200,7 +198,7 @@ There is an optional jar that provides additional support for Spring:
200
198
<artifactId>spring</artifactId>
201
199
<version>0.8.1</version>
202
200
</dependency>
203
-
201
+
204
202
### Avoiding Spring jar collisions
205
203
206
204
ModelFactory should work with Spring 3.x, so you can easily exclude ModelFactory's Spring depedency and use the existing one in your pom.
@@ -216,7 +214,7 @@ ModelFactory should work with Spring 3.x, so you can easily exclude ModelFactory
216
214
</exclusion>
217
215
</exclusions>
218
216
</dependency>
219
-
217
+
220
218
### What does this give me?
221
219
222
220
This provides a new class [ModelFactoryBean](https://github.com/mguymon/model-citizen/blob/master/spring/src/main/java/com/tobedevoured/modelcitizen/spring/ModelFactoryBean.java) and annotation [@SpringBlueprint](https://github.com/mguymon/model-citizen/blob/master/spring/src/main/java/com/tobedevoured/modelcitizen/spring/annotation/SpringBlueprint.java). When the ModelFactoryBean is registered with Spring, it allows the `@SpringBlueprint` and models created by the ModelFactoryBean to be injected by Spring.
@@ -235,12 +233,12 @@ This provides a new class [ModelFactoryBean](https://github.com/mguymon/model-ci
235
233
236
234
@Autowired
237
235
SportsCarRepository sportsCarRepository;
238
-
236
+
239
237
@Default
240
238
Integer topSpeed = 100;
241
239
}
242
-
243
-
240
+
241
+
244
242
## License
245
243
246
244
Licensed to the Apache Software Foundation (ASF) under one or more
0 commit comments