lib/eco/api/usecases/use_case_chain.rb in eco-helpers-0.9.1 vs lib/eco/api/usecases/use_case_chain.rb in eco-helpers-0.9.2
- old
+ new
@@ -3,11 +3,18 @@
module UseCases
class UseCaseChain < UseCase
MAX_CHAINS = 70
@@num_chains = 0
- def initialize(name, type:, root:, &block)
+ def initialize(name = nil, type: nil, root:, usecase: nil, &block)
+ if usecase
+ raise "Expected Eco::API::UseCases::UseCase. Given #{usecase.class}" if !usecase.is_a?(Eco::API::UseCases::UseCase)
+ type = usecase.type
+ name = usecase.name
+ block = usecase.callback unless block
+ end
+
super(name, type: type, root: root, &block)
@chains = []
@resolved_chains = nil
end
@@ -62,13 +69,13 @@
return @resolved_chains if @resolved_chains
raise "Only UseGroup object can resolve chains. Given: #{use_group} " if use_group && !use_group.is_a?(UseGroup)
use_group = use_group || @root
@resolved_chains = @chains.map do |usecase|
- usecase = usecase.call(use_group) if usecase.respond_to? :call
+ usecase = usecase.call(use_group) if usecase.respond_to?(:call)
raise "A UseCase can only be chained with another UseCase" if usecase && !usecase.is_a?(UseCase)
- usecase.resolved_chains(use_group)
+ usecase.resolved_chains(use_group) if usecase.respond_to?(:resolved_chains)
usecase
end
end
private
@@ -81,9 +88,10 @@
resolved_chains.each do |usecase|
# chained cases use same params as parent case (out of simplicity)
data_chain = dm[self][:io].chain(usecase: usecase)
keyed_data = data_chain.params(keyed: true)
+ keyed_data.delete(:usecase)
dm[self][:chains].merge(usecase.launch(keyed_data))
end
end
end