Skip to content
This repository
Browse code

Move config extension modules into main config object.

This allows all config options to be set in the default_configuration block.
  • Loading branch information...
commit a8d55bcf7f1787086eb8d033f21270a9a9bc7c35 1 parent 88ac760
Myron Marston authored April 04, 2012
35  lib/interpol/configuration.rb
@@ -28,6 +28,7 @@ def extract_search_options_from(options)
28 28
   # Public: Defines interpol configuration.
29 29
   class Configuration
30 30
     attr_reader :endpoint_definition_files, :endpoints
  31
+    attr_accessor :validation_mode
31 32
 
32 33
     def initialize
33 34
       api_version do
@@ -57,15 +58,39 @@ def api_version_for(rack_env_hash)
57 58
       @api_version_block.call(rack_env_hash).to_s
58 59
     end
59 60
 
  61
+    def validate_if(&block)
  62
+      @validate_if_block = block
  63
+    end
  64
+
  65
+    def validate?(*args)
  66
+      @validate_if_block ||= lambda do |env, status, headers, body|
  67
+        (200..299).cover?(status) && status != 204 # No Content
  68
+      end
  69
+      @validate_if_block.call(*args)
  70
+    end
  71
+
  72
+    def on_invalid_request_version(&block)
  73
+      @invalid_request_version_block = block
  74
+    end
  75
+
  76
+    def request_version_invalid(execution_context, *args)
  77
+      @invalid_request_version_block ||= lambda do |requested, available|
  78
+        message = "The requested API version is invalid. " +
  79
+                  "Requested: #{requested}. " +
  80
+                  "Available: #{available}"
  81
+        halt 406, JSON.dump(error: message)
  82
+      end
  83
+
  84
+      execution_context.instance_exec(*args, &@invalid_request_version_block)
  85
+    end
  86
+
60 87
     def self.default
61 88
       @default ||= Configuration.new
62 89
     end
63 90
 
64  
-    def customized_duplicate(*extensions)
65  
-      dup.tap do |instance|
66  
-        extensions.each { |e| instance.extend(e) }
67  
-        yield instance if block_given?
68  
-      end
  91
+    def customized_duplicate(&block)
  92
+      block ||= lambda { |c| }
  93
+      dup.tap(&block)
69 94
     end
70 95
   end
71 96
 end
17  lib/interpol/response_schema_validator.rb
@@ -7,23 +7,8 @@ module Interpol
7 7
   # error or warn in this condition. Intended for development and
8 8
   # test use.
9 9
   class ResponseSchemaValidator
10  
-    module ConfigurationExtras
11  
-      attr_accessor :validation_mode
12  
-
13  
-      def validate_if(&block)
14  
-        @validate_if_block = block
15  
-      end
16  
-
17  
-      def validate?(*args)
18  
-        @validate_if_block ||= lambda do |env, status, headers, body|
19  
-          (200..299).cover?(status) && status != 204 # No Content
20  
-        end
21  
-        @validate_if_block.call(*args)
22  
-      end
23  
-    end
24  
-
25 10
     def initialize(app, &block)
26  
-      @config = Configuration.default.customized_duplicate(ConfigurationExtras, &block)
  11
+      @config = Configuration.default.customized_duplicate(&block)
27 12
       @app = app
28 13
       @handler_class = @config.validation_mode == :warn ? HandlerWithWarnings : Handler
29 14
     end
19  lib/interpol/stub_app.rb
@@ -6,25 +6,8 @@ module Interpol
6 6
   module StubApp
7 7
     extend self
8 8
 
9  
-    module ConfigurationExtras
10  
-      def on_invalid_request_version(&block)
11  
-        @invalid_request_version_block = block
12  
-      end
13  
-
14  
-      def request_version_invalid(execution_context, *args)
15  
-        @invalid_request_version_block ||= lambda do |requested, available|
16  
-          message = "The requested API version is invalid. " +
17  
-                    "Requested: #{requested}. " +
18  
-                    "Available: #{available}"
19  
-          halt 406, JSON.dump(error: message)
20  
-        end
21  
-
22  
-        execution_context.instance_exec(*args, &@invalid_request_version_block)
23  
-      end
24  
-    end
25  
-
26 9
     def build(&block)
27  
-      config = Configuration.default.customized_duplicate(ConfigurationExtras, &block)
  10
+      config = Configuration.default.customized_duplicate(&block)
28 11
       builder = Builder.new(config)
29 12
       builder.build
30 13
       builder.app
22  spec/unit/interpol/configuration_spec.rb
@@ -172,28 +172,22 @@ def find(*args)
172 172
     end
173 173
 
174 174
     describe "#customized_duplicate" do
175  
-      let(:mod1) { Module.new { attr_accessor :mod1 } }
176  
-      let(:mod2) { Module.new { attr_accessor :mod2 } }
177  
-
178  
-      it "applies the given extension modules" do
179  
-        cd = config.customized_duplicate(mod1, mod2)
180  
-        cd.should respond_to(:mod1, :mod2)
181  
-      end
182  
-
183 175
       it 'yields a configuration instance' do
184 176
         cd = nil
185  
-        config.customized_duplicate(mod1, mod2) { |c| cd = c }
  177
+        config.customized_duplicate { |c| cd = c }
186 178
         cd.should be_a(Configuration)
187  
-        cd.should respond_to(:mod1, :mod2)
188 179
       end
189 180
 
190 181
       it 'uses a duplicate so as not to affect the original instance' do
191  
-        config.customized_duplicate(mod1, mod2) do |c|
192  
-          c.mod1 = :foo
193  
-          c.mod2 = :bar
  182
+        config.validation_mode = :warn
  183
+        cd = nil
  184
+        config.customized_duplicate do |c|
  185
+          c.validation_mode = :error
  186
+          cd = c
194 187
         end
195 188
 
196  
-        config.should_not respond_to(:mod1, :mod2)
  189
+        config.validation_mode.should be(:warn)
  190
+        cd.validation_mode.should be(:error)
197 191
       end
198 192
     end
199 193
   end

0 notes on commit a8d55bc

Please sign in to comment.
Something went wrong with that request. Please try again.