StaticContext (StaticContext v0.1.0)

Copy Markdown View Source

Generates standard data access functions for static lookup modules at compile time.

Static lookup modules hold lists of structs defined in code, not the database — think lookup tables like states, types, or categories. The static_context block is the table of contents; everything below it is domain logic.

String ids throughout. Matches DB storage, URL params, and form values with no atom/string boundary to cross.

Usage

import StaticContext

static_context struct: MineState do
  list()
  list_by()
  get()
  get!()
  get_by()
  get_by!()
end

def entries do
  [
    %MineState{id: "production",           name: "Production"},
    %MineState{id: "care_and_maintenance", name: "Care and Maintenance"},
    %MineState{id: "closed",               name: "Closed"}
  ]
end

Generated functions

FunctionSignatureNotes
listlist()All entries via entries/0
list_bylist_by(clauses)Enum.filter with clause key validation
list_forlist_for(assoc, id)Filter by assoc_id foreign key
getget(id)Binary-only guard, nil if not found
get!get!(id)Binary-only guard, raises if not found
get_byget_by(clauses)First match or nil
get_by!get_by!(clauses)First match or raises

get and get! are guarded with when is_binary(id). Passing an atom raises FunctionClauseError — intentional, enforces string ids at the call site.

Summary

Functions

Declares the generated functions for a static lookup module.

Functions

static_context(context_opts_ast, list)

(macro)

Declares the generated functions for a static lookup module.

Accepts a keyword list with :struct pointing to the struct module, and a do block containing function declarations like list(), get!(), get_by().

The calling module must define an entries/0 function returning a list of structs.

See the module documentation for the full list of supported functions.