
Not thread safe! But I don't think we care. We want to evaluate the env DSL block in the context of ENV for asthetic reasons, but we also want access to methods on the requirement instance. We can use #instance_exec to pass the requirement itself into the block: class Foo < Requirement env do |req| append 'PATH', req.some_path end def some_path which 'something' end end Also add a simplified version of Object#instance_exec for Ruby 1.8.6.
16 lines
422 B
Ruby
16 lines
422 B
Ruby
class Object
|
|
def instance_exec(*args, &block)
|
|
method_name = :__temp_instance_exec_method
|
|
singleton_class = (class << self; self; end)
|
|
singleton_class.class_eval do
|
|
define_method(method_name, &block)
|
|
end
|
|
|
|
send(method_name, *args)
|
|
ensure
|
|
singleton_class.class_eval do
|
|
remove_method(method_name) if method_defined?(method_name)
|
|
end
|
|
end unless method_defined?(:instance_exec)
|
|
end
|