Skip to content

Instantly share code, notes, and snippets.

View dasch's full-sized avatar
💭
LOOKING INTENTLY INTO THE VOID

Daniel Schierbeck dasch

💭
LOOKING INTENTLY INTO THE VOID
View GitHub Profile
@dasch
dasch / faster_helper.rb
Created January 25, 2012 12:18 — forked from dnagir/faster_helper.rb
The spec helper I use to run faster specs
ENV["RAILS_ENV"] ||= 'test'
cur_dir = File.expand_path(File.dirname(__FILE__) + '/..')
$LOAD_PATH << "#{cur_dir}"
if defined? Bundler
# Most likely going with the full env
require 'spec_helper'
else
$LOAD_PATH << "#{cur_dir}/app/models"
class PasswordChanger
def initialize(account)
@account = account
end
def change_password_for(user, old_password, new_password)
unless user.authenticated?(old_password)
raise PasswordIncorrect
end
def close
ticket_manager.solve_ticket(ticket)
rescue TicketManager::Unauthorized
flash[:error] = "You are not authorized to solve this ticket."
end
def create
@profile = ProfileManager.create(params[:profile])
rescue ProfileManager::CreationFailed => e
render :new, :errors => e.errors
end
class SpamMarker
def mark_comment_as_spam(comment)
author = comment.author
comment.mark_as_spam!
author.suspend!
end
end
@dasch
dasch / ticket_finder.rb
Created December 18, 2011 11:44
Composable Security
class TicketFinder
def initialize(account, user)
# These would be current_account and current_user from the perspective of
# the controller layer.
raise unless account.present? && user.account == account
@account = account
@user = user
end
class Issue < ActiveRecord::Base
# This is a very simplifed version of the old method.
def merge_issues(sources, options = {})
target = self
comment = options[:comment]
transaction do
sources.each do |source|
source.merge_into(target, :comment => comment)
end
@dasch
dasch / rack.rb
Created September 1, 2011 09:17
Re-thought Rack API
class Middleware
# Processes a request before it reaches the application.
#
# The middleware can change the flow of the request/response chain by:
# - Raising an exception, which will halt the process
# - Returning an instance of Response, which will go through the middleware layer
#
# Returning any other value will not do anything.
def process_request(request)
# ...
@dasch
dasch / benchmark.rb
Created August 29, 2011 10:33
Delegation Benchmarks
#
# A benchmark testing the performance of three methods of delegation:
#
# - Manually defining delegation methods
# - Metaprogramming using __send__
# - Metaprogramming without using __send__
#
# Note that the manual way of doing it is much faster if the arity of the
# target method is known. However, this benchmark is only meant to test
# the issue of __send__, and so we define the delegation methods with
@dasch
dasch / gist:1130665
Created August 7, 2011 19:09 — forked from dkubb/gist:1130086
case with predicates
require 'rubygems'
require 'backports' # aliases Proc#=== to Proc#call
rs = (0..10000).to_a.sample(30)
rs.each do |r|
case r
when lambda { |n| n.zero? } then puts "#{r} is zero"
when lambda { |n| (n % 5).zero? } then puts "#{r} is fiven"
when lambda { |n| (n % 4).zero? } then puts "#{r} is fourven"