Skip to content

Instantly share code, notes, and snippets.

@gregnavis
gregnavis / routes.rb
Created Jan 15, 2019
An example how to protect user-uploaded data with a password in a Rails app. This example uses a custom password page.
View routes.rb
Rails.application.routes.draw do
resources :uploads, only: :show do
member do
post :download
end
end
end
@gregnavis
gregnavis / routes.rb
Last active Jan 15, 2019
An example how to protect user-uploaded data with a password in a Rails app. This example uses HTTP Basic authentication.
View routes.rb
Rails.application.routes.draw do
resources :uploads, only: :show
end
@gregnavis
gregnavis / fork.rb
Created Oct 29, 2018
This is a code snippet I used to delegate execution of test cases to forked processes.
View fork.rb
class ActiveSupport::TestCase
def run
read_io, write_io = IO.pipe
read_io.binmode
write_io.binmode
if fork
# Parent: load the result sent from the child
write_io.close
View extended_formatter.rb
class Logging::ExtendedFormatter
def initialize(class_name, formatter)
@class_name = class_name
@formatter = formatter
end
def call(severity, datetime, progname, msg)
@formatter.call(severity, datetime, progname(progname), msg(msg))
end
@gregnavis
gregnavis / markdown.py
Created May 30, 2015
A simple Markdown-like parser
View markdown.py
'''
>>> parse_markdown('This is a paragraph.')
Markdown([Paragraph([Text('This is a paragraph.')])])
>>> parse_markdown('The first paragraph.\\n\\nThe second paragraph.')
Markdown([Paragraph([Text('The first paragraph.')]), Paragraph([Text('The second paragraph.')])])
>>> parse_markdown('Emphasis *is* supported.')
Markdown([Paragraph([Text('Emphasis '), Emphasis('is'), Text(' supported.')])])
@gregnavis
gregnavis / super_method_proxy.rb
Created May 29, 2015
SuperMethodProxy calls the method of the parent class and allows skipping a method prepended by a module.
View super_method_proxy.rb
module Authentication
def login
puts 'Authentication#login'
super
end
end
class User
prepend Authentication
@gregnavis
gregnavis / 20150521093555_create_one_time_switches.rb
Created May 21, 2015
Ensuring that something happens at most once
View 20150521093555_create_one_time_switches.rb
class CreateOneTimeSwitches < ActiveRecord::Migration
def change
create_table :one_time_switches do |t|
t.string :name, null: false
t.timestamp :created_at, null: false
t.index :name, unique: true
end
end
end
@gregnavis
gregnavis / insert_query.rb
Created Apr 15, 2015
An InsertQuery object that can be used to insert models in bulk.
View insert_query.rb
class InsertQuery
def initialize(model_class, column_names, returning = nil)
@model_class = model_class
@column_names = column_names
@returning = returning
@rows = []
@executed = false
end
@gregnavis
gregnavis / polynomial.rb
Last active May 13, 2019
Polynomial Manipulation in Ruby
View polynomial.rb
class Polynomial
# maps e.g. {x: 1, y: 2} => 3 which corresponds to term 3 * x ** 1 * y ** 2
attr_reader :coefficients
def initialize(coefficients = {})
puts @coefficients.inspect
@coefficients = coefficients.reject do |_index, coefficient|
coefficient == 0
end
end
@gregnavis
gregnavis / call_tracer.py
Created Feb 26, 2014
This is an example of an automatic call tracer that can report the whole call tree. I use it instead of adding lots of logging statements.
View call_tracer.py
import bdb
# This is a custom debugger that calls the specified handler on each function
# call.
class _CallTracerBdb(bdb.Bdb):
def __init__(self, call_handler, return_handler):
bdb.Bdb.__init__(self)
self._call_handler = call_handler
self._return_handler = return_handler
You can’t perform that action at this time.