Injects the plumbing that lets an Ecto.Schema be backed by a Google
Spreadsheet list (tab).
Usage
defmodule MyApp.Account do
use EctoGSS.Schema, {
:model,
columns: ["A", "Y"],
list: "All Accounts",
spreadsheet: "1h85keViqbRzgTN245gEw5s9roxpaUtT7i-mNXQtT8qQ"
}
use Ecto.Schema
schema "accounts" do
field(:nickname, EctoGSS.Schema.AllAccounts.A)
field(:email, EctoGSS.Schema.AllAccounts.Y)
end
enduse EctoGSS.Schema, {:model, opts} generates, at compile time:
- one
Ecto.Typemodule per entry incolumns:, namedEctoGSS.Schema.<ListNameWithoutSpaces>.<Column>(spaces inlist:are stripped to build the module name, e.g. list"All Accounts"and column"A"produceEctoGSS.Schema.AllAccounts.A). Each generated type module knows the spreadsheet column letter it maps to and is meant to be used as the type of a field in the host schema's ownschema/2block; spreadsheet/0,list/0andgss_schema/0functions injected into the host schema module, used byEctoGSS.Repoto locate the backing sheet and mark the module as a GSS-backed schema.
Locating the spreadsheet
There are two ways to tell EctoGSS.Repo which spreadsheet and list to use:
- pass
spreadsheet:(andlist:) explicitly inopts, as shown above; or - omit
spreadsheet:and instead rely on the host schema's@schema_prefixto supply the spreadsheet id:EctoGSS.Repofalls back to__schema__(:prefix)wheneverspreadsheet/0returnsnil.
list: has no such fallback and is always required: omitting it raises
ArgumentError when use EctoGSS.Schema, {:model, opts} expands.
Compile-time vs. runtime option
list: and columns: must be compile-time literals: they are used to name
the generated Ecto.Type modules while this macro is expanding, so their
values must be known then. spreadsheet:, on the other hand, is only
unquoted into the generated spreadsheet/0 function body and may be any
expression valid in the host module (e.g. a module attribute or a call).