Skip to content

Instantly share code, notes, and snippets.

View ngty's full-sized avatar

Ng Tze Yang ngty

  • RankAbove Ltd
  • Singapore
View GitHub Profile
module RailsBestPractices
module Macros
def self.included(base)
# Calling of "include RailsBestPractices::Macros" will make all macros available as
# class methods within describe { ... }
base.extend(ClassMethods)
end
module ClassMethods
# instead of Time.now (or watever class methods of Time), we use:
LocalTime.now
class Someone
class << self
def say
$stdin.each_line do |line|
$stdout << "someone says '#{line}' @ #{Time.now}"
end
end
end
end
# Usages:
#
# 1. Obtain benchmark results by repeating :spec by running the default number (5) of times
# $ rake benchmark[spec]
#
# 2. Obtain benchmark results by repeating :spec by running it 2 times
# $ rake benchmark[spec,2]
#
task :benchmark, :task, :times do |t, args|
times, task = (args.times || 5).to_i.method(:times), args.task
class TweetObserver < ActiveRecord::Observer
observe :post, :implementation, :question
def after_create(model)
tweet(model.tweet_title, model.tweet_path)
end
private
def tweet(title, path)
@ngty
ngty / rvm_error.dump
Created October 16, 2010 01:44
Need extra stripping of trailing double-quote char to prevent yaml-related error
/home/ty.archlinux/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `load': syntax error on line 23, col -1: ` ' :cd': "\E[J:ce=\E[K:cl=\E[H\E[J:cm=\E[%i%d;%dH:ct=\E[3g:\" (ArgumentError)
' :do': "^J:nd=\E[C:pt:rc=\E8:rs=\Ec:sc=\E7:st=\EH:up=\EM:\"
' :le': "^H:bl=^G:cr=^M:it#8:ho=\E[H:nw=\EE:ta=^I:is=\E)0:\"
' :li#33:co#127:am:xn:xv:LP:sr': "\EM:al=\E[L:AL=\E[%dL:\"
' :cs': "\E[%i%d;%dr:dl=\E[M:DL=\E[%dM:im=\E[4h:ei=\E[4l:mi:\"
' :IC': "\E[%d@:ks=\E[?1h\E=:ke=\E[?1l\E>:vi=\E[?25l:\"
' :ve': "\E[34h\E[?25h:vs=\E[34l:ti=\E[?1049h:te=\E[?1049l:\"
' :us': "\E[4m:ue=\E[24m:so=\E[3m:se=\E[23m:mb=\E[5m:\"
' :md': "\E[1m:mr=\E[7m:me=\E[m:ms:\"
' :Co#8:pa#64:AF': "\E[3%dm:AB=\E[4%dm:op=\E[39;49m:AX:\"
if Rails.env.test?
ActionDispatch::Callbacks.before do
CrossStub.refresh :file => Rails.root.join('tmp', 'crossstub.cache')
end
end
@ngty
ngty / showoff-scraper.rb
Created June 4, 2011 17:49
Showoff's (https://github.com/schacon/showoff) builtin pdf conversion doesn't work well for me, so i hacked up my own version ... & it works !!
#!/usr/bin/env ruby
require 'rubygems'
require 'RMagick'
require 'capybara'
require 'capybara/dsl'
# ================================================================
# Collect input args
# ================================================================
begin
# features/support/cucumber_fuubar_patch.rb
module Cucumber::Cli::Configuration::Patch
def self.included(base)
base.class_eval do
alias_method :_orig_formatter_class, :formatter_class
def formatter_class(format)
if format == 'Cucumber::Formatter::Fuubar'
require 'cucumber/formatter/fuubar'
@ngty
ngty / gist:1229514
Created September 20, 2011 16:04
Module#attr_accessor & friends
# Have u ever found urself doing this:
class Thing
def size=(size)
@size = size
end
def size
@size
end
end