Skip to content

Instantly share code, notes, and snippets.

@luizpvas
Created May 11, 2022 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizpvas/dab8a80124d59d67caedbb42b7794314 to your computer and use it in GitHub Desktop.
Save luizpvas/dab8a80124d59d67caedbb42b7794314 to your computer and use it in GitHub Desktop.
# 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