Data stores
Data stores are key-value stores that store a piece of content with meta (the value) and reference it with a string uid (the key).
Dragonfly uses these to store data which can later be fetched, e.g.
Given any Dragonfly job
it can be stored with
and later fetched with
Models
Models simply hold a reference to the uid and do the storing and fetching behind the scenes at the appropriate times - see Models for more details.
File data store
This is the default, but it can be manually configured using
or with options
You can specify the storage path per-content with
To see how to do this with models, see Models - Storage Options
Memory data store
The Memory data store keeps everything in memory and is useful for things like tests.
To use:
You can also specify the uid on store
Other data stores
The following datastores previously in Dragonfly core are now in separate gems:
Building a custom data store
Data stores need to implement three methods: write
, read
and destroy
.
The above should be fairly self-explanatory, but to be a bit more specific:
write
- takes a content object (see Dragonfly::Content for more details) and uses a method like
data
(String),file
,path
to get its data andmeta
to get its meta - also takes an options hash, passing through any options passed to
store
- returns a unique String uid
read
- takes a String uid
- returns a 2-item array; the data in the form of a String, Pathname, File or Tempfile and the meta hash
- returns nil instead if not found
destroy
- takes a String uid
- destroys the content
You can also optionally serve data directly from the datastore using
or
provided the data store implements url_for
Both remote_url_for
and remote_url
also take an options hash which will be passed through to the data store’s url_for
method.
Using your custom data store
Your custom data store can be used by a Dragonfly app with
or you can register a symbol (which you may want to do if creating a gem)
so you configure using just the symbol
Note that the data store class is registered with the symbol, not the instance. Any other args are passed straight to the data store’s initialize
method.
Testing with RSpec
Dragonfly provides a shared rspec example group that you can use to test that your custom data store conforms to the basic spec. Here’s a simple example spec file
Derived from theme by orderedlist