Class: MethodDecorators::Deprecated
- Defined in:
- lib/method_decorators/deprecated.rb
Overview
Example:
class MyClass
extend MethodDecorators
+MethodDecorators::Deprecated
def deprecated_method
brand_new_method
end
end
When deprecated_method is called, message
"MyClass#deprecated_method is deprecated"
is output.
Another example:
class MyClass
extend MethodDecorators
+MethodDecorators::Deprecated.new('deprecated_method will be removed in the future')
def deprecated_method
brand_new_method
end
end
Above output given message
"deprecated_method will be removed in the future"
Custom message example:
class MyClass
extend MethodDecorators
+MethodDecorators::Deprecated.new {|class_name, method_name| "#{class_name}##{method_name} will be removed in the future. Use #{class_name}#brand_new_method instead"}
def deprecated_method
brand_new_method
end
end
Outputs
"MyClass#deprecated_method will be removed in the future. Use MyClass#brand_new_method instead"
As you see, you can use class name as the first argument and method name as the second in the block.
Formatter example:
class Formatter2
def call(class_name, method_name)
"#{class_name}##{method_name} will be removed after the next version. Use #{class_name}#brand_new_method instead"
end
end
class MyClass
extend MethodDecorators
formatter1 = ->(class_mane, method_name) {"#{class_name}##{method_name} will be removed in the next version. Use #{class_name}#brand_new_method instead"}
+MethodDecorators::Deprecated.new(formatter1)
def very_old_method
brand_new_method
end
+ MethodDecorators::Deprecated.new(Formatter2.new)
def deprecated_method
brand_new_method
end
end
Outputs
"MyClass#deprecated_method will be removed in the future. Use MyClass#brand_new_method instead"
You can give any object which responds to method “call” like Proc.
Constant Summary collapse
- DEFAULT_FORMATTER =
lambda {|class_name, method_name| "#{class_name}##{method_name} is deprecated"}
Instance Method Summary collapse
- #call(orig, this, *args, &blk) ⇒ Object
-
#initialize(message = nil, &blk) ⇒ Deprecated
constructor
A new instance of Deprecated.
- #message(class_name, method_name) ⇒ Object
Methods inherited from Decorator
Constructor Details
#initialize(message = nil, &blk) ⇒ Deprecated
Returns a new instance of Deprecated.
67 68 69 |
# File 'lib/method_decorators/deprecated.rb', line 67 def initialize(=nil, &blk) = || blk || DEFAULT_FORMATTER end |
Instance Method Details
#call(orig, this, *args, &blk) ⇒ Object
71 72 73 74 |
# File 'lib/method_decorators/deprecated.rb', line 71 def call(orig, this, *args, &blk) warn (this.class, orig.name) orig.call(*args, &blk) end |
#message(class_name, method_name) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/method_decorators/deprecated.rb', line 76 def (class_name, method_name) if .respond_to? :call .call(class_name, method_name) else .to_s end end |