Skip to content

Instantly share code, notes, and snippets.

View hdoan741's full-sized avatar

Harry Doan hdoan741

  • Retool Inc.
  • San Francisco, CA
View GitHub Profile
class AddTeacherToSite < TypedMutation
class Args < T::Struct
const :site, Site
const :teacher, Teacher
end
def policy
# pass the privacy policy required
end
class AddTeacherToSite < Mutation
def required_arguments
[
model_or_id(:site, Site),
model_or_id(:teacher, Teacher),
]
end
def policy
# list the privacy policy required for this
@hdoan741
hdoan741 / sorbet_example_2.txt
Created March 2, 2020 22:52
Sorbet-Rails blogpost - Sorbet example 2
Method `to_i` does not exist on `Array` component of
`T.any(String, Integer, T::Array[Integer])`
4 |hash = str_or_int_or_array.to_i
@hdoan741
hdoan741 / sorbet_example_1.rb
Created March 2, 2020 22:51
Sorbet-Rails blogpost - sorbet example 1
str_or_int_or_array = ["1", 2, [3]].sample
hash = str_or_int_or_array.to_i
# NoMethodError: method to_i doesn't exist on `Array`
@hdoan741
hdoan741 / sorbet_example_3.rb
Last active March 2, 2020 22:51
Sorbet-Rails blogpost - Sorbet full example
# typed: true
extend T::Sig
sig { params(name: String).returns(Integer) }
def main(name)
puts "Hello, #{name}!"
end
main("Sorbet") # ok!
main() # error: Not enough arguments provided
@hdoan741
hdoan741 / typed_params.rb
Created March 2, 2020 22:47
Sorbet-Rails blogpost - Typed Params example
# typed: strict
class MyCoolController < ApplicationController
class MyActionParams < T::Struct
const :id, T.nilable(Integer)
const :show, T.nilable(T::Boolean)
const :wands, T::Array[Integer]
end
sig { void }
def my_action
typed_params = TypedParams[MyActionParams].new.extract!(params)
@hdoan741
hdoan741 / mailer.rbi
Created March 2, 2020 22:46
SR blogpost mailer.rbi
# mailer.rb
class HogwartsAcceptanceMailer < ApplicationMailer
extend T::Sig
sig { params(student: Wizard).void }
def notify(student)
# TODO: send an owl
end
def notify_retry(student)
@hdoan741
hdoan741 / routes.rbi
Last active March 2, 2020 22:45
routes.rbi
# -- generated routes.rbi
# Sigs for route /test/index(.:format)
sig { params(args: T.untyped, kwargs: T.untyped).returns(String) }
def test_index_path(*args, **kwargs); end
sig { params(args: T.untyped, kwargs: T.untyped).returns(String) }
def test_index_url(*args, **kwargs); end
@hdoan741
hdoan741 / wizard_relation.rbi
Created March 2, 2020 22:42
Wizard RBI with relation
# - example wizard.rbi
class Wizard < ApplicationRecord
sig { returns(T.nilable(String)) }
def name; end
sig { returns(T.nilable(::Wand)) }
def wand; end
sig { returns(::SpellBook::ActiveRecord_Associations_CollectionProxy) }
def spell_books; end
@hdoan741
hdoan741 / wizard.rbi
Created March 2, 2020 22:41
example wizard.rbi
# -- wizard.rbi
class Wizard
sig { params(id: String).returns(Wizard)) }
def self.find(id); end
end