Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update README

  • Loading branch information...
commit 437cfcd81c5385d34d781ea9c98f2659503a1de0 1 parent 469184f
@charleseff authored
Showing with 37 additions and 8 deletions.
  1. +1 −0  .gitignore
  2. +20 −8 README
  3. +16 −0 lib/alias_method_chain_block.rb
View
1  .gitignore
@@ -0,0 +1 @@
+.idea
View
28 README
@@ -1,7 +1,3 @@
-the functionality I'm looking for is something like this:
-
-
-
class SomeObject
def do_stuff
puts "doing stuff"
@@ -9,22 +5,38 @@ class SomeObject
end
extra_stuff = -> do
- do_stuff_without_extra_stuff
puts "doing extra stuff"
+ do_stuff_without_extra_stuff
end
some_object = SomeObject.new
-alias_method_chain_block(SomeObject, :do_stuff, :extra_stuff) do
+puts "Outside the block:"
+some_object.do_stuff
+puts
+
+puts "Inside the block:"
+alias_method_chain(SomeObject, :do_stuff, :extra_stuff, extra_stuff) do
some_object.do_stuff
end
+puts
+puts "Outside the block again:"
some_object.do_stuff
-Output:
+
+
+OUTPUT:
+
+Outside the block:
+doing stuff
+
+Inside the block:
doing stuff
doing extra stuff
-doing stuff
+
+Outside the block again:
+doing stuff
View
16 lib/alias_method_chain_block.rb
@@ -0,0 +1,16 @@
+class Object
+ if method_defined? :alias_method_chain
+ # alias method chain the :alias_method_chain method. meta.
+ # todo: complete me
+ else
+ def alias_method_chain(target_class, target, feature, feature_proc, &block)
+ target_class.send :alias_method, "#{target}_without_#{feature}", target
+ target_class.send :define_method, target, &feature_proc
+
+ block.call
+
+ target_class.send :remove_method, target
+ target_class.send :alias_method, target, "#{target}_without_#{feature}"
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.