Skip to content

Instantly share code, notes, and snippets.

View diegolinhares's full-sized avatar
🎯
Focusing

Diego Linhares diegolinhares

🎯
Focusing
View GitHub Profile
@serradura
serradura / mecha_basic.rb
Last active July 31, 2022 11:57
mecha.rb - a minimalist finite state machine implemented using Ruby 3.1 (basic = 34 LOC, enhanced = 53 LOC)
class Mecha
private attr_accessor(:states_map, :callbacks, :transitions, :current_state)
public :transitions, :current_state
def initialize(initial_state:, transitions:)
self.states_map = transitions.parameters.select { |(type, _)| type == :keyreq }.to_h { [_2, _2] }
self.callbacks = Hash.new { |hash, key| hash[key] = [] }
self.transitions = transitions.call(**states_map).transform_values(&:freeze).freeze
@serradura
serradura / pub_sub.rb
Created May 11, 2022 15:18
Implementação do pub/sub na mão equivalente a gem Wisper
module Susurro
def subscribe(handler)
subscribers << {name: nil, handler: handler}
self
end
def on(event_name, &handler)
subscribers << {name: event_name, handler: handler}
@serradura
serradura / dog_ceo_api.rb
Created May 2, 2022 17:10
Example of how to use the strategy pattern in Ruby to create a fake and a real API client
require 'uri'
require 'json'
require 'net/http'
# https://dog.ceo/dog-api/
class DogCeoAPI
class FakeStrategy
def get_random
{
"message" => "https://images.dog.ceo/breeds/terrier-welsh/lucy.jpg",
@itkrt2y
itkrt2y / association.rb
Last active September 3, 2024 11:45
Association dataloader with graphql-ruby
# official docs: https://graphql-ruby.org/dataloader/sources.html
# app/graphql/sources/association.rb
class Sources::Association < ::GraphQL::Dataloader::Source
def initialize(association_name, scope = nil)
@association_name = association_name
@scope = scope
end
def fetch(records)
@serradura
serradura / AES256.rb
Last active December 31, 2022 15:16
AES256
require 'base64'
require 'openssl'
require 'singleton'
require 'forwardable'
class AES256
class Config
class << self
attr_accessor :key
end
// DISCLAIMER : You can now probably use `data-turbo-action="advance"` on your frame to perform what this controller is aiming to do
// https://turbo.hotwired.dev/handbook/frames#promoting-a-frame-navigation-to-a-page-visit
// Note that you probably want to disable turbo cache as well for those page to make popstate work properly
import { navigator } from '@hotwired/turbo'
import { Controller } from '@hotwired/stimulus'
import { useMutation } from 'stimulus-use'
export default class extends Controller {
connect (): void {
@rponte
rponte / using-uuid-as-pk.md
Last active May 8, 2025 12:46
Não use UUID como PK nas tabelas do seu banco de dados

Pretende usar UUID como PK em vez de Int/BigInt no seu banco de dados? Pense novamente...

TL;TD

Não use UUID como PK nas tabelas do seu banco de dados.

Um pouco mais de detalhes

@serradura
serradura / rails_app.rb
Last active July 6, 2022 14:01
Rails + u-authorization
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'pry', '~> 0.13.1'
gem 'rails', '~> 6.0', '>= 6.0.3.4'
gem 'sqlite3', '~> 1.4', '>= 1.4.2'
gem 'u-authorization', '~> 2.3'
end
@serradura
serradura / observers.rb
Last active April 21, 2022 19:33
Simple observer (pub/sub) in Ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'u-test'
gem 'activerecord', require: 'active_record'
gem 'sqlite3'
end