Created
October 5, 2010 03:27
-
-
Save sebastiandeutsch/610936 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I wanted to | |
# define a class which has a foo method | |
# which can also be called from a block | |
# so you can spice it and call the super | |
# class method again | |
# | |
# thanks to banisterfiend, xxxxxx and coderr | |
# for helping me on this one | |
class Document | |
def self.foo(test) | |
puts "foo: #{test}" | |
end | |
module TabProxy | |
def foo(test) | |
puts "block" | |
super(test) | |
end | |
end | |
def self.tab( name, &block ) | |
context = eval 'self', block.binding | |
dup_context = context.dup | |
dup_context.extend TabProxy | |
dup_context.instance_eval &block | |
dup_context.instance_variables.each do |v| | |
context.instance_variable_set(v, dup_context.instance_variable_get(v)) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dummy < Document | |
foo :foo | |
tab :general do | |
foo :bar | |
end | |
foo :baz | |
end | |
# should output | |
foo: foo | |
block | |
foo: bar | |
foo: baz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment