Created
October 28, 2020 17:40
-
-
Save serradura/0ca656e029beed8110f4725aa0aef57f to your computer and use it in GitHub Desktop.
Examples of dependency injection using the u-case gem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'u-case', '~> 4.2.1' | |
end | |
require 'json' | |
module ReportFormatters | |
HTML = -> data { %[<ul>#{data.map { |product, amount| "<li>#{product}: #{amount}</li>" }.join}</ul>] } | |
JSON = -> data { data.to_json } | |
PlainText = -> data { data.to_s } | |
end | |
class FetchReportData < Micro::Case | |
def call! | |
Success result: { data: { onions: 31, potatoes: 24, eggs: 10 } } | |
end | |
end | |
class GenerateReport < Micro::Case | |
attributes :data, :formatter | |
def call! | |
return Failure() unless data || formatter | |
Success result: { report: formatter.call(data) } | |
end | |
end | |
FetchReportData | |
.call | |
.then(GenerateReport, formatter: ReportFormatters::HTML) | |
.on_success { |result| p result[:report] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'u-case', '~> 4.2.1' | |
end | |
require 'json' | |
module ReportFormatters | |
HTML = -> data { %[<ul>#{data.map { |product, amount| "<li>#{product}: #{amount}</li>" }.join}</ul>] } | |
JSON = -> data { data.to_json } | |
end | |
class FetchReportData < Micro::Case | |
def call! | |
Success result: { data: { onions: 31, potatoes: 24, eggs: 10 } } | |
end | |
end | |
class GenerateReport < Micro::Case | |
attributes :data, :formatter | |
def call! | |
return Failure() unless data || formatter | |
Success result: { report: formatter.call(data) } | |
end | |
end | |
GenerateReportAsJSON = Micro::Cases.flow([ | |
FetchReportData, | |
[GenerateReport, formatter: ReportFormatters::JSON] | |
]) | |
GenerateReportAsJSON.call.on_success { |result| p result[:report] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'u-case', '~> 4.2.1' | |
end | |
require 'json' | |
module ReportFormatters | |
HTML = -> data { %[<ul>#{data.map { |product, amount| "<li>#{product}: #{amount}</li>" }.join}</ul>] } | |
JSON = -> data { data.to_json } | |
end | |
class GenerateReport < Micro::Case | |
attributes :data, :formatter | |
def call! | |
return Failure() unless data || formatter | |
Success result: { report: formatter.call(data) } | |
end | |
end | |
class GenerateReportAsHTML < Micro::Case | |
attribute :data | |
def call! | |
call(GenerateReport, formatter: ReportFormatters::HTML) | |
end | |
end | |
GenerateReportAsHTML | |
.call(data: { onions: 31, potatoes: 24, eggs: 10 }) | |
.on_success { |result| p result[:report] } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment