metadata.ex (1719B)
1 defmodule Ecto.Schema.Metadata do 2 @moduledoc """ 3 Stores metadata of a struct. 4 5 ## State 6 7 The state of the schema is stored in the `:state` field and allows 8 following values: 9 10 * `:built` - the struct was constructed in memory and is not persisted 11 to database yet; 12 * `:loaded` - the struct was loaded from database and represents 13 persisted data; 14 * `:deleted` - the struct was deleted and no longer represents persisted 15 data. 16 17 ## Source 18 19 The `:source` tracks the (table or collection) where the struct is or should 20 be persisted to. 21 22 ## Prefix 23 24 Tracks the source prefix in the data storage. 25 26 ## Context 27 28 The `:context` field represents additional state some databases require 29 for proper updates of data. It is not used by the built-in adapters of 30 `Ecto.Adapters.Postgres` and `Ecto.Adapters.MySQL`. 31 32 ## Schema 33 34 The `:schema` field refers the module name for the schema this metadata belongs to. 35 """ 36 defstruct [:state, :source, :context, :schema, :prefix] 37 38 @type state :: :built | :loaded | :deleted 39 40 @type context :: any 41 42 @type t(schema) :: %__MODULE__{ 43 context: context, 44 prefix: Ecto.Schema.prefix(), 45 schema: schema, 46 source: Ecto.Schema.source(), 47 state: state 48 } 49 50 @type t :: t(module) 51 52 defimpl Inspect do 53 import Inspect.Algebra 54 55 def inspect(metadata, opts) do 56 %{source: source, prefix: prefix, state: state, context: context} = metadata 57 58 entries = 59 for entry <- [state, prefix, source, context], 60 entry != nil, 61 do: to_doc(entry, opts) 62 63 concat(["#Ecto.Schema.Metadata<"] ++ Enum.intersperse(entries, ", ") ++ [">"]) 64 end 65 end 66 end