Shaf ships with a couple of generators to simplify the creation of new files. Each generator has an identifier and they are called with shaf generate IDENTIFIER
plus zero or more arguments.
(An identifier is a string of one or more words that uniquely identifies a generator). Generators take the approach of rather generating too much than than too little, with the mindset that it's easier to delete than to write. If you prefer to not generate specs, then pass --no-specs
as an option.
Important: Always run git [stash|commit]
before you generate new files. Generators may create/modify files and then delegate further work to another generator that happens to fail. In that case the generation is only partly performed and the project is in an unknown state. In such case, you would like to be able to easily restore the previous state (e.g git checkout -- .
).
When adding a new resource its recommended to use the scaffold generator. It accepts a resource name and an arbitrary number of attribute:type arguments.
shaf generate scaffold some_resource attr1:string attr2:integer
The scaffold generator will call the model generator and the controller generator, see below.
A new controller is generated with the controller identifier, a resource name and an arbitrary number of attribute:type arguments.
shaf generate controller some_resource attr1:string attr2:integer
This will add a new controller and an integration spec. It will also modify the root resource to include a link the the collection endpoint for some_resource.
A new model is generated with the model identifier, a resource name and an arbitrary number of attribute:type arguments.
shaf generate model some_resource attr1:string attr2:integer
This will add a new model, call the serializer generator and generate a new db migration to created a new table.
A new serializer is generated with the serializer identifier, a resource name and an arbitrary number of attribute arguments.
shaf generate serializer some_resource attr1 attr2
This will add a new serializer, a serializer spec and call the policy generator and the profile generator.
A new policy is generated with the policy identifier, a resource name and an arbitrary number of attribute arguments.
shaf generate policy some_resource attr1 attr2
This will add a new policy.
A new mediatype profile is generated with the profile identifier, a resource name and an arbitrary number of attribute arguments.
shaf generate profile some_resource attr1 attr2
This will add a new profile.
Shaf currently supports 5 db migrations to be generated plus the possibility to generate an empty migration. These are:
generate migration [name]
generate migration add column TABLE_NAME field:type
generate migration add index TABLE_NAME COLUMN_NAME
generate migration create table TABLE_NAME [field:type] [..]
generate migration drop column TABLE_NAME COLUMN_NAME
generate migration rename column TABLE_NAME OLD_NAME NEW_NAME
The field
parameter is the name of the column (to be added resp. altered). The type
parameter specifies the type that the column should have. The following types are supported:
integer
=>Integer
varchar
=>String
string
=>String
text
=>String (
text: true`)blob
=>File
bigint
=>Bignum
double
=>Float
numeric
=>BigDecimal
date
=>Date
timestamp
=>DateTime
time
=>Time
bool
=>TrueClass
boolean
=>TrueClass
index
=>index
(unique: true
)foreign_key
=>Integer
The foreign_key type requires that the referenced table is given. This is done by adding a comma, ,
and the table name. Like:
shaf generate scaffold post user_id:foreign_key,users
See the Sequel migrations documentation for more info. Note: You can also add custom migrations, see Customizations