Fix a bug where class extentions were conflicting with ActiveSupport

This commit is contained in:
Matthew Ford 2009-03-05 15:47:53 +00:00 committed by Matt Aimonetti
parent d95b9de50d
commit fdb0854859
2 changed files with 215 additions and 140 deletions

View file

@ -25,7 +25,19 @@
# example, an array without those additions being shared with either their
# parent, siblings, or children, which is unlike the regular class-level
# attributes that are shared across the entire hierarchy.
class Class
module CouchRest
module ClassExtension
def self.included(base)
if CouchRest::ClassExtension::InstanceMethods.instance_methods.all? {|m| base.respond_to?(m)}
# do nothing
elsif CouchRest::ClassExtension::InstanceMethods.instance_methods.any? {|m| base.respond_to?(m)}
raise RuntimeError, "Conflicting extentions to Class, work it out"
else
base.send(:include, CouchRest::ClassExtension::InstanceMethods)
end
end
module InstanceMethods
# Defines class-level and instance-level attribute reader.
#
# @param *syms<Array> Array of attributes to define reader for.
@ -173,3 +185,7 @@ RUBY
class_inheritable_writer(*syms)
end
end
end
end
Class.send(:include, CouchRest::ClassExtension)

View file

@ -0,0 +1,59 @@
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'couchrest', 'support', 'class')
describe CouchRest::ClassExtension do
before :all do
class FullyDefinedClassExtensions
def self.respond_to?(method)
if CouchRest::ClassExtension::InstanceMethods.instance_methods.include?(method)
true
else
super
end
end
end
class PartDefinedClassExtensions
def self.respond_to?(method)
methods = CouchRest::ClassExtension::InstanceMethods.instance_methods
methods.delete('cattr_reader')
if methods.include?(method)
false
else
super
end
end
end
class NoClassExtensions
def self.respond_to?(method)
if CouchRest::ClassExtension::InstanceMethods.instance_methods.include?(method)
false
else
super
end
end
end
end
it "should not include InstanceMethods if the class extensions are already defined" do
FullyDefinedClassExtensions.send(:include, CouchRest::ClassExtension)
FullyDefinedClassExtensions.ancestors.should_not include(CouchRest::ClassExtension::InstanceMethods)
end
it "should raise RuntimeError if the class extensions are only partially defined" do
lambda {
PartDefinedClassExtensions.send(:include, CouchRest::ClassExtension)
}.should raise_error(RuntimeError)
end
it "should include class extensions if they are not already defined" do
NoClassExtensions.send(:include, CouchRest::ClassExtension)
NoClassExtensions.ancestors.should include(CouchRest::ClassExtension::InstanceMethods)
end
end