github.com/bscott/buffalo@v0.11.1/generators/resource/generator.go (about) 1 package resource 2 3 import ( 4 "errors" 5 "os" 6 "strings" 7 8 "github.com/gobuffalo/buffalo/meta" 9 "github.com/markbates/inflect" 10 ) 11 12 // Generator for generating a new resource 13 type Generator struct { 14 App meta.App `json:"app"` 15 Name inflect.Name `json:"name"` 16 Model inflect.Name `json:"model"` 17 SkipMigration bool `json:"skip_migration"` 18 SkipModel bool `json:"skip_model"` 19 SkipTemplates bool `json:"skip_templates"` 20 UseModel bool `json:"use_model"` 21 FilesPath string `json:"files_path"` 22 ActionsPath string `json:"actions_path"` 23 Props []Prop `json:"props"` 24 Args []string `json:"args"` 25 } 26 27 // New constructs new options for generating a resource 28 func New(modelName string, args ...string) (Generator, error) { 29 o := Generator{ 30 Args: args, 31 } 32 pwd, _ := os.Getwd() 33 o.App = meta.New(pwd) 34 35 if len(o.Args) > 0 { 36 o.Name = inflect.Name(o.Args[0]) 37 o.Model = inflect.Name(o.Args[0]) 38 } 39 o.Props = modelPropertiesFromArgs(o.Args) 40 41 o.FilesPath = o.Name.PluralUnder() 42 o.ActionsPath = o.FilesPath 43 if strings.Contains(string(o.Name), "/") { 44 parts := strings.Split(string(o.Name), "/") 45 o.Model = inflect.Name(parts[len(parts)-1]) 46 o.ActionsPath = inflect.Underscore(o.Name.Resource()) 47 } 48 if modelName != "" { 49 o.Model = inflect.Name(modelName) 50 } 51 return o, o.Validate() 52 } 53 54 // Validate that the options have what you need to build a new resource 55 func (o Generator) Validate() error { 56 if len(o.Args) == 0 && o.Model == "" { 57 return errors.New("you must specify a resource name") 58 } 59 return nil 60 }