Check out a free preview of the full Rapid Application Development with Code Generation course

The "Model Name Variations" Lesson is part of the full, Rapid Application Development with Code Generation course featured in this preview video. Here's what you'd learn in this lesson:

Lukas walks through building out a name variation generator, including base variations, plural versions, and adding parameters. This domains model variations object can be used to quickly and efficiently hydrate templates.

Preview
Close

Transcript from the "Model Name Variations" Lesson

[00:00:00]
>> Back to our schema, we need to generate name variations. And once we generate name variations, we are in a good place to actually plug that into a template. So let me close this for space. And as with anything, the first thing I do is I ask myself, how do I model the models?

[00:00:24]
So I'm starting with a basic schema and then I'm adding in a name variations property. And I'm asking myself, when I'm generating a template, what are the most common things that I need? What are the most common name variations that I need to plug it into a template and to make it work?

[00:00:47]
And so I think of this and I worked around this, I polished it and I won't get hang up so much on he said ref and refs and what does that mean, is that typically I try to think or arrive at a semantic or set of semantics that is self documenting and that feels natural to me.

[00:01:09]
So we have refs, models, selectors and model plans. And so all I've done is I've defined these in an interface and then I have two additional ones here that are optional. So this is what this question mark means. And so, based on this here, what ideally we want to generate is something that looks like this.

[00:01:39]
So a ref is camel case. A model, and so this would be like a case name, it's Pasco case selectors. And so this is distinctly angular, but it could be CSS, whatever. These are kabob cases. And then these parameters are something a little different. So, I think of these are just strongly typed parameters singular and plural.

[00:02:04]
And so the challenge here is we need to first build our base. And then we need to add our parameters. And so I have these two empty functions here, buildBase and addParams, and I have a pipe. And then from here, we can go ahead and trace this out.

[00:02:28]
So when we're all said and done, we should hopefully have something that looks like this. So let me just double check. We have our schema here. And so in our base we need to return the name variations object. And clearly this is definitely not a name variations object.

[00:02:55]
So, what we're gonna do is, we're gonna go ref, and I'm gonna, and this is just for me preferential if I have to generate or build an object, then I just will type out the properties first. And so this is even as I write code, it's kind of horizontal.

[00:03:13]
Models selector and selectors. All right, so for a ref, what we need is you'll notice here, this is camel case. So I'm gonna go camel case. And I'm going to go schema.model. And for refs, same exact thing. And I'm gonna go schema.model Plural. And then for models you'll see here, this is pascal case, so I'm just gonna go pascalcase schema.model.

[00:03:59]
And, We'll do the same thing for models. The difference is that I'm making this plural. And again, if the English language was consistent, I may do this a little differently but it's not. And so pony, ponies and even potato, potatoes. And for selectors, we are going to go kebab case.

[00:04:27]
And the difference is, we're just going single model, and plural. All right, let's save this out. All right, and then, let's do our add params. So now this is taking a named variations object and we're going to return a new object. So now using the spread operator, what I can do here is just add in the variations.

[00:04:59]
And then I'm going to add in the two, I believe it's single param and multi param. So buildSingleParam, which is gonna take in variations. And buildMultiParam, also going to take in variations. And you'll notice here that, if I can drag this over, now, we have user course, user courses.

[00:05:50]
So, camel case, Pascal case, kebab case and then our params. And, I can hear it now. Like, this is it. What's interesting about this though, when I think of writing a code generator, this in my opinion is the engine. So if code is characters, characters become strings, is that your ability to intelligently manipulate strings determines or fetch your ability to write code generators.

[00:06:35]
And so if someone said, what does the engine look like for your code generator? Well, my answer would be, this right here. Within a whopping 60 ish lines of code, this is my generator. Basic string manipulation. But this produces something that is invaluable, which is this domain model variations object which I will use to hydrate my templates.

[00:07:20]
So that is, String manipulation 101. Array.join, array.split, array.join. And this very basic transform pipe method which then allows me to generate an object like this.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now