Working through an old @why post on metaprogramming

since I'd like to make the JRuby rabbitmq stuff a bit less boilerplate.
This commit is contained in:
Dan Buch 2012-12-02 20:21:14 -05:00
parent 9fd03cab2f
commit e1a07fadd9
4 changed files with 48 additions and 0 deletions

9
why/metaid/attr_abort.rb Normal file
View File

@ -0,0 +1,9 @@
class Class
def attr_abort(*args)
abort "Please no more attributes today."
end
end
class MyNewClass
attr_abort :id, :diagram, :telegram
end

View File

@ -0,0 +1,5 @@
require_relative 'mail_truck'
class HappyTruck < MailTruck
company "Happy's -- We Bring the Mail, and That's It!"
end

15
why/metaid/mail_truck.rb Normal file
View File

@ -0,0 +1,15 @@
require_relative 'metaid'
class MailTruck
attr_accessor :driver, :route
def initialize(driver, route)
@driver, @route = driver, route
end
def self.company(name)
meta_def :company do
name
end
end
end

19
why/metaid/metaid.rb Normal file
View File

@ -0,0 +1,19 @@
class Object
def metaclass
class << self
self
end
end
def meta_eval(&block)
metaclass.instance_eval(&block)
end
def meta_def(name, &block)
meta_eval { define_method(name, &block) }
end
def class_def(name, &block)
class_eval { define_method(name, &block) }
end
end