Skip to content

Instantly share code, notes, and snippets.

View timuruski's full-sized avatar
🏳️‍⚧️
Protect Trans Rights

Tim Uruski timuruski

🏳️‍⚧️
Protect Trans Rights
View GitHub Profile
@timuruski
timuruski / helpers.rb
Created July 15, 2011 18:25
A modular way to write helpers
module Authorization
module Service
class << self
def authorize(name)
case name
when 'timuruski', 'stevejobs' then "authorized"
else "unauthorized"
end
end
end
require 'mongo_mapper'
MongoMapper.database = 'tmp'
class BoolTest
include MongoMapper::Document
key :value, Boolean, :default => false
end
def test(value)
require 'mongo_mapper'
MongoMapper.database = 'tmp'
class HasValidAccessTokensValidator < ActiveModel::EachValidator
VALID_ACCESS_TOKENS = ['read', 'write', 'admin']
def validate_each(record, attribute, value)
unless value.all? { |token| VALID_ACCESS_TOKENS.include?(token) }
record.errors[attribute] << (options[:message] || "has invalid access_tokens")
names1 = File.readlines('names1.txt').map(&:chomp).map { |line| line.split(',')[-1].strip }
names2 = File.readlines('names2.txt').map(&:chomp).map { |line| line.split(',')[-1].strip }
p names1.map { |name| names2.include?(name) ? name : nil }.compact
p names2.map { |name| names1.include?(name) ? name : nil }.compact

Potato Salad with Black Beans and Chipotle Chiles

Chipotle chiles are dried, smoked jalapeños that are sold canned in adobo sauce, a vinegary tomato sauce. They can be found in the Mexican food section of most large grocery marts.

Ingredients

  • 2 lbs Red Bliss potatoes (about 6 medium or 18 small), scrubbed
  • 1/4 cup red wine vinegar
  • 1/2 cup mayonnaise
  • 1 medium celery rib, minced (about 1/2 cup)
@timuruski
timuruski / open_gemfile.rb
Created August 3, 2011 00:06
A TextMate command for opening your project's Gemfile
#!/usr/bin/env ruby
require 'pathname'
def find_gemfile(path)
path = Pathname.new(path).parent
gemfile_path = Pathname.new("Gemfile")
unless path.root?
path.entries.include?(gemfile_path) ? path + gemfile_path : find_gemfile(path)
end
end
@timuruski
timuruski / app_base.ru
Created August 4, 2011 15:55
Different ways to stack Rack middleware
require 'sinatra'
class AppBase < Sinatra::Base
def body
<<-EOS
<pre>
request_path: #{request.env['REQUEST_PATH']}
path_info: #{request.path_info}
</pre>
EOS
@timuruski
timuruski / sinatra-controllers.rb
Created August 8, 2011 15:37
An example of using plain Ruby classes as Controllers with Sinatra. No magic or rocket science required.
class App < Sinatra::Base
before "/hello" do
@controller = HelloController.new
end
get "/hello" do
@controller.index
end
end
@timuruski
timuruski / sinatra-controllers.rb
Created August 9, 2011 04:30
An example implementation of automatic controllers in Sinatra.
require 'sinatra'
require 'rack/test'
class FooController
def index
"Hello, foo!"
end
end
@timuruski
timuruski / rats.txt
Created August 10, 2011 21:33
Why are there rats in our code?
api.themoviedb.org $ ack rats
lib/personalization/service.rb
37: rats= nil
39: rats= PersRating.find_all_by_user_id(getRatingRequest.user_id)
41: if rats && (!getRatingRequest.entity_type.nil? || !getRatingRequest.entity_id.nil?)
42: newrats= []
43: rats.each do |rat|
49: newrats <<rat
52: rats= newrats
54: if rats