Permalink
Browse files

Move config extension modules into main config object.

This allows all config options to be set in the default_configuration block.
  • Loading branch information...
1 parent 88ac760 commit a8d55bcf7f1787086eb8d033f21270a9a9bc7c35 @myronmarston myronmarston committed Apr 4, 2012
View
35 lib/interpol/configuration.rb
@@ -28,6 +28,7 @@ def extract_search_options_from(options)
# Public: Defines interpol configuration.
class Configuration
attr_reader :endpoint_definition_files, :endpoints
+ attr_accessor :validation_mode
def initialize
api_version do
@@ -57,15 +58,39 @@ def api_version_for(rack_env_hash)
@api_version_block.call(rack_env_hash).to_s
end
+ def validate_if(&block)
+ @validate_if_block = block
+ end
+
+ def validate?(*args)
+ @validate_if_block ||= lambda do |env, status, headers, body|
+ (200..299).cover?(status) && status != 204 # No Content
+ end
+ @validate_if_block.call(*args)
+ end
+
+ def on_invalid_request_version(&block)
+ @invalid_request_version_block = block
+ end
+
+ def request_version_invalid(execution_context, *args)
+ @invalid_request_version_block ||= lambda do |requested, available|
+ message = "The requested API version is invalid. " +
+ "Requested: #{requested}. " +
+ "Available: #{available}"
+ halt 406, JSON.dump(error: message)
+ end
+
+ execution_context.instance_exec(*args, &@invalid_request_version_block)
+ end
+
def self.default
@default ||= Configuration.new
end
- def customized_duplicate(*extensions)
- dup.tap do |instance|
- extensions.each { |e| instance.extend(e) }
- yield instance if block_given?
- end
+ def customized_duplicate(&block)
+ block ||= lambda { |c| }
+ dup.tap(&block)
end
end
end
View
17 lib/interpol/response_schema_validator.rb
@@ -7,23 +7,8 @@ module Interpol
# error or warn in this condition. Intended for development and
# test use.
class ResponseSchemaValidator
- module ConfigurationExtras
- attr_accessor :validation_mode
-
- def validate_if(&block)
- @validate_if_block = block
- end
-
- def validate?(*args)
- @validate_if_block ||= lambda do |env, status, headers, body|
- (200..299).cover?(status) && status != 204 # No Content
- end
- @validate_if_block.call(*args)
- end
- end
-
def initialize(app, &block)
- @config = Configuration.default.customized_duplicate(ConfigurationExtras, &block)
+ @config = Configuration.default.customized_duplicate(&block)
@app = app
@handler_class = @config.validation_mode == :warn ? HandlerWithWarnings : Handler
end
View
19 lib/interpol/stub_app.rb
@@ -6,25 +6,8 @@ module Interpol
module StubApp
extend self
- module ConfigurationExtras
- def on_invalid_request_version(&block)
- @invalid_request_version_block = block
- end
-
- def request_version_invalid(execution_context, *args)
- @invalid_request_version_block ||= lambda do |requested, available|
- message = "The requested API version is invalid. " +
- "Requested: #{requested}. " +
- "Available: #{available}"
- halt 406, JSON.dump(error: message)
- end
-
- execution_context.instance_exec(*args, &@invalid_request_version_block)
- end
- end
-
def build(&block)
- config = Configuration.default.customized_duplicate(ConfigurationExtras, &block)
+ config = Configuration.default.customized_duplicate(&block)
builder = Builder.new(config)
builder.build
builder.app
View
22 spec/unit/interpol/configuration_spec.rb
@@ -172,28 +172,22 @@ def find(*args)
end
describe "#customized_duplicate" do
- let(:mod1) { Module.new { attr_accessor :mod1 } }
- let(:mod2) { Module.new { attr_accessor :mod2 } }
-
- it "applies the given extension modules" do
- cd = config.customized_duplicate(mod1, mod2)
- cd.should respond_to(:mod1, :mod2)
- end
-
it 'yields a configuration instance' do
cd = nil
- config.customized_duplicate(mod1, mod2) { |c| cd = c }
+ config.customized_duplicate { |c| cd = c }
cd.should be_a(Configuration)
- cd.should respond_to(:mod1, :mod2)
end
it 'uses a duplicate so as not to affect the original instance' do
- config.customized_duplicate(mod1, mod2) do |c|
- c.mod1 = :foo
- c.mod2 = :bar
+ config.validation_mode = :warn
+ cd = nil
+ config.customized_duplicate do |c|
+ c.validation_mode = :error
+ cd = c
end
- config.should_not respond_to(:mod1, :mod2)
+ config.validation_mode.should be(:warn)
+ cd.validation_mode.should be(:error)
end
end
end

0 comments on commit a8d55bc

Please sign in to comment.