Fix a bug where class extentions were conflicting with ActiveSupport
This commit is contained in:
parent
d95b9de50d
commit
fdb0854859
2 changed files with 215 additions and 140 deletions
|
@ -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.
|
||||
|
@ -39,18 +51,18 @@ class Class
|
|||
syms.flatten.each do |sym|
|
||||
next if sym.is_a?(Hash)
|
||||
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
||||
unless defined? @@#{sym}
|
||||
@@#{sym} = nil
|
||||
end
|
||||
unless defined? @@#{sym}
|
||||
@@#{sym} = nil
|
||||
end
|
||||
|
||||
def self.#{sym}
|
||||
@@#{sym}
|
||||
end
|
||||
def self.#{sym}
|
||||
@@#{sym}
|
||||
end
|
||||
|
||||
def #{sym}
|
||||
@@#{sym}
|
||||
end
|
||||
RUBY
|
||||
def #{sym}
|
||||
@@#{sym}
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -65,21 +77,21 @@ RUBY
|
|||
options = syms.last.is_a?(Hash) ? syms.pop : {}
|
||||
syms.flatten.each do |sym|
|
||||
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
||||
unless defined? @@#{sym}
|
||||
@@#{sym} = nil
|
||||
end
|
||||
unless defined? @@#{sym}
|
||||
@@#{sym} = nil
|
||||
end
|
||||
|
||||
def self.#{sym}=(obj)
|
||||
@@#{sym} = obj
|
||||
end
|
||||
RUBY
|
||||
def self.#{sym}=(obj)
|
||||
@@#{sym} = obj
|
||||
end
|
||||
RUBY
|
||||
|
||||
unless options[:instance_writer] == false
|
||||
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
||||
def #{sym}=(obj)
|
||||
@@#{sym} = obj
|
||||
end
|
||||
RUBY
|
||||
def #{sym}=(obj)
|
||||
@@#{sym} = obj
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -114,19 +126,19 @@ RUBY
|
|||
|
||||
ivars.each do |ivar|
|
||||
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def self.#{ivar}
|
||||
return @#{ivar} if self.object_id == #{self.object_id} || defined?(@#{ivar})
|
||||
ivar = superclass.#{ivar}
|
||||
return nil if ivar.nil? && !#{self}.instance_variable_defined?("@#{ivar}")
|
||||
@#{ivar} = ivar && !ivar.is_a?(Module) && !ivar.is_a?(Numeric) && !ivar.is_a?(TrueClass) && !ivar.is_a?(FalseClass) && !ivar.is_a?(Symbol) ? ivar.dup : ivar
|
||||
end
|
||||
RUBY
|
||||
def self.#{ivar}
|
||||
return @#{ivar} if self.object_id == #{self.object_id} || defined?(@#{ivar})
|
||||
ivar = superclass.#{ivar}
|
||||
return nil if ivar.nil? && !#{self}.instance_variable_defined?("@#{ivar}")
|
||||
@#{ivar} = ivar && !ivar.is_a?(Module) && !ivar.is_a?(Numeric) && !ivar.is_a?(TrueClass) && !ivar.is_a?(FalseClass) && !ivar.is_a?(Symbol) ? ivar.dup : ivar
|
||||
end
|
||||
RUBY
|
||||
unless instance_reader == false
|
||||
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def #{ivar}
|
||||
self.class.#{ivar}
|
||||
end
|
||||
RUBY
|
||||
def #{ivar}
|
||||
self.class.#{ivar}
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -147,14 +159,14 @@ RUBY
|
|||
instance_writer = ivars.pop[:instance_writer] if ivars.last.is_a?(Hash)
|
||||
ivars.each do |ivar|
|
||||
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def self.#{ivar}=(obj)
|
||||
@#{ivar} = obj
|
||||
end
|
||||
RUBY
|
||||
def self.#{ivar}=(obj)
|
||||
@#{ivar} = obj
|
||||
end
|
||||
RUBY
|
||||
unless instance_writer == false
|
||||
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def #{ivar}=(obj) self.class.#{ivar} = obj end
|
||||
RUBY
|
||||
def #{ivar}=(obj) self.class.#{ivar} = obj end
|
||||
RUBY
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -172,4 +184,8 @@ RUBY
|
|||
class_inheritable_reader(*syms)
|
||||
class_inheritable_writer(*syms)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Class.send(:include, CouchRest::ClassExtension)
|
||||
|
|
59
spec/couchrest/support/class_spec.rb
Normal file
59
spec/couchrest/support/class_spec.rb
Normal 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
|
Loading…
Add table
Reference in a new issue