diff --git a/lib/httparty.rb b/lib/httparty.rb index b8b8cea..6a9b0e4 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -10,19 +10,48 @@ $:.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 - + AllowedFormats = {:xml => 'text/xml', :json => 'application/json', :html => 'text/html'} 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 + module ClassMethods def default_options - @@default_options ||= {} + @default_options end # diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index e5f27ef..3ad556e 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -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