Skip to content

Instantly share code, notes, and snippets.

class Result{
constructor(public title: string, public imagePath: string) { }
}
class Sentence{
base: string;
values: string[];
results: { [id: string]: Result };
constructor(base: string, values: string[], results: { [id: string]: Result }) {
@jaimerson
jaimerson / rspec_custom_matcher.rb
Created September 25, 2019 22:50
RSpec custom matchers
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'rspec'
end
require 'rspec/autorun'
class Car
@jaimerson
jaimerson / interface_test.rb
Created September 6, 2017 13:41
Interface Implementation Test
require 'rspec/autorun'
class Parent
def self.descendants
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
def foo
fail NotImplementedError
end
@jaimerson
jaimerson / init.vim
Last active August 31, 2018 18:18
My nvim config
set number
set ruler
set hidden
set hlsearch " highlight search terms
set incsearch " show search matches as you type
set history=1000 " remember more commands and search history
set undolevels=1000 " use many muchos levels of undo
set wildignore=*.swp,*.bak,*.pyc,*.class
set title " change the terminal's title
set visualbell " don't beep
@jaimerson
jaimerson / final_benchmark_bikes.rb
Created August 1, 2016 21:54
Final benchmark bikes
Benchmark.bm do |x|
x.report(:naive) { BikesReport::NaiveApproach.new.to_a }
x.report(:ar_and_sql) { BikesReport::ActiveRecordSqlApproach.new.to_a }
x.report(:view) { BikesReport::ViewApproach.new.to_a }
end
# user system total real
# 9.430000 0.650000 10.080000 ( 10.306346)
# 0.020000 0.000000 0.020000 ( 0.125085)
# 0.010000 0.000000 0.010000 ( 0.011208)
@jaimerson
jaimerson / view_approach.rb
Created August 1, 2016 21:50
Bikes report view approach
class BikesReport::ViewApproach
def to_a
BikesReport
.all
.as_json
.map { |r| r.symbolize_keys!.merge!(model: MODELS.fetch(r[:model])) }
end
private
@jaimerson
jaimerson / bikes_report.rb
Created August 1, 2016 21:41
Bikes report final form
class BikesReport < ActiveRecord::Base
def self.refresh
Scenic
.database
.refresh_materialized_view(table_name, concurrently: false)
end
def readonly?
true
end
@jaimerson
jaimerson / [timestamp]_create_bikes_reports.rb
Created August 1, 2016 21:28
Materialized view with scenic
class CreateBikesReports < ActiveRecord::Migration
def change
create_view :bikes_reports, materialized: true
end
end
@jaimerson
jaimerson / create_bikes_report_view.rb
Created August 1, 2016 21:11
First approach at creating view
class CreateBikesReportView < ActiveRecord::Migration
def change
reversible do |dir|
dir.up do
execute(
<<-QUERY
CREATE MATERIALIZED VIEW bikes_reports AS
SELECT
bikes.name, bikes.model,
count(rentals.id) as times_rented,
@jaimerson
jaimerson / benchmark_naive_vs_ar_sql.rb
Created August 1, 2016 20:43
Benchmark of naive vs AR+SQL
Benchmark.bm do |x|
x.report(:naive) { BikesReport::NaiveApproach.new.to_a }
x.report(:ar_and_sql) { BikesReport::ActiveRecordSqlApproach.new.to_a }
end
# user system total real
# 9.600000 0.790000 10.390000 ( 10.639153)
# 0.020000 0.000000 0.020000 ( 0.137610)