Skip to content

Instantly share code, notes, and snippets.

View unders's full-sized avatar

Anders Törnqvist unders

  • Functionbox
  • Göteborg, Sweden
View GitHub Profile
@unders
unders / default.rb
Created October 18, 2010 14:49 — forked from ezmobius/default.rb
#
# Cookbook Name:: delayed_job
# Recipe:: default
#
if ['solo', 'app', 'app_master'].include?(node[:instance_role])
# be sure to replace "app_name" with the name of your application.
run_for_app("maloca") do |app_name, data|
@unders
unders / god.rb
Created October 18, 2010 14:53 — forked from defunkt/god.rb
rails_root = "/data/github/current"
20.times do |num|
God.watch do |w|
w.name = "dj-#{num}"
w.group = 'dj'
w.interval = 30.seconds
w.start = "rake -f #{rails_root}/Rakefile production jobs:work"
w.uid = 'git'
@unders
unders / Presenter.rb
Created November 26, 2010 15:05
Presenter that works with Rails
#app/presenters/article_presenter
module ArticlePresenter
def fancy_name
end
end
#app/presenters/comment_presenter
module CommentPresenter
def fancy_name
end
@milandobrota
milandobrota / monty_hall.rb
Created December 19, 2010 18:24
Monty Hall Problem simulation written in Ruby. Usage: ruby monty_hall.rb <number of games>
class Show
attr_reader :games
def initialize
@games = []
end
def first_pick(door)
@current_game = Game.new(door)
@current_game.first_pick(door)
@jimweirich
jimweirich / exceptions.rb
Created March 7, 2011 02:55
How to organize exceptions in your library
# Suppose you were designing a library named "Zippo".
# How would you organize the exceptions thrown by the library.
# Option 1
# All library errors inheriting from a common base
module Zippo
class ZippoError < StandardError; end
class ZippoArgumentError < ZippoError; end
class ZippoTypeError < ZippoError; end
@cdcarter
cdcarter / form_helper.rb
Created June 4, 2011 02:52
NS Form Helper Class
class FormHelper
attr_accessor :form
def initialize(nsform=nil)
@form = nsform
end
def length
form.numberOfRows
end
@nevans
nevans / method_vs_constant_lookup.rb
Created August 19, 2011 15:50
demonstrating the difference between method and constant lookup
#!/usr/bin/env ruby
module TopLevel
CONST_D = :d_from_top_level
module MiddleLevel
class BottomLevel
CONST_A = :foo
CONST_B = :bar
CONST_C = :c_from_bottom_level
@reagent
reagent / credential.rb
Created September 28, 2011 19:01
Sample code from my Using OO to Manage Control Flow post
class Credential
include ActiveModel::Validations
attr_accessor :screen_name, :oauth_token, :oauth_secret, :token, :description
validates_presence_of :screen_name, :oauth_token, :oauth_secret, :message => 'required'
validate :user_exists, :unless => :errors?
def initialize(attributes = {})
attributes.each {|k, v| set_recognized_attribute(k, v) }
###
# This is a JSON building library. It allows you to build data structures
# to dump as JSON. Here is a sample of how to use it:
#
# def person_hash(person)
# {
# 'name' => person.name,
# 'age' => person.age,
# 'friends' => person.friends.map { |x| person_hash x }
# }
@unders
unders / gist:1427613
Created December 3, 2011 17:17 — forked from dhh/gist:1014971
Use concerns to keep your models manageable
# autoload concerns
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)
end
end