Skip to content

Instantly share code, notes, and snippets.

View laktek's full-sized avatar

Lakshan Perera laktek

View GitHub Profile
require 'singleton'
class State
attr_accessor :machine
STATES = {:ready => "Ready", :listen => "Listen", :processing => "Processing"}
def initialize(machine)
@machine = machine
end
@laktek
laktek / new_state_machine.rb
Created June 17, 2009 22:41
Vending Machine with multiple beverages (using State Pattern and a Singleton class)
require 'singleton'
class State
attr_accessor :machine
STATES = {:ready => "Ready", :listen => "Listen", :processing => "Processing"}
def initialize(machine)
@machine = machine
end
*sponsor.txt* For Vim version 7.1. Last change: 2007 Jan 05
VIM REFERENCE MANUAL by Bram Moolenaar
SPONSOR VIM DEVELOPMENT *sponsor*
Fixing bugs and adding new features takes a lot of time and effort. To show
# bootstrapper.rb
# Rails Bootstrapper using rails template
# Based on Extended Bort -http://github.com/laktek/extended-bort/tree/master
# Ideal to use when building a comprehensive web app
# Uses Braid to track remote repos.
# by Lakshan Perera
# Use Braid to track remote git or svn repos.
def braid(command, repo, options={})
other_params = ""
function parse_git_dirty {
git diff --quiet HEAD &>/dev/null
[[ $? == 1 ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}
export PS1='\u@\h \[\033[1;33m\]\w\[\033[0m\]$(parse_git_branch)$ '
@laktek
laktek / httpdump
Created April 4, 2009 01:53 — forked from peterc/httpdump
# Monitor HTTP requests being made from your machine with a one-liner..
# Replace "en1" below with your network interface's name (usually en0 or en1)
sudo tcpdump -i en1 -n -s 0 -w - | grep -a -o -E "Host\: .*|GET \/.*"
# OR.. to be able to use as "httpdump" from anywhere, drop this into ~/.bash_profile:
# (again replace "en1" with correct network interface name)
alias httpdump="sudo tcpdump -i en1 -n -s 0 -w - | grep -a -o -E "Host\: .*|GET \/.*""
# All the above tested only on OS X.
link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }}, { :class => "fine" })
#Currently renders:
<a class="fine" href="#" onclick="new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true}); return false;">Remote outauthor</a>
#suggested view render:
<a class="fine" data-rails-remote="true" href="http://www.example.com/whatnot">Remote outauthor</a>
#Method implemented by jquery.rails.js helper
$(a["data-rails-remote=true"]).($.ajax({
link_to_function("Show me more", nil, :id => "more_link") do |page|
page[:details].visual_effect :toggle_blind
page[:more_link].replace_html "Show me less"
end
#without :ujs option
link_to_function("Greeting", "alert('Hello world!')")
# <a href="#" onclick="alert('Hello world!'); return false;">Greeting</a>
#with :ujs and :id option
link_to_function("Greeting", "alert('Hello world!')", :id => "greeting_link", :ujs => true)
# <a href="#" id="greeting_link">Greeting</a>
# <script type="text/javascript">
# //assuming users JS framework of choice is jQuery
# $(\"#greeting_link\").click( function(){ alert('Hello world!'); return false } );
@laktek
laktek / gist:72105
Created February 28, 2009 21:01
has_many relationships
class Student < ActiveRecord::Base
has_many :rating
has_many :books, :through => :ratings
end
class Book < ActiveRecord::Base
has_many :ratings
has_many :students, :through => :ratings
end