Skip to content

Instantly share code, notes, and snippets.

View rinaldifonseca's full-sized avatar

Rinaldi Fonseca rinaldifonseca

View GitHub Profile
@rinaldifonseca
rinaldifonseca / example run
Last active February 24, 2024 02:59 — forked from MachinesAreUs/example run
Demonstration of trapping exits in Elixir
iex(2)> pid = spawn Traps, :init, []
waiting to die...
#PID<0.92.0>
Nobody tried to kill me! ^_^
waiting to die...
Nobody tried to kill me! ^_^
waiting to die...
iex(3)> Process.exit pid, :normal
true
(⊙_⊙') somebody tried to kill me! {:EXIT, #PID<0.88.0>, :normal}
@rinaldifonseca
rinaldifonseca / box.ex
Created February 10, 2024 20:13 — forked from teamon/box.ex
Define elixir structs with typespec with single line of code
defmodule Box do
defmacro __using__(_env) do
quote do
import Box
end
end
@doc """
Define module with struct and typespec, in single line
@rinaldifonseca
rinaldifonseca / README.md
Created January 27, 2021 21:19 — forked from jesster2k10/README.md
JWT Auth + Refresh Tokens in Rails

JWT Auth + Refresh Tokens in Rails

This is just some code I recently used in my development application in order to add token-based authentication for my api-only rails app. The api-client was to be consumed by a mobile application, so I needed an authentication solution that would keep the user logged in indefinetly and the only way to do this was either using refresh tokens or sliding sessions.

I also needed a way to both blacklist and whitelist tokens based on a unique identifier (jti)

Before trying it out DIY, I considered using:

<!DOCTYPE html>
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet">
<link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
<div id="app">
<v-app id="inspire">
require 'logger'
require 'byebug'
require 'dry/monads'
require 'dry/monads/do'
require 'dry/matcher/result_matcher'
module Dry
module Transaction
module Steps
@rinaldifonseca
rinaldifonseca / your_application.rb
Created June 4, 2020 00:11 — forked from nicholasjhenry/your_application.rb
PayRoll application, embedded in Rails, borrowing from Use Case Driven Architecture and DCI
## PayRoll gem
# lib/pay_roll.rb
module PayRoll
class << self
attr_accessor :employee_directory
def config
yield self
end
end
@rinaldifonseca
rinaldifonseca / readme.md
Created February 10, 2020 19:10 — forked from maxivak/readme.md
Integrating Gem/Engine and Main Rails App
@rinaldifonseca
rinaldifonseca / rails_engine_setup.md
Created January 10, 2020 12:35 — forked from livecodelife/rails_engine_setup.md
Rails Engine Setup with RSpec, Capybara, FactoryGirl, and DatabaseCleaner
  1. rails plugin new <project_name> -T -d postgresql --dummy-path=spec/dummy --skip-turbolinks --skip-spring --mountable
  • -T skips the creation of the test directory and the use of Test::Unit
  • --dummy-path=spec/dummy sets up your dummy app in a spec directory
  • --skip-turbolinks & --skip-spring creates a project that does not use turbolinks or spring
  • --mountable creates a "mountable" and namespace-isolated engine.
  1. In Gemfile:
  • gem 'rspec-rails' - user rspec in place of minitest (docs)
  • gem 'capybara' - act as a user navigating your site in tests (docs)
  • gem 'factory_girl_rails' - easily generate database objects without repetitive code in tests (docs)
  • gem 'database_cleaner' - clean your database between tests (docs)
@rinaldifonseca
rinaldifonseca / domain.md
Created January 6, 2020 16:18 — forked from vsavkin/domain.md
Building Rich Domain Models in Rails

Building Rich Domain Models in Rails

Abstract

Domain model is an effective tool for software development. It can be used to express really complex business logic, and to verify and validate the understanding of the domain among stakeholders. Building rich domain models in Rails is hard. Primarily, because of Active Record, which doesn't play well with the domain model approach.

One way to deal with this problem is to use an ORM implementing the data mapper pattern. Unfortunately, there is no production ready ORM doing that for Ruby. DataMapper 2 is going to be the first one.

Another way is to use Active Record just as a persistence mechanism and build a rich domain model on top of it. That's what I'm going to talk about here.

  • Dynamic Dispatch
  • Dynamic Method
  • Ghost Methods
  • Dynamic Proxies
  • Blank Slate
  • Kernel Method
  • Flattening the Scope (aka Nested Lexical Scopes)
  • Context Probe
  • Class Eval (not really a 'spell' more just a demonstration of its usage)
  • Class Macros