View Source CFSync.Entry.Fields behaviour (CFSync v0.7.0)

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.

Link to this section Summary

Callbacks

Called at struct creation.

Link to this section Types

Link to this section Callbacks

@callback new({map(), binary()}) :: 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

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_value \\ nil)
      name: extract_binary(data, "name", "Default name"),
      body: extract_richtext(data, "body"),
      author: extract_link(data, "author"),
    }
  end
end