View Source Confeature behaviour (Confeature v0.1.2)

Defines a macro for implementing your own feature settings store on top of Ecto and the cache store of your choice.

You can simply declare your module like this:

defmodule MyApp.Confeature do
  use Confeature,
      ecto_repo: MyApp.Repo,
      cache: MyApp.Cache.Feature
end

Providing an ecto_repo is mandatory, but by default, Confeature will use a placeholder cache module. Please refer to the Confeature.Cache module doc for detailed instructions on how to implement your cache.

The functions docs below assume that you have the following feature modules declared, as references:

defmodule MyApp.Features.UsageAlert do
  defstruct [:threshold]

  @type t :: %__MODULE__{
    threshold: float()
  }
end

defmodule MyApp.Features.HiddenPixel do
  defstruct [:enabled, :x, :y]

  @type t :: %__MODULE__{
    enabled: boolean(),
    x: integer(),
    y: integer()
  }
end

Summary

Callbacks

Deletes the feature row from your database and invalidates the cache.

Disables a feature. This is a helper function, and it requires you to declare the enabled boolean field on the feature struct.

Enables a feature. This is a helper function, and it requires you to declare the enabled boolean field on the feature struct.

Returns true if your feature is enabled. This is a helper function, and it requires you to declare the enabled boolean field on the feature struct.

Returns a feature struct based on the module name.

Writes new settings using a feature struct. It'll write to the database and invalidate cache right after.

Callbacks

@callback delete!(name :: atom()) :: {:ok, any()}

Deletes the feature row from your database and invalidates the cache.

You may want to call this function once you're completely done with a feature (eg. in a post-release Ecto migration).

@callback disable(name :: atom()) :: {:ok, struct()}

Disables a feature. This is a helper function, and it requires you to declare the enabled boolean field on the feature struct.

MyApp.Confeature.disable(MyApp.Features.UsageAlert)
=> *will throw an error*

MyApp.Confeature.disable(MyApp.Features.HiddenPixel)
=> {:ok, *your_updated_struct*}
@callback enable(name :: atom()) :: {:ok, struct()}

Enables a feature. This is a helper function, and it requires you to declare the enabled boolean field on the feature struct.

MyApp.Confeature.enable(MyApp.Features.UsageAlert)
=> *will throw an error*

MyApp.Confeature.enable(MyApp.Features.HiddenPixel)
=> {:ok, *your_updated_struct*}
@callback enabled?(name :: atom()) :: boolean()

Returns true if your feature is enabled. This is a helper function, and it requires you to declare the enabled boolean field on the feature struct.

MyApp.Confeature.enabled?(MyApp.Features.UsageAlert)
=> *will throw an error*

MyApp.Confeature.enabled?(MyApp.Features.HiddenPixel)
=> false
@callback get(name :: atom()) :: struct() | nil

Returns a feature struct based on the module name.

MyApp.Confeature.get(MyApp.Features.UsageAlert)
# If you never set it:
=> nil

# After setting it:
=> %MyApp.Features.UsageAlert{threshold: 4.5}
@callback set(struct :: struct()) :: {:ok, struct()}

Writes new settings using a feature struct. It'll write to the database and invalidate cache right after.

This function allows incremental updates; parameters you don't provide won't get erased.

MyApp.Confeature.set(%MyApp.Features.HiddenPixel{x: 43, y: 219})
=> {:ok, *your_updated_struct*}