localshred/protobuf — pull request #33 by localshred (2012-11-03 17:33:37+00:00)
Title: [WIP] Add callback (filter) support for service classes

This pull adds a callback-style filter support to service classes similar to ActionController filters, specifically adding `before_filter`, `after_filter`, and `around_filter`.

```ruby
class Foo::UserService < ::Protobuf::Rpc::Service

before_filter :start_request_timer
after_filter :end_request_timer
around_filter :benchmark_request # don't do this

def find
#... snip
end

def start_request_timer
@time_start = Time.now
end

def end_request_timer
@time_end = Time.now
log_info { ... }
end

def benchmark_request
Benchmark.benchmark do
yield
end
end
end
```

### Stopping execution of rpc request inside a filter

*Around Filters* - Inside of an around filter, if you wish to halt request processing and return, simply do not yield the block. It would make sense in this case to usually provide an `rpc_failed` message, but not always.

*Before Filters* - I haven't yet decided the best approach, open to comments (of course). I would like to steer away from `raise` or `throw` if at all possible.

*After Filters* - There is no request shortcutting since the after filter runs after the request. Duh.