lib/hobo_support/methodcall.rb in hobosupport-0.8.5 vs lib/hobo_support/methodcall.rb in hobosupport-0.8.6
- old
+ new
@@ -7,27 +7,51 @@
require 'delegate'
require 'singleton'
require 'blankslate'
+module HoboSupport
+ def self.hobo_try(this, *args, &block)
+ if args.length==0
+ # Hobo style try
+ CallIfAvailable.new(this)
+ else
+ # activesupport 2.3 style try
+ this.send(*args, &block)
+ end
+ end
+end
+
class Object
def _?()
self
end
- def try
- CallIfAvailable.new(self)
+ def try(*args, &block)
+ HoboSupport.hobo_try(self, *args, &block)
end
end
class NilClass
def _?()
SafeNil.instance
end
+
+
+ def try(*args)
+ if args.length==0
+ # Hobo style try
+ CallIfAvailable.new(self)
+ else
+ # activesupport 2.3 style try
+ nil
+ end
+ end
+
end
class SafeNil
include Singleton
@@ -61,6 +85,16 @@
@target.send(name, *args, &b) if @target.respond_to?(name)
end
end
+module ActiveRecord
+ module Associations
+ class AssociationProxy
+ # 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