ruby
50 lines · 7 steps
Memoized report metrics in Ruby in Rails
A report object computes each metric once and caches it, so a single summary call reuses shared queries.
Explained by
highlit
1class ReportGenerator
2 def initialize(account, period)
3 @account = account
4 @period = period
5 end
6
7 def summary
8 {
9 revenue: total_revenue,
10 churn_rate: churn_rate,
11 active_customers: active_customers.size,
12 top_products: top_products
13 }
14 end
15
16 def total_revenue
17 @total_revenue ||= paid_invoices.sum(&:amount_cents) / 100.0
18 end
19
20 def churn_rate
21 return @churn_rate if defined?(@churn_rate)
22
23 starting = @account.customers.active_at(@period.begin).count
24 @churn_rate = starting.zero? ? 0.0 : (cancelled_count.to_f / starting).round(4)
25 end
26
27 def active_customers
28 @active_customers ||= @account.customers.active_during(@period).to_a
29 end
30
31 private
32
33 def paid_invoices
34 @paid_invoices ||= @account.invoices.paid.where(issued_at: @period)
35 end
36
37 def cancelled_count
38 @cancelled_count ||= @account.subscriptions.cancelled_during(@period).count
39 end
40
41 def top_products
42 @top_products ||= paid_invoices
43 .flat_map(&:line_items)
44 .group_by(&:product_id)
45 .transform_values { |items| items.sum(&:amount_cents) }
46 .sort_by { |_, total| -total }
47 .first(5)
48 .map(&:first)
49 end
50end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Memoizing each metric method turns repeated calls into cheap lookups after the first computation.
- 2Use defined? instead of ||= when a legitimate result can be falsey like 0.0.
- 3Grouping metric logic into one object lets several methods share the same underlying query cheaply.
Related explainers
python
from functools import lru_cache import math
Memoizing number theory with lru_cache
memoization
number-theory
caching
Intermediate
8 steps
python
import re from collections import defaultdict from pathlib import Path
Summarizing log files by date in Python
regex
parsing
aggregation
Intermediate
7 steps
php
<?php namespace App\Services;
Caching tenant dashboard metrics in Laravel
caching
multi-tenancy
aggregation
Intermediate
7 steps
ruby
class PurgeStaleExportsJob < ApplicationJob queue_as :maintenance retry_on ActiveRecord::Deadlocked, wait: :polynomially_longer, attempts: 5
Purging stale exports with an Active Job in Rails
background-jobs
batching
error-handling
Intermediate
7 steps
ruby
class EmailValidator < ActiveModel::EachValidator EMAIL_FORMAT = /\A[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}\z/i def validate_each(record, attribute, value)
Writing a custom email validator in Rails
validation
regex
custom validators
Intermediate
7 steps
python
import csv from collections import defaultdict from dataclasses import dataclass, field from decimal import Decimal, InvalidOperation
Aggregating CSV sales by category in Python
dataclasses
csv-parsing
defaultdict
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/memoized-report-metrics-in-ruby-in-rails-explained-ruby-c941/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.