Permalink
Browse files

Issue #21: Change configuration to be tolerant of route reloading

  • Loading branch information...
1 parent 7ecfb90 commit 4747f387d0ca0a71badaee46f5059183b1917546 Brian Ploetz committed Jul 26, 2012
@@ -16,6 +16,7 @@ def clear!
@versioning_strategies.clear
@default_version = nil
@header_versions.clear
+ @parameter_versions.clear
end
end
end
View
@@ -33,10 +33,10 @@ def configure_header(config, &block)
end
def configure_path(config, &block)
+ config[:path].slice!(0) if config[:path] =~ /^\//
path = Versionist::VersioningStrategy::Path.new(config)
# Use the :as option and strip out non-word characters from the path to avoid this:
# https://github.com/rails/rails/issues/3224
- config[:path].slice!(0) if config[:path] =~ /^\//
route_hash = {:module => config[:module], :as => config[:path].gsub(/\W/, '_')}
route_hash.merge!({:defaults => config[:defaults]}) if config.has_key?(:defaults)
namespace(config[:path], route_hash, &block)
@@ -53,14 +53,3 @@ def configure_parameter(config, &block)
end
end
end
-
-# Hook to clear versionist cached data when routes are reloaded
-module Rails
- class Application #:nodoc:
- def reload_routes_with_versionist!
- Versionist.configuration.clear!
- reload_routes_without_versionist!
- end
- alias_method_chain :reload_routes!, :versionist
- end
-end
@@ -10,17 +10,26 @@ def initialize(config={})
raise ArgumentError, "you must pass a configuration Hash" if config.nil? || !config.is_a?(Hash)
@config = config
@config.symbolize_keys!
- Versionist.configuration.versioning_strategies << self
- raise ArgumentError, "[VERSIONIST] attempt to set more than one default api version" if !Versionist.configuration.default_version.nil? && @config.has_key?(:default)
if @config.has_key?(:default)
- Versionist.configuration.default_version = self
@default = true
+ else
+ @default = false
+ end
+ if !Versionist.configuration.versioning_strategies.include?(self)
+ raise ArgumentError, "[VERSIONIST] attempt to set more than one default api version" if !Versionist.configuration.default_version.nil? && self.default? && Versionist.configuration.default_version != self
+ Versionist.configuration.versioning_strategies << self
+ Versionist.configuration.default_version = self if self.default?
end
end
def default?
@default
end
+
+ def ==(other)
+ return false if other.nil? || !other.is_a?(Versionist::VersioningStrategy::Base)
+ return self.config == other.config && self.default? == other.default?
+ end
end
end
end
@@ -7,9 +7,9 @@ class Header < Base
# - :header the header to inspect
# - :value the value of the header specifying the version
def initialize(config)
+ super
raise ArgumentError, "you must specify :header in the configuration Hash" if !config.has_key?(:header)
raise ArgumentError, "you must specify :value in the configuration Hash" if !config.has_key?(:value)
- super
Versionist.configuration.header_versions << config[:value]
end
@@ -18,6 +18,12 @@ def matches?(request)
return ((!header_string.blank? && header_string.include?(config[:value])) ||
(self.default? && (Versionist.configuration.header_versions.none? {|v| header_string.include?(v)})))
end
+
+ def ==(other)
+ super
+ return false if !other.is_a?(Versionist::VersioningStrategy::Header)
+ return config[:header] == other.config[:header] && self.config[:value] == other.config[:value]
+ end
end
end
end
@@ -7,9 +7,9 @@ class Parameter < Base
# - :parameter the parameter to inspect
# - :value the value of the parameter specifying the version
def initialize(config)
+ super
raise ArgumentError, "you must specify :parameter in the configuration Hash" if !config.has_key?(:parameter)
raise ArgumentError, "you must specify :value in the configuration Hash" if !config.has_key?(:value)
- super
Versionist.configuration.parameter_versions << config[:value]
end
@@ -18,6 +18,12 @@ def matches?(request)
return ((!parameter_string.blank? && parameter_string == config[:value]) ||
(self.default? && (Versionist.configuration.parameter_versions.none? {|v| parameter_string.include?(v)})))
end
+
+ def ==(other)
+ super
+ return false if !other.is_a?(Versionist::VersioningStrategy::Parameter)
+ return config[:parameter] == other.config[:parameter] && self.config[:value] == other.config[:value]
+ end
end
end
end
@@ -7,8 +7,14 @@ class Path < Base
# Creates a new Path VersioningStrategy object. config must contain the following keys:
# - :path the path prefix containing the version
def initialize(config)
+ super
raise ArgumentError, "you must specify :path in the configuration Hash" if !config.has_key?(:path)
+ end
+
+ def ==(other)
super
+ return false if !other.is_a?(Versionist::VersioningStrategy::Path)
+ return config[:path] == other.config[:path]
end
end
end
View
@@ -637,9 +637,9 @@
end
context "route reloading" do
- it "should clear cached data when calling Rails.application.reload_routes!" do
+ it "should handle Rails.application.reload_routes!" do
lambda {
- Versionist.configuration.should_receive(:clear!)
+ Rails.application.reload_routes!
Rails.application.reload_routes!
}.should_not raise_error
end
@@ -17,13 +17,35 @@
}.should raise_error(ArgumentError, /you must pass a configuration Hash/)
end
+ it "should set attributes" do
+ @base = Versionist::VersioningStrategy::Base.new({})
+ @base.config.should == {}
+ @base.default?.should == false
+
+ @base2 = Versionist::VersioningStrategy::Base.new({"default" => true})
+ @base2.default?.should == true
+ # symbolize_keys! should be called
+ @base2.config.should_not == {"default" => true}
+ @base2.config.should == {:default => true}
+ end
+
it "should add self to Versionist::Configuration.versioning_strategies" do
Versionist.configuration.versioning_strategies.should be_empty
Versionist::VersioningStrategy::Base.new({})
Versionist.configuration.versioning_strategies.should_not be_empty
Versionist.configuration.versioning_strategies.size.should == 1
end
+ it "should not add self to Versionist::Configuration.versioning_strategies more than once" do
+ Versionist.configuration.versioning_strategies.should be_empty
+ Versionist::VersioningStrategy::Base.new({})
+ Versionist.configuration.versioning_strategies.should_not be_empty
+ Versionist.configuration.versioning_strategies.size.should == 1
+ Versionist::VersioningStrategy::Base.new({})
+ Versionist.configuration.versioning_strategies.should_not be_empty
+ Versionist.configuration.versioning_strategies.size.should == 1
+ end
+
it "should set self as Versionist::Configuration.default_version if config contains :default" do
Versionist.configuration.default_version.should be_nil
Versionist::VersioningStrategy::Base.new({:default => true, :path => "foo"})
@@ -38,4 +60,35 @@
Versionist::VersioningStrategy::Base.new({:default => true, :path => "bar"})
}.should raise_error(ArgumentError, /attempt to set more than one default api version/)
end
+
+ context "==" do
+ before :each do
+ @base = Versionist::VersioningStrategy::Base.new({:default => false})
+ @equal_base = Versionist::VersioningStrategy::Base.new({:default => false})
+ end
+
+ it "should return true if passed an equal object" do
+ (@base == @equal_base).should == true
+ end
+
+ it "should return false if passed nil" do
+ (@base == nil).should == false
+ end
+
+ it "should return false if passed an object that's not a Versionist::VersioningStrategy::Base" do
+ (@base == Array.new).should == false
+ end
+
+ it "should return false if passed an object that's not equal" do
+ @unequal_base = Versionist::VersioningStrategy::Base.new({})
+ (@base == @unequal_base).should == false
+ end
+
+ it "should find equal versioning strategies via Array.include?" do
+ @array = Array.new
+ @array << @base
+ @array.include?(@base).should == true
+ @array.include?(@equal_base).should == true
+ end
+ end
end
@@ -26,4 +26,31 @@
Versionist::VersioningStrategy::Header.new({:header => "foo", :value => "v3"})
Versionist.configuration.header_versions.include?("v3").should be_true
end
+
+ context "==" do
+ before :each do
+ @header = Versionist::VersioningStrategy::Header.new({:header => "Accept", :value => "application/vnd.mycompany.com; version=1"})
+ @equal_header = Versionist::VersioningStrategy::Header.new({:header => "Accept", :value => "application/vnd.mycompany.com; version=1"})
+ end
+
+ it "should return true if passed an equal object" do
+ (@header == @equal_header).should == true
+ end
+
+ it "should return false if passed an object that's not a Versionist::VersioningStrategy::Header" do
+ (@header == Array.new).should == false
+ end
+
+ it "should return false if passed an object that's not equal" do
+ @unequal_header = Versionist::VersioningStrategy::Header.new({:header => "Accept", :value => "application/vnd.mycompany.com; version=2"})
+ (@header == @unequal_header).should == false
+ end
+
+ it "should find equal versioning strategies via Array.include?" do
+ @array = Array.new
+ @array << @header
+ @array.include?(@header).should == true
+ @array.include?(@equal_header).should == true
+ end
+ end
end
@@ -12,4 +12,31 @@
Versionist::VersioningStrategy::Parameter.new({:parameter => "version"})
}.should raise_error(ArgumentError, /you must specify :value in the configuration Hash/)
end
+
+ context "==" do
+ before :each do
+ @parameter = Versionist::VersioningStrategy::Parameter.new({:parameter => "version", :value => "1"})
+ @equal_parameter = Versionist::VersioningStrategy::Parameter.new({:parameter => "version", :value => "1"})
+ end
+
+ it "should return true if passed an equal object" do
+ (@parameter == @equal_parameter).should == true
+ end
+
+ it "should return false if passed an object that's not a Versionist::VersioningStrategy::Parameter" do
+ (@parameter == Array.new).should == false
+ end
+
+ it "should return false if passed an object that's not equal" do
+ @unequal_parameter = Versionist::VersioningStrategy::Parameter.new({:parameter => "ver", :value => "1"})
+ (@parameter == @unequal_parameter).should == false
+ end
+
+ it "should find equal versioning strategies via Array.include?" do
+ @array = Array.new
+ @array << @parameter
+ @array.include?(@parameter).should == true
+ @array.include?(@equal_parameter).should == true
+ end
+ end
end
@@ -6,4 +6,31 @@
Versionist::VersioningStrategy::Path.new({})
}.should raise_error(ArgumentError, /you must specify :path in the configuration Hash/)
end
+
+ context "==" do
+ before :each do
+ @path = Versionist::VersioningStrategy::Path.new({:path => "/v1"})
+ @equal_path = Versionist::VersioningStrategy::Path.new({:path => "/v1"})
+ end
+
+ it "should return true if passed an equal object" do
+ (@path == @equal_path).should == true
+ end
+
+ it "should return false if passed an object that's not a Versionist::VersioningStrategy::Path" do
+ (@path == Array.new).should == false
+ end
+
+ it "should return false if passed an object that's not equal" do
+ @unequal_path = Versionist::VersioningStrategy::Path.new({:path => "v2"})
+ (@path == @unequal_path).should == false
+ end
+
+ it "should find equal versioning strategies via Array.include?" do
+ @array = Array.new
+ @array << @path
+ @array.include?(@path).should == true
+ @array.include?(@equal_path).should == true
+ end
+ end
end

0 comments on commit 4747f38

Please sign in to comment.