No commit activity in last 3 years
No release in over 3 years
Write centralized model observers in a simple DSL
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.0
~> 1.5.0.pre3
>= 0
~> 1.2.8
>= 2.0.0.beta.19
~> 0.6.0
 Project Readme

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
   default_config(:method => :purge)

   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>)`

Note that you can easily provide default options for your plugin by calling default_config in your plugin file:

 default_config(:method => :purge)

which comes in handy if some config options of your plugin do not need to be set by the users.

Configuration Validator

If your want to make sure that all configuration values of your plugin are valid, you can define a method called validate_configuration that does the necessary checks. It gets called after calling default_config and config= and should get called by methods that take in options that can overwrite the configuration. It should take care of the situation. If validate_configuration validates it should return the @config variable as its result gets returned when calling config=.

 class VarnishSweeper < SurveillanceAuthority::Sanctions
   default_config(:method => :purge)
    
   def validate_configuration
     # make sure the method option is either set to :purge or :header_invalidation
     raise "invalid config" unless [:purge, :header_invalidation].include?( @config[:method] )
     @config
   end

   def sweep(url, options = {})
     options = validate_configuration( self.config.merge(options) )
     ...
   end
 end

Copyright

Copyright (c) 2010 Daniel Bornkessel. See LICENSE for details.