W3cubDocs

/Phoenix

Phoenix.Naming

Conveniences for inflecting and working with names in Phoenix.

Summary

Functions

camelize(value)

Converts a string to camel case.

humanize(atom)

Converts an attribute/form field into its humanize version.

resource_name(alias, suffix \\ "")

Extracts the resource name from an alias.

underscore(value)

Converts a string to underscore case.

unsuffix(value, suffix)

Removes the given suffix from the name if it exists.

Functions

camelize(value)

Specs

camelize(String.t()) :: String.t()

Converts a string to camel case.

Takes an optional :lower flag to return lowerCamelCase.

Examples

iex> Phoenix.Naming.camelize("my_app")
"MyApp"

iex> Phoenix.Naming.camelize("my_app", :lower)
"myApp"

In general, camelize can be thought of as the reverse of underscore, however, in some cases formatting may be lost:

Phoenix.Naming.underscore "SAPExample"  #=> "sap_example"
Phoenix.Naming.camelize   "sap_example" #=> "SapExample"

camelize(value, atom)

Specs

camelize(String.t(), :lower) :: String.t()

humanize(atom)

Specs

humanize(atom() | String.t()) :: String.t()

Converts an attribute/form field into its humanize version.

Examples

iex> Phoenix.Naming.humanize(:username)
"Username"
iex> Phoenix.Naming.humanize(:created_at)
"Created at"
iex> Phoenix.Naming.humanize("user_id")
"User"

resource_name(alias, suffix \\ "")

Specs

resource_name(String.Chars.t(), String.t()) :: String.t()

Extracts the resource name from an alias.

Examples

iex> Phoenix.Naming.resource_name(MyApp.User)
"user"

iex> Phoenix.Naming.resource_name(MyApp.UserView, "View")
"user"

underscore(value)

Specs

underscore(String.t()) :: String.t()

Converts a string to underscore case.

Examples

iex> Phoenix.Naming.underscore("MyApp")
"my_app"

In general, underscore can be thought of as the reverse of camelize, however, in some cases formatting may be lost:

Phoenix.Naming.underscore "SAPExample"  #=> "sap_example"
Phoenix.Naming.camelize   "sap_example" #=> "SapExample"

unsuffix(value, suffix)

Specs

unsuffix(String.t(), String.t()) :: String.t()

Removes the given suffix from the name if it exists.

Examples

iex> Phoenix.Naming.unsuffix("MyApp.User", "View")
"MyApp.User"

iex> Phoenix.Naming.unsuffix("MyApp.UserView", "View")
"MyApp.User"

© 2014 Chris McCord
Licensed under the MIT License.
https://hexdocs.pm/phoenix/Phoenix.Naming.html