mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Made it so that multiple class definitions don't overrun each others options by using class instance variable instead of class variable. [#11 state:resolved]
This commit is contained in:
parent
a517168278
commit
c531652d81
2 changed files with 48 additions and 5 deletions
|
@ -10,6 +10,32 @@ $:.unshift(directory) unless $:.include?(directory) || $:.include?(File.expand_p
|
|||
|
||||
require 'httparty/request'
|
||||
|
||||
module ModuleLevelInheritableAttributes
|
||||
def self.included(base)
|
||||
base.extend(ClassMethods)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def mattr_inheritable(*args)
|
||||
@mattr_inheritable_attrs ||= [:mattr_inheritable_attrs]
|
||||
@mattr_inheritable_attrs += args
|
||||
args.each do |arg|
|
||||
module_eval %(
|
||||
class << self; attr_accessor :#{arg} end
|
||||
)
|
||||
end
|
||||
@mattr_inheritable_attrs
|
||||
end
|
||||
|
||||
def inherited(subclass)
|
||||
@mattr_inheritable_attrs.each do |inheritable_attribute|
|
||||
instance_var = "@#{inheritable_attribute}"
|
||||
subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module HTTParty
|
||||
class UnsupportedFormat < StandardError; end
|
||||
class RedirectionTooDeep < StandardError; end
|
||||
|
@ -18,11 +44,14 @@ module HTTParty
|
|||
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.send :include, ModuleLevelInheritableAttributes
|
||||
base.send(:mattr_inheritable, :default_options)
|
||||
base.instance_variable_set("@default_options", {})
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def default_options
|
||||
@@default_options ||= {}
|
||||
@default_options
|
||||
end
|
||||
|
||||
#
|
||||
|
|
|
@ -5,9 +5,16 @@ class Foo
|
|||
base_uri 'api.foo.com/v1'
|
||||
end
|
||||
|
||||
class FooWithHttps
|
||||
class GRest
|
||||
include HTTParty
|
||||
base_uri 'api.foo.com/v1:443'
|
||||
base_uri "grest.com"
|
||||
default_params :one => 'two'
|
||||
end
|
||||
|
||||
class HRest
|
||||
include HTTParty
|
||||
base_uri "hrest.com"
|
||||
default_params :two => 'three'
|
||||
end
|
||||
|
||||
describe HTTParty do
|
||||
|
@ -117,4 +124,11 @@ describe HTTParty do
|
|||
end.should raise_error(HTTParty::RedirectionTooDeep)
|
||||
end
|
||||
end
|
||||
|
||||
describe "with multiple class definitions" do
|
||||
it "should not run over each others options" do
|
||||
HRest.default_options.should == {:base_uri => 'http://hrest.com', :default_params => {:two => 'three'}}
|
||||
GRest.default_options.should == {:base_uri => 'http://grest.com', :default_params => {:one => 'two'}}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue