Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (39 sloc) 1.15 KB
module Deject
UninitializedDependency = Class.new StandardError
end
def Deject(klass)
uninitialized_error = lambda do |meth|
raise Deject::UninitializedDependency, "#{meth} invoked before being defined"
end
# define klass.dependency
klass.define_singleton_method :dependency do |meth, &default_block|
# define the getter
define_method meth do
uninitialized_error[meth] unless default_block
value = instance_eval &default_block
define_singleton_method(meth) { value }
send meth
end
# define the override
define_method :"with_#{meth}" do |value=nil, &block|
# redefine getter if given a block
if block
define_singleton_method meth do
value = instance_eval &block
define_singleton_method(meth) { value }
send meth
end
# always return value if given a value
else
define_singleton_method(meth) { value }
end
self
end
end
# override multiple dependencies
klass.send :define_method, :with_dependencies do |overrides|
overrides.each { |meth, value| send "with_#{meth}", value }
self
end
klass
end
Jump to Line
Something went wrong with that request. Please try again.