Micro::Cases.map()
serradura opened this issue · 0 comments
serradura commented
Create an abstraction to allow the usage of the chain of responsibility pattern using the u-case. That, is call a collection of use cases with the same input
results = Micro::Cases.map([
Foo,
Bar,
FooOrBar,
FooAndBar,
[FooAndBar, bar: 'bar']
]).call(foo: 'foo')
PS: One of the ideas is to allow the usage of dependency injection like was described in this issue: #38
Next, you can see an implementation of the concept:
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'u-case', '~> 4.0.0'
end
class Foo < Micro::Case
attribute :foo
def call!
return Success(:filled_foo) if foo
Failure(:missing_foo)
end
end
class Bar < Micro::Case
attribute :bar
def call!
return Success(:filled_bar) if bar
Failure(:missing_bar)
end
end
class FooOrBar < Micro::Case
attributes :foo, :bar
def call!
return Success(:filled_foo_or_bar) if foo || bar
Failure(:missing_foo_and_bar)
end
end
class FooAndBar < Micro::Case
attributes :foo, :bar
def call!
return Success(:filled_foo_and_bar) if foo && bar
Failure(:missing_foo_or_bar)
end
end
results = [
Foo,
Bar,
FooOrBar,
FooAndBar
].map { |use_case| use_case.call(foo: 'foo') }
results.each do |result|
p(success: result.success?, data: result.data)
end
# {:success => true , :data => { :filled_foo => true }}
# {:success => false, :data => { :missing_bar => true }}
# {:success => true , :data => { :filled_foo_or_bar => true }}
# {:success => false, :data => { :missing_foo_or_bar => true }}
@MatheusRich thanks to help me to evolve the idea! 👏