Skip to content

Conversation

jodosha
Copy link
Member

@jodosha jodosha commented May 27, 2014

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.

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.

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.

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.

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 # => {}

@coveralls
Copy link

Coverage Status

Coverage increased (+0.09%) when pulling 431970c on configuration into 8d39557 on master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.09%) when pulling 075f9c3 on configuration into 8d39557 on master.

jodosha added a commit that referenced this pull request May 28, 2014
Framework, controller and action configuration
@jodosha jodosha merged commit 6e90caf into master May 28, 2014
@jodosha jodosha deleted the configuration branch May 28, 2014 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants