lib/hobo_support/methodcall.rb in hobosupport-0.9.103 vs lib/hobo_support/methodcall.rb in hobosupport-0.9.104
- old
+ new
@@ -1,11 +1,11 @@
# The dot operator calls methods on objects. Power Dots are dots with extra features
#
-# .? calls a method if the reciever is not nil, returns nil
+# .? calls a method if the receiver is not nil, returns nil
# otherwise. We have to write it ._?. in order to be valid Ruby
#
-# .try. calls a mehod only if the recipient resonds to that method
+# .try. calls a method only if the recipient responds to that method
require 'delegate'
require 'singleton'
require 'blankslate'
@@ -54,34 +54,17 @@
class SafeNil
include Singleton
- DONT_REDEFINE_METHODS = "__id__", "__send__", "object_id"
-
- NIL_RESPONSE_METHODS = ["to_s", "to_json", "to_yaml", "__id__", "__is_a__", "__metaclass__", "__send__"]
-
- (NIL_RESPONSE_METHODS - DONT_REDEFINE_METHODS).each do |method|
- # can't use define_method with a block
- eval "
- def #{method}(*args, &b)
- nil.send(:#{method}, *args, &b)
- end"
+ begin
+ old_verbose, $VERBOSE = $VERBOSE, nil # just like Rails silence_warnings: suppress "warning: undefining `object_id' may cause serious problem"
+ instance_methods.each { |m| undef_method m unless m[0,2] == '__' }
+ ensure
+ $VERBOSE = old_verbose
end
- (instance_methods.map{|m| m.to_s} - NIL_RESPONSE_METHODS - DONT_REDEFINE_METHODS).each do |method|
- # can't use define_method with a block
- eval "
- def #{method}(*args, &b)
- nil
- end"
- end
-
- def to_s
- ""
- end
-
def method_missing(method, *args, &b)
return nil
end
end
@@ -118,6 +101,17 @@
def try(*args, &block)
HoboSupport.hobo_try(self, *args, &block)
end
end
end
+
+ module NamedScope
+ class Scope
+ # we need to make sure we don't trigger AssociationCollections' method_missing
+ def try(*args, &block)
+ HoboSupport.hobo_try(self, *args, &block)
+ end
+ end
+ end
+
end
+