diff options
author | Gibheer <gibheer@gmail.com> | 2012-12-13 08:45:03 +0100 |
---|---|---|
committer | Gibheer <gibheer@gmail.com> | 2012-12-13 08:45:03 +0100 |
commit | 7bda0ec7e5f6ead0af610f0393c4e039fad9dbf1 (patch) | |
tree | 18741bc721d87b62906dc38e2effc801f1969518 | |
parent | 38bde320ac7ba70b5d3a0320f70080062a8790c0 (diff) |
added options to controller
This avoids hardcoding the options into the controller and therefore
should it make possible to use Rack::Request or Sinatra apps as
requests. (That sounds so weird.)
-rw-r--r-- | lib/zero/controller.rb | 21 | ||||
-rw-r--r-- | spec/unit/controller/renderer_spec.rb | 4 |
2 files changed, 18 insertions, 7 deletions
diff --git a/lib/zero/controller.rb b/lib/zero/controller.rb index 17d9b3f..febb23e 100644 --- a/lib/zero/controller.rb +++ b/lib/zero/controller.rb @@ -7,7 +7,7 @@ module Zero class Controller # initialize a new instance of the controller and call response on it def self.call(env) - new(Zero::Request.new(env)).response + new(env).response end # set the class to use for responses @@ -20,6 +20,16 @@ module Zero @@response ||= Zero::Response end + # set a class to use for requests + def self.request=(request_class) + @@request = request_class + end + + # return the set request class + def self.request + @@request ||= Zero::Request + end + # set the renderer to use in the controller def self.renderer=(renderer) @@renderer = renderer @@ -35,10 +45,11 @@ module Zero # initialize the controller # - # At initialization `@request`, `@response` and `@renderer` are set. - # @param request [Request] a request object - def initialize(request) - @request = request + # This creates a new controller instance using the defined classes of + # renderer, request and response. + # @param env [Hash] a rack compatible environment + def initialize(env) + @request = self.class.request.new(env) @response = self.class.response @renderer = self.class.renderer end diff --git a/spec/unit/controller/renderer_spec.rb b/spec/unit/controller/renderer_spec.rb index c840f23..21664ec 100644 --- a/spec/unit/controller/renderer_spec.rb +++ b/spec/unit/controller/renderer_spec.rb @@ -3,9 +3,9 @@ require 'spec_helper' describe Zero::Controller, '#renderer' do subject { Zero::Controller } let(:renderer) { Object.new } - + it 'returns the set renderer' do subject.renderer = renderer - expect(subject.new(Object.new).renderer).to be(renderer) + expect(subject.new({}).renderer).to be(renderer) end end |