Skip to content

Instantly share code, notes, and snippets.

View leandro's full-sized avatar
🏠
Working from home

Leandro Camargo leandro

🏠
Working from home
View GitHub Profile
@leandro
leandro / curry.js
Last active December 20, 2021 15:04
A curry function on JS
const curry = (fn, arity = null) => {
const realArity = fn.length;
const providedArity = arity === null ? realArity :
(arity > realArity ? realArity : arity);
return (...args) => {
const argsUsed = args.length > providedArity ? providedArity : args.length;
const finalFn = fn.bind(this, ...args.slice(0, argsUsed));
return realArity === argsUsed ? finalFn() : curry(finalFn);
@leandro
leandro / example.rb
Last active December 22, 2021 14:11
A simple memoization module (concern)
class MyClass
include Memoization::Memoizable
memoize :my_memoizable_method
def my_memoizable_method
p 123
:abc
end
end
@leandro
leandro / abreai-api-howto.md
Created November 22, 2021 13:05
Usando API do abre.ai

Basicamente há duas maneiras: via text/plain (por motivos de retrocompatibilidade) e via application/json. Para você escolher entre um dos dois, basta, primeiramente setar o seu header "content-type" para um dos dois:

  • "Content-Type: text/plain"
  • "Content-Type: application/json"

Para facilitar, deixo abaixo alguns exemplos prontos usando o curl:

$ curl -XPOST -H "Content-Type: text/plain" -d 'url_translation[url]=https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html' https://abre.ai/_/generate
$ https://abre.ai/cFgb

$ curl -XPOST -H "Content-Type: application/json" -d '{"url_translation":{"url":"https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html"}}' https://abre.ai/_/generate
@leandro
leandro / smart_around_hook_spec.rb
Created August 17, 2021 18:51
A way to know which example (in any given rspec file) is being run out of total examples in the spec file
RSpec.describe 'whatever' do
around do |example|
total_examples = example.example_group.parent_groups.last.descendant_filtered_examples.size
examples_executed = example.reporter.examples.size
do_initial_thing if examples_executed == 1
example.run
do_final_thing if examples_executed == total_examples
end
@leandro
leandro / entrypoint.sh
Created December 21, 2020 22:50
Docker entrypoint para migrations no rails (e mais)
#!/bin/sh
set -e
[ -z "$(env | grep RUN_RAILS_CMDS)" ] && RUN_RAILS_CMDS=1
rails_cmd () {
if [ -z "$RUN_RAILS_CMDS" ]; then
echo "Rails commands are deactivated because RUN_RAILS_CMDS is empty." >&2
return 0
defmodule Autor do
def novo(nome, _) when nome in [nil, ""], do: {:error, "nome não pode ser vazio"}
def novo(_, email) when email in [nil, ""], do: {:error, "email não pode ser vazio"}
def novo(nome, email), do: {:ok, %{nome: nome, email: email}}
end
def create
@block_edit = false
debit_params = BankDebit.new(permitted_params[:bank_debit])
.attributes.symbolize_keys.merge(user_id: current_user.id)
Bank.transaction do
current_user.up_score!(1.3) if current_user.Potencial?
create_plot_debits!(debit_params)
create_debits!(debit_params)
redirect_to cash_path, notice: 'Lançamento realizado com sucesso'
@leandro
leandro / controller.rb
Last active March 19, 2019 14:45
Alterar nome do Admin mesmo com a senha em branco.
# frozen_string_literal: true
# Main Backoffice
class Backoffice::AdminsController < BackofficeController
before_action :set_admin, only: %i[edit update destroy]
after_action :verify_authorized, only: :new
after_action :verify_policy_scoped, only: :index
def index
@admins = policy_scope(Admin)
var modalQuarteirao = $('#modal-quarteirao')
, botaoQuarteiraoCancelar = modalQuarteirao.find('.cancelar');
new ProtetorDeAlteracoes(modalQuarteirao, botaoQuarteiraoCancelar);
@leandro
leandro / spec__models__concerns__durable_record_spec.rb
Last active November 20, 2018 02:12
Practical (or too dirty?) way of testing something
require 'spec_helper'
shared_examples_for 'durable_record' do
let(:duration) { nil }
let(:end_date) { nil }
let(:start_date) { nil }
let(:instance) { described_class.new }
let(:attributes) do
{ duration: duration, end_date: end_date, start_date: start_date }
end