lotus/controller — pull request #11 by jodosha (2014-05-27 12:08:35+00:00)
Title: Framework, controller and action configuration

Until now, in order to configure the framework, we had direct accessors like `Lotus::Controller. handle_exceptions=`. This design had two main drawbacks:

1. In order to add new settings, we had to expand the public API of `Lotus::Controller`.
2. This configuration was unique per Ruby process, and made impossible to reuse the framework, or do fine tuning at the level of a single action.

This new design, adds a class level accessor `.configuration` and a `.configure` DSL for `Lotus::Controller`. Those two methods return an instance of `Lotus::Controller::Configuration`.

```ruby
Lotus::Controller.configure do
handle_exceptions false
end

Lotus::Controller.configuration.handle_exceptions # => false
```

Every time that `Lotus::Controller` gets included in a class, the configuration is copied into the target. **The two copies are independent each other**.

```ruby
class ArticlesController
include Lotus::Controller
self.configuration.handle_exceptions = false
end

ArticlesController.configuration.handle_exceptions # => false
Lotus::Controller.configuration.handle_exceptions # => true (default)
```

Every time that an action is generated with the `.action` DSL, the controller's configuration gets copied into the target. Again, the two copies are independent each other.

```ruby
class ArticlesController
include Lotus::Controller

action 'Index' do
def call(params)
# ...
end
end

action 'Show' do
handle_exception RecordNotFound => 404

def call(params)
# ...
end
end
end

ArticlesController.configuration.handled_exceptions # => {}
ArticlesController.Index::configuration.handled_exceptions # => {}
ArticlesController.Show::configuration.handled_exceptions # => { RecordNotFound => 404 }
```

This change is also important for Lotus, because it allows to have a micro-services architecture.

```ruby
module MusicPlayer
Controller = Lotus::Controller.duplicate
Controller.configure do
handle_exception ArtistNotFound => 404
end
end

module JsonApi
Controller = Lotus::Controller.duplicate
end

class MusicPlayer::ArtistsController
include MusicPlayer::Controller
end

class JsonApi::ArtistsController
include MusicPlayer::Controller
end

MusicPlayer::ArtistsController.configuration.handled_exceptions # => {ArtistNotFound => 404}
JsonApi::ArtistsController.configuration.handled_exceptions # => {}
```