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
|
require "uber/option"
module Uber
module Builder
def self.included(base)
base.extend DSL
base.extend Build
end
class Builders < Array
def call(context, *args)
each do |block|
klass = block.(context, *args) and return klass # Uber::Value#call()
end
context
end
def <<(proc)
super Uber::Option[proc, instance_exec: true]
end
end
module DSL
def builders
@builders ||= Builders.new
end
def builds(proc=nil, &block)
builders << (proc || block)
end
end
module Build
# Call this from your class to compute the concrete target class.
def build!(context, *args)
builders.(context, *args)
end
end
end
end
|