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:
parent
9fd03cab2f
commit
e1a07fadd9
9
why/metaid/attr_abort.rb
Normal file
9
why/metaid/attr_abort.rb
Normal 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
|
5
why/metaid/happy_truck.rb
Normal file
5
why/metaid/happy_truck.rb
Normal 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
15
why/metaid/mail_truck.rb
Normal 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
19
why/metaid/metaid.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user