Archives
Tags
- General (18)
- Food (1)
- Cooking (1)
- Ruby (6)
- Rails (2)
- Svn (2)
- Linux (9)
- Git (1)
- Firefox (8)
- Porn (1)
- Freyja (1)
- Witchhammer (6)
- Music (1)
- Merb (3)
- Poetry (0)
- Bolverk (3)
- Sinatra (1)
- Discogs (1)
- Centos (1)
- Python (1)
- Whinging (2)
- Travel (2)
- Scheme (7)
- Lisp (8)
- Sicp (1)
- Rot13 (1)
- Czech (2)
- Metal (3)
- Passenger (1)
- Fun (5)
- Fractals (2)
- Plt (2)
- Clojure (1)
- Continuations (1)
- Javascript (1)
- Presentation (1)
Multiple before/after model filters with Merb
This evening I found myself in a position in which I wanted to execute several "before" methods hooks/filters on a DataMapper model in a Merb application. I had a look around in the source and API docs, but couldn't really find a nice way of doing it (even though having every hook on a seperate line is not such a bad thing in terms of readability, I suppose).
Anyway, I came up with the following mixin for Extlib (which houses DataMapper's before/after hook functionality):
module Extlib
module Hook
module ClassMethods
alias_method :singular_before, :before
alias_method :singular_after, :after
def before(target_methods, filter_methods = nil, &block)
insert_multiple_hooks "before", target_methods, filter_methods, &block
end
def after(target_methods, filter_methods = nil, &block)
insert_multiple_hooks "after", target_methods, filter_methods, &block
end
private
def insert_multiple_hooks(context, target_methods, filter_methods, &block)
targets = [target_methods].flatten
filters = [filter_methods].flatten
targets.each do |target|
filters.each do |filter|
send("singular_#{context}", target.to_sym, filter.to_sym, &block)
end
end
end
end
end
end Hopefully it is fairly straight-forward. Basically, it will allow you to pass an array to the before and after methods. For example, you can do this:
before [ :create, :update ], [ :set_filename, :set_filesize, :generate_thumbnail ]
Which is equivalent to:
before :create, :set_filename before :create, :set_filesize before :create, :generate_thumbnail before :update, :set_filename before :update, :set_filesize before :update, :generate_thumbnail
I got it loaded into my Merb app by creating an app/lib directory and requireing the contents of it from the config/init.rb file.