lib/unextendable/object.rb in unextendable-0.1.3 vs lib/unextendable/object.rb in unextendable-0.1.4
- old
+ new
@@ -1,20 +1,7 @@
class Object
- def meta_class(&block)
- class << self
- yield if block_given?
- self
- end
- end
- alias :singleton_class :meta_class
-
- def meta_class?
- !!(meta_class rescue false)
- end
- alias :singleton_class? :meta_class?
-
meta_class do
def extended_modules
@extended_modules ||= []
end
@@ -41,18 +28,10 @@
meta_class.extended_modules.delete mod if unextend? mod, &block
end
end
end
- def respond_to?(symbol, include_private = false)
- if meta_class? && meta_class.extended_modules.any?{|mod| mod.unextendable?}
- meta_class.extended_modules.detect{|x| x.instance_methods.include? symbol.to_s} || meta_class.method_procs[symbol.to_s].class == Proc
- else
- !(meta_class? && meta_class.method_procs.key?(symbol.to_s) && meta_class.method_procs[symbol.to_s].nil?) && super
- end
- end
-
private
def unextend?(mod, &block)
mod.unextendable? && (!block_given? || !!block.call(mod))
end
@@ -61,20 +40,30 @@
return unless (mod.class == Module) && mod.unextendable?
mod.instance_methods.each do |method_name|
wrap_unextendable_method method_name
end
+
+ return if @wrapped_respond_to
+
+ instance_eval <<-CODE
+ def respond_to?(symbol, include_private = false)
+ meta_class.extended_modules.detect{|x| x.instance_methods.collect(&:to_s).include? symbol.to_s} || meta_class.method_procs[symbol.to_s].class == Proc
+ end
+ CODE
+
+ @wrapped_respond_to = true
end
- def wrap_unextendable_method(name)
- return if meta_class.method_procs.key? name
+ def wrap_unextendable_method(method_name)
+ return if meta_class.method_procs.key? method_name.to_s
- meta_class.method_procs[name] = respond_to?(name) ? method(name).to_proc : nil
+ meta_class.method_procs[method_name.to_s] = respond_to?(method_name) ? method(method_name.to_s).to_proc : nil
instance_eval <<-CODE
- def #{name}(*args, &block)
- call_unextendable_method :#{name}, *args, &block
+ def #{method_name}(*args, &block)
+ call_unextendable_method :#{method_name}, *args, &block
end
CODE
end
def add_extended_module(mod)
@@ -89,10 +78,10 @@
raise NoMethodError, "undefined method `#{method_name}' for #{self.inspect}"
end
end
def method_for(method_name)
- mod = meta_class.extended_modules.detect{|x| x.instance_methods.include? method_name.to_s}
+ mod = meta_class.extended_modules.detect{|x| x.instance_methods.collect(&:to_s).include? method_name.to_s}
mod ? mod.instance_method(method_name).bind(self) : proc_for(method_name)
end
def proc_for(method_name)
meta_class.method_procs.key?(method_name.to_s) ?
\ No newline at end of file