Created
May 11, 2022 23:10
-
-
Save luizpvas/dab8a80124d59d67caedbb42b7794314 to your computer and use it in GitHub Desktop.
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
# CONTROLER | |
class MyApp::InvoicesController | |
def index | |
input = { interval: params[:interval] } | |
MyApp::GetInvoices.call(input) do |on| | |
on.success do |result| | |
# ... | |
end | |
on.failure(:invalid_attributes) do |result| | |
# ... | |
end | |
end | |
end | |
end | |
# USE-CASE | |
class MyApp::GetInvoices < ::Micro::Case | |
attribute :interval, cast: MyApp::DateRange | |
def call! | |
run_query(interval.from, interval.to) | |
end | |
end | |
# CUSTOM TYPE | |
class MyApp::DateRange | |
# Ainda não tenho certeza dessa API, se é melhor retornar algum valor (Kind::Some) ou lançar uma exception | |
def self.cast(input) | |
raise "Must be a string" unless input.is_a?(String) | |
date1, date2 = input.split(" - ") | |
new(Time.new(date1), Time.new(date2)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment