Skip to content

Instantly share code, notes, and snippets.

View timruffles's full-sized avatar

Tim Ruffles timruffles

View GitHub Profile
@timruffles
timruffles / javascript_refactor.rb
Created January 7, 2011 11:35
refactor a huge 'functions' javascript file into separate files
current_class = nil
classes = open(ARGV[0]).read.split("\n").inject({}) do |classes, line|
if line =~ /^var (\w+) = function.*/
current_class = $1
elsif line =~ /^[\s\t]*$/
skip = true
end
(classes[current_class] ||= []) << line unless skip
classes
end
@timruffles
timruffles / history_stats.sh
Created January 10, 2011 21:58
get some stats on your past
history | awk '{$words[$2]++} END { for(w in words) print words[w], w }' | sort -k 1,2 -nr
#!/usr/bin/ruby
js_path = 'public/javascripts'
problems = `grep -rP "\}[\s\n]*,[\s\n]*\}" #{js_path}`
if problems.length > 1
puts "Woops, Internet Explorer HATES it when you use too many commas. Fix this or you'll be sad:"
puts problems
exit 1
else
exit 0
end
@timruffles
timruffles / dojo_events_only.js
Created February 10, 2011 18:24
Takes the sweet dojo event functions - dojo.connect, dojo.publish/subscribe, dojo.hitch - and pulls them out. Relies on a few underscore.js methods at the mo, could easily be removed
var dojo = d = {}
// this file courtesy of the TurboAjax Group, licensed under a Dojo CLA
dojo.hitch = function(/*Object*/scope, /*Function|String*/method /*,...*/){
// summary:
// Returns a function that will only ever execute in the a given scope.
// This allows for easy use of object member functions
// in callbacks and other places in which the "this" keyword may
// otherwise not reference the expected scope.
// Any number of default positional arguments may be passed as parameters
@timruffles
timruffles / require_js_boilerplater.rb
Created February 18, 2011 17:03
a little Textmate command to take a lot of typing out of require.js
#!/usr/bin/ruby
require 'pathname'
proj_dir = Pathname.new(ENV['TM_PROJECT_DIRECTORY'])
file_path = Pathname.new(ENV['TM_FILEPATH'])
path = file_path.relative_path_from(proj_dir).to_s
module_name = path.gsub(/\.js$/,'')
class_name = (module_name.split('/').last.capitalize.gsub(/_(\w)/) {|str| $1.upcase})
puts "define('#{module_name}',[],function() {
var #{class_name} = function() {
$1
@timruffles
timruffles / backbone_view_require_js.rb
Created February 18, 2011 17:09
textmate backbone view
#!/usr/bin/ruby
require 'pathname'
proj_dir = Pathname.new(ENV['TM_PROJECT_DIRECTORY'])
file_path = Pathname.new(ENV['TM_FILEPATH'])
path = file_path.relative_path_from(proj_dir).to_s
module_name = path.gsub(/\.js$/,'')
moddy = module_name.split('/').last
class_name = (moddy.capitalize.gsub(/_(\w)/) {|str| $1.upcase})
puts "define('#{module_name}',[],function() {
var #{class_name} = Backbone.View.extend({
@timruffles
timruffles / textmate.re
Created March 3, 2011 16:47
regex for textmate folder ignore
Files:
!(?:.*/\.(?!gitignore)|\.css$)
Folders:
!(?:\.|game-client)
@timruffles
timruffles / application.js
Created March 24, 2011 12:27
discussion of why pub/sub vs 'event' methods on objects is better at encapsulation
realGameSub: function(el, body) {
var oldPlayer = this.players[body.playerOff.id];
// TODO refactor this - why is the application making players for RealTeam?
var newPlayer = new Player({
'id': body.player.id,
'pos': oldPlayer.position,
'no': body.player.number,
'name': body.player.name,
'st': 'playing'
});
@timruffles
timruffles / aop_logging.rb
Created May 25, 2011 10:32
extreme aop logging idea
before:
def process_message message
logger.debug "Started #{message.headers["message-id"]}"
event = JSON.parse message.body
logger.debug "Action: #{event['action']}"
match_id = event['matchID']
active_entries = PassingGame::Entry.count_active_for match_id
logger.info "#{active_entries} active entries for match_id = #{match_id}"
if fail_on? event
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<h3 class="title">Picklive buzz</h3>
<script type="text/javascript">
var rotater = function(selector) {
var item, lastItem;