Code Explainers

Code explainers tagged #callbacks

rust
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
use std::thread;

Polling a directory for new files in Rust

polling filesystem callbacks
Intermediate 6 steps
ruby
class ReverseGeocodeSignupJob < ApplicationJob
  queue_as :default
 
  retry_on Geocoder::OverQueryLimitError, wait: 5.minutes, attempts: 3

How Rails jobs handle reverse geocoding

background-jobs error-handling geocoding
Intermediate 7 steps
python
import fnmatch
from collections import defaultdict
from typing import Any, Callable
 

A pattern-matching event bus in Python

pub-sub decorators pattern-matching
Intermediate 8 steps
ruby
class Current < ActiveSupport::CurrentAttributes
  attribute :user, :request_id, :user_agent, :ip_address
 
  resets { Time.zone = nil }

Per-request audit trails with Current in Rails

thread-local-state auditing concerns
Intermediate 8 steps
python
from celery import shared_task, signature
from django.core.mail import send_mail
 
from .models import Report, ReportExport

Chaining Celery tasks in Django

asynchronous tasks task chaining retries
Intermediate 8 steps
ruby
class Post < ApplicationRecord
  belongs_to :author, counter_cache: false
  has_many :comments, dependent: :destroy
end

A custom counter cache in Rails

counter-cache callbacks associations
Intermediate 7 steps
php
<?php
 
namespace App\Services;
 

Building a profanity filter in PHP

regex text-processing immutability
Intermediate 6 steps
ruby
class Article < ApplicationRecord
  belongs_to :author
  has_many :taggings, dependent: :destroy
  has_many :tags, through: :taggings

Keeping a search index fresh in Rails

callbacks background-jobs debounce
Intermediate 9 steps
ruby
class Admin::AccountsController < Admin::BaseController
  before_action :set_account, only: :destroy
 
  def destroy

A guarded destroy action in Rails

controllers callbacks transactions
Intermediate 8 steps
ruby
class Membership < ApplicationRecord
  belongs_to :organization
  belongs_to :user
 

Enforcing scoped uniqueness in Rails

validations uniqueness callbacks
Intermediate 8 steps
ruby
class Order < ApplicationRecord
  include AASM
 
  belongs_to :warehouse

Modeling an order lifecycle with AASM in Rails

state machine guards callbacks
Intermediate 10 steps
ruby
module Authenticatable
  extend ActiveSupport::Concern
 
  included do

JWT authentication as a Rails concern

authentication jwt concerns
Intermediate 6 steps
ruby
class Contact < ApplicationRecord
  PHONE_FORMATS = {
    "US" => /\A\+1\d{10}\z/,
    "GB" => /\A\+44\d{10}\z/,

Country-aware phone validation in Rails

validation callbacks regex
Intermediate 6 steps
ruby
class Product < ApplicationRecord
  belongs_to :category, touch: true
 
  has_many :reviews, dependent: :destroy

How Rails models wire up associations

active record associations validations
Intermediate 7 steps
java
public List<OrderSummary> streamRecentOrders(LocalDateTime since, Consumer<OrderSummary> handler) {
    String sql = """
        SELECT id, customer_id, total_cents, status, created_at
        FROM orders

Streaming large JDBC result sets safely

jdbc streaming resource-management
Intermediate 7 steps
ruby
class Order
  class InvalidTransition < StandardError; end
 
  TRANSITIONS = {

A state machine for order transitions in Ruby

state-machine data-driven error-handling
Intermediate 8 steps
ruby
class ApplicationJob < ActiveJob::Base
  retry_on ActiveRecord::Deadlocked, wait: 5.seconds, attempts: 3
  discard_on ActiveJob::DeserializationError
 

Instrumenting every job in Rails

active-job structured-logging retries
Intermediate 6 steps
ruby
class PaymentMethod < ApplicationRecord
  belongs_to :account
 
  encrypts :card_number, deterministic: false

How Rails encrypts sensitive payment fields

encryption validation scopes
Intermediate 9 steps