Helper Functions

Helper functions can be called on variable invokations to sanitize or enforce a specific type of output. This is useful in circumstances where a filename must be of a specific form or when the form varies between usage.

A concrete example of where this is useful is when rendering ruby projects. In ruby, classes must be defined in camel case, in a file (by convention) of the same name in snake case. The snake_case and camel_case helper functions in this example can do 2 things to make it easier to handle this scenario:

  • First, it means that only 1 variable need be specified for rendering the class and its file
  • Second, it can sanitize a variable input from a user when they specify it one way or another to be certain the filename and classname render as expected.

It is recommended that you use the helper functions each time you use a variable value provided by the user.

Helper functions can be chained together. For example variable.upcase.snake_case is a valid use of the helpers. In fact you might wish to use this precise method call to render a constant name for a variable in languages where this is appropriate.

The following helper functions are supported:

Snake Case

Snake Case converts any given string into its snake case equivalent. For the purpose of demonstration assume that the following variable is defined in the manifest:

{
  "name": "user_class_name",
  "description": "Name of the user class model",
}

If the user while rendering the template enters the following:

Team Member

That value can be rendered in snake case in the filename or within a file by calling .snake_case on its usage like this:

{{user_class_name.snake_case}}

The output would be rendered as:

team_member

Dash Case

Dash Case converts any given string into its dash case equivalent. For the purpose of demonstration assume that the following variable is defined in the manifest:

{
  "name": "application_role_name",
  "description": "Role of the application",
}

If the user while rendering the template enters the following:

Primary service

That value can be rendered in dash case in the filename or within a file by calling .dash_case on its usage like this:

{{application_role_name.dash_case}}

The output would be rendered as:

primary-service

Camel Case

Camel Case converts any given string into its camel case equivalent. For the purpose of demonstration assume that the following variable is defined in the manifest:

{
  "name": "user_class_name",
  "description": "Name of the user class model",
}

If the user while rendering the template enters the following:

team Member

That value can be rendered in camel case in the filename or within a file by calling .camel_case on its usage like this:

{{user_class_name.camel_case}}

The output would be rendered as:

TeamMember

Up Case

Upcase will convert the string to be all upper case. For example, given the following variable:

{
  "name": "user_class_name",
  "description": "Name of the user class model",
}

If the user while rendering the template enters the following:

User

That value can be rendered in upper case in the filename or within a file by calling .upcase on its usage like this:

{{user_class_name.upcase}}

The output would be rendered as:

USER

Down Case

Upcase will convert the string to be all lower case. For example, given the following variable:

{
  "name": "user_class_name",
  "description": "Name of the user class model",
}

If the user while rendering the template enters the following:

User

That value can be rendered in lower case in the filename or within a file by calling .downcase on its usage like this:

{{user_class_name.downcase}}

The output would be rendered as:

user

Pluralize

Pluralize will attempt to convert an English singular string into its plural equivalent.

{
  "name": "user_class_name",
  "description": "Name of the user class model",
}

If the user while rendering the template enters the following:

User

That value can be converted to a plural by calling .pluralize on its usage like this:

{{user_class_name.pluralize}}

The output would be rendered as:

Users

Singularize

Singularize will attempt to convert an English plural string into its singular form.

{
  "name": "user_class_name",
  "description": "Name of the user class model",
}

If the user while rendering the template enters the following:

Users

That value can be converted to its singular form by calling .singularize on its usage like this:

{{user_class_name.singularize}}

The output would be rendered as:

User

Randomize

Randomize will add a random character string at the end of a variable value, to ensure that it is very likely to be unique. For the purpose of demonstration, assume that your manifest has the following variable defined:

{
  "name": "user_1_name",
  "description": "Name of first user seed",
}

If the user while rendering the template enters the following:

Nathan Jones

That value can be randomized by calling it in the template or file name like this:

{{user_1_name.randomize}}

By default the output would be rendered like:

Nathan Jones_Aab08

Sometimes you may require more entropy on your randomization. To satisfy such a requirement you can specify the number of random characters on the end, by passing the number as an argument to randomize. For example the following:

{{user_1_name.randomize(10)}}

Will append 10 random characters on the end of the name and render something like:

Nathan Jones_Aab08Aab08