CFSync.Entry.Fields behaviour (CFSync v0.18.0)

View Source

behaviour for fields struct.

For each Contentful content-type you want to map in your Elixir app, you have to provide an Elixir module implementing this behaviour.

Summary

Callbacks

Called at struct creation.

Types

t()

@type t() :: struct()

Callbacks

new(data)

@callback new(CFSync.Entry.Extractors.data()) :: t()

Called at struct creation.

When creating an entry, CFSync will call this function with entry's data as first argument. Your implementation must return a struct implementing the CFSync.Entry.Fields protocol.

The struct must be build using the extract_* functions provided by CFSync.Entry.Extractors

You must configure your fields struct in config.exs, see CFSync.

Example

defmodule MyApp.PageFields do
  alias CFSync.Entry

  @behaviour Entry.Fields
  import Entry.Extractors

  defstruct :name, :body, :author

  def new(data) do
    %__MODULE__{
      # field_name: extract_*(data, "fieldNameInContentful", default: default_value)
      name: extract_binary(data, "name", default: "Default name"),
      body: extract_richtext(data, "body"),
      author: extract_link(data, "author"),
    }
  end
end