Skip to content

Instantly share code, notes, and snippets.

View pewniak747's full-sized avatar

Tomasz Pewiński pewniak747

View GitHub Profile
"files": ["src/index.ts", "src/apidoc.d.ts"]
export interface Order {
id: number;
productId: number;
quantity: number;
shipDate: string;
status: "placed" | "approved" | "delivered";
complete: boolean;
}
class SomeService
def call
call_service_a
end
private
def service_a
# ... uses status objects
end
case result
when Success then
# success handler
when SpecificError then
# specific error handler
when Error then
# error handler
end
Success = Struct.new(:data) do
def success?
true
end
end
Error = Struct.new(:error, :code, :details) do
def error_message
error.to_s
end
class ServiceObject
attr_reader :input
# ...
private
def private_method_with_input
input.field # access input as method
end
class APIResponder < ActionController::Responder
private
def display(resource, options = {})
super(resource.data, options)
end
def has_errors?
!resource.success?
describe CreateInvoice do
subject { CreateInvoice.new(user, notification_center, form) }
let(:user) { instance_double("User", subscriber?: true) }
let(:notification_center) { instance_double("NotificationCenter", invoice_created: nil)
let(:form) { instance_double("InvoiceForm", company_name: "ACME Corp.", billing_date: Time.now)
describe "passing valid parameters" do
class InvoicesController < ApplicationController
# ...
def create
form = InvoiceForm.new(params)
result = CreateInvoice.new(current_user, form).call
@invoice = result.invoice
if result.success?
redirect_to @invoice
else
class InvoiceForm
attr_reader :params
def initialize(params)
@params = params
end
def billing_date
Time.new(params[:year], params[:month], params[:day]) if time_data_present?
rescue ArgumentError
end