javan/whenever — pull request #334 by AlphaHydrae (2013-04-15 09:09:23+00:00)
Title: Support giving the schedule as a block for instance eval

I'm looking to use whenever as a dependency in one of my gems to schedule periodic jobs. However, `Whenever::CommandLine` only supports loading the configuration from a file (or string as used for tests), which is not practical when used programmatically.

Since `instance_eval` can also take a block, I made a few small modifications to add the `:block` option. That way you can configure a cron job in code directly without going through a file. For example:

```ruby
block = Proc.new do
every 2.hours do
command 'my command'
end
end

Whenever::CommandLine :block => block, :identifier => 'My ID'
```

I added a check so that you can either give a block or a file, not both. Also note that since the identifier was automatically deduced from the file name, which is unnecessary with a block, I added a check to require an identifier when using a block.

I added tests for these new checks and copied a few tests for block configurations. I ran the test suite successfully for 1.8.7 and 1.9.3 (and 2.0.0 as a bonus).

Following my modifications, I improved three tests in `test/functional/command_line_test.rb`. These tests were checking invocation of `:exit` in the `initialize` method when invalid arguments are given. This stubs normal exit functionality and thus causes the execution of `initialize` to continue despite the error, which caused weird behavior in my case (I was getting multiple calls to `warn` instead of one). I replaced that with an `assert_raise SystemExit` which seems better.

I hope you will find these changes useful and integrate them. If so, I would gladly add a wiki page to describe how to use whenever programmatically with this new option.