surveillance_authority
Introduction
This gem provides a dsl to easily write observers in one or more centralized files.
Installation
Install surveillance_authority by adding
gem 'surveillance_authority'
to your Gemfile or install it using
gem install surveillance_authority
Integration into your project
In
config/initializers
create one or more ruby files in which you can define your surveillance rules. For example:
SurveillanceAuthority.observe do
# Do something after a new movie was created
after "Movie#create" do |movie|
# ... do stuff
end
# Do something before a User gets updated
before "User#update" do |user|
# ... do stuff
end
end
Uhm ...so what?
surveillance_authority is meant to be used together with some plugins. One of those is varnish_sweeper, which invalidates certain routes.
Writing plugins for surveillance_authority
Making methods available to surveillance_authority
In order to write a plugin for surveillance_authority, simply create a class that inherits from
SurveillanceAuthority::Sanctions
all public methods of that class will be available in the blocks. E.g.
class VarnishSweeper < SurveillanceAuthority::Sanctions
def sweep(url, options = {})
options.reverse_merge(
:method => :invalidate
}
options[:method] == :purge ? purge_url(url) : invalidate(url)
end
private
def purge_url(url)
...
end
def invalidate(url)
...
end
end
will make the sweep method available:
SurveillanceAuthority.observe do
# Do something after a new movie was updated
after "Movie#update" do |movie|
sweep movie_path(movie), :method => :invalidate
end
end
Configuration for plugins
Configuring plugins should be made with the central config method provided by surveillance_authority. If we again use a plugin called VarnishSweeper
, configuring this plugin should happen like this:
SurveillanceAuthority.config(VarnishSweeper, <hash with options>)
Withing your plugin, you will be able to access your options simply by calling config
.
Example: VarnishSweep
needs to be configured with a base url to varnish
class VarnishSweeper < SurveillanceAuthority::Sanctions
def sweep(url, options = {})
options.reverse_merge(
:method => :invalidate
}
options[:method] == :purge ? purge_url(url) : invalidate(url)
end
private
def varnish_base_url
config[:base_url]
end
def purge_url(url)
...
end
def invalidate(url)
...
end
end
In the project using this plugin:
SurveillanceAuthority.config(VarnishSweeper, :base_url => "http://varnish.example.com")
...
If you want to access the config of other plugins, use:
`SurveillanceAuthority.config_for(<plugin>)`
Copyright
Copyright (c) 2010 Daniel Bornkessel. See LICENSE for details.