1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
module Generator
module ClassMethods
attr_reader :configurators
def factory
klass = Class.new(self)
yield klass if block_given?
klass
end
def call(*args, &block)
new(*args, &block).tap { |object| object.call }
end
def generate(&block)
factory.call(&block)
end
def mixin(mod)
include mod
end
def configure(&block)
configurators << block
end
def configurators
@configurators ||= []
end
end
def self.included(base)
base.extend(ClassMethods)
end
def initialize(*args)
setup(*args)
run_configurators
configure(*args)
yield self if block_given?
end
def setup(*args)
end
def configure(*args)
end
private
def run_configurators
self.class.configurators.each do |configurator|
configurator.call(self)
end
end
end
|