ArchTest.Pattern (ArchTest v0.2.0)

Copy Markdown View Source

Glob pattern matching for Elixir module names.

Module names are treated as dot-separated segments, and glob patterns follow these semantics:

PatternMatches
"MyApp.Orders.*"Direct children only (MyApp.Orders.Order)
"MyApp.Orders.**"All descendants at any depth
"MyApp.Orders"Exact match only
"**.*Service"Any module whose last segment ends with Service
"**.*Service*"Any module whose last segment contains Service
"MyApp.**.*Repo"Under MyApp, last segment ends with Repo
"**"All modules

Summary

Functions

Compiles a glob pattern string into a Regex.

Filters a list of modules/module name strings to those matching pattern.

Returns true if module_name matches pattern.

Converts a module atom to its string representation without the Elixir. prefix.

Functions

compile(pattern)

@spec compile(String.t()) :: Regex.t()

Compiles a glob pattern string into a Regex.

** matches one or more dot-separated segments (any depth). * within a single segment matches any characters except ..

filter(modules, pattern)

@spec filter([module() | String.t()], String.t()) :: [module() | String.t()]

Filters a list of modules/module name strings to those matching pattern.

matches?(pattern, module_name)

@spec matches?(String.t() | Regex.t(), String.t() | module()) :: boolean()

Returns true if module_name matches pattern.

Accepts either a pattern string or a pre-compiled Regex.

Examples

iex> ArchTest.Pattern.matches?("MyApp.Orders.*", "MyApp.Orders.Order")
true

iex> ArchTest.Pattern.matches?("MyApp.Orders.*", "MyApp.Orders.Schemas.Order")
false

iex> ArchTest.Pattern.matches?("MyApp.Orders.**", "MyApp.Orders.Schemas.Order")
true

iex> ArchTest.Pattern.matches?("**.*Service", "MyApp.Orders.OrderService")
true

iex> ArchTest.Pattern.matches?("**.*Service", "MyApp.Orders.OrderServiceHelper")
false

module_to_string(module)

@spec module_to_string(module()) :: String.t()

Converts a module atom to its string representation without the Elixir. prefix.