Skip to content

Instantly share code, notes, and snippets.

@miketierney
Created January 14, 2010 18:47
Show Gist options
  • Save miketierney/277386 to your computer and use it in GitHub Desktop.
Save miketierney/277386 to your computer and use it in GitHub Desktop.

Snippets

This repo is just a collection of snippets that I've wanted to hang on to. Not unlike gists, but more for my own not-keeping purposes. Hopefully someone else will find these as useful as I do. If you have any questions, you can email me at [email protected]. Thanks!

-Mike

WARNING

As these are nothing more than snippets, I really make no promise of extensive documentation. Where I can, I'll make notes, but I can't promise that they'll mean much to anyone other than me. You're more than welcome to ask me to clarify, but please keep in mind that initial caveat.

ruby -I.:lib:test test/test_csscrubber.rb | unit_diff -u
# run this from the command line with ruby csspool_test.rb filename.css and it will ouptut all of the selectors in a css file
require 'rubygems'
require 'csspool'
class DH < CSS::SAC::DocumentHandler
def start_selector(selectors)
puts selectors.map { |x| x.to_css }
end
end
parser = CSS::SAC::Parser.new(DH.new)
parser.parse(File.read(ARGV[0]))
git svn clone http://svn/project/trunk project_trunk --username=miket # results in refs/remotes/git-svn, which seems to be the norm, but I don't like.
git svn init http://svn/project project_dir -s --username=miket
git svn clone http://svn/project project_dir -s --username=miket
-------
git svn init http://svn/project project_dir -s --username=miket
git svn fetch --all -r 1000
git svn show-ignore >> .git/info/exclude
git svn init http://svn/project project_dir -s --username=miket
<!-- after including both the prototype.js and jquery.js files... -->
<script type="text/javascript">
jQuery.noConflict();
var $j = jQuery;
</script>
MyModel.find(:all, :conditions => "ISNULL(`something_that_is_empty`) AND CHARACTER_LENGTH(`something_with_a_length`) > 5")
require 'nokogiri'
doc = Nokogiri::HTML(open('doc.html'))
selectors.each do |selector|
if doc.search(selector).empty?
@unused_selectors[selector] += 1
end
end
# Works for non-edge case CSS files... it's ugly, but it'll get the bulk of the selectors. It'll also grab font names and a few more things that you probbaly don't want either. Use a SAC parser instead.
/([\.|^\#|^|\s]([\.a-zA-Z0-9_-]+)\s?){1,}[\,|\{]/
# Create a tmp directory for use in ... well, whenever you'd need a tmp directory. Mostly useful for testing, but there are other uses too.
>> require 'tmpdir'
#=> true
>> Dir::tmpdir
#=> "/var/folders/22/22A2X2I6HcmsssIW7SdM7++++TI/-Tmp-"
#######################################################
#
# DISCLAIMER: This is not my code. It's written by
# David Berube, and is an example from his book
# 'Practical Ruby Gems' (Ch. 7).
#
# I'm keepiing it here because it checks to see if a
# database exists, and if it doesn't it creates one.
#
# This code requires the Campfire gem to run.
#
#######################################################
#!/usr/bin/ruby
%w(rubygems camping ).each { |lib| require lib }
# Populate our namespace with Camping functionality.
Camping.goes :TrackTime
#
# Contains the application's single model, ClientTime.
#
module TrackTime::Models
#Sets or retrieves the schema.
def self.schema(&block)
@@schema = block if block_given?
@@schema
end
#
# The single model for this application.
# It inherits from ActiveRecord,
# so you can use it like any Rails
# model.
#
class ClientTime < Base
# Returns the difference between the starting
# and stopping times - if the entry hasn't been
# stopped yet, it will return the time elapsed
# since it was started.
def elapsed
diff=((stop || Time.now) - start)
format("%0.2f",(diff/3600))
end
end
end
#
# Sets the schema, defining our single table.
#
TrackTime::Models.schema do
create_table :tracktime_client_times, :force => true do |t|
t.column :client, :string, :limit => 255
t.column :start, :datetime
t.column :stop, :datetime
t.column :created_at, :datetime
end
end
#
# Get ready to run by creating the database
# if it doesn't already exist.
#
def TrackTime.create
unless TrackTime::Models::ClientTime.table_exists?
ActiveRecord::Schema.define(&TrackTime::Models.schema)
TrackTime::Models::ClientTime.reset_column_information
end
end
#
# Contains all of the controllers for the application.
#
module TrackTime::Controllers
#
# Homepage for the application.
#
class Index < R '/'
def get
@times=ClientTime.find_all
render :homepage
end
end
#
# Controller which creates a new timer.
#
class Start < R('/start/')
def get
@text='Started!'
new_time=ClientTime.create :client=>@input[:client], :start=>Time.now
render :statictext
end
end
#
# Controller for stopping a timer.
#
class Stop < R('/stop/(\w+)')
def get(id)
@text='Stopped!'
old_time=ClientTime.find id
if !old_time
@text="failed on stopping time # #{id}"
else
old_time.update_attributes :stop=>Time.now
end
render :statictext
end
end
#
# Deletes a timer.
#
class Kill < R('/kill/(\w+)')
def get(id)
@text='Killed!'
deleted_successfully=ClientTime.delete id
@text="failed on killing time # #{id}" unless deleted_successfully
render :statictext
end
end
end
#
# Contains all of the views for the application.
#
module TrackTime::Views
TIME_FORMAT="%H:%M:%S"
#
# View which statically shows a message with a
# redirect back to the homepage.
#
def statictext
h1 { a @text, :href=>R(Index), :style=>ítext-align:centerí}
end
#
# View which shows the homepage.
#
def homepage
div do
table :cellpadding=>5, :cellspacing=>0 do
tr do
th :colspan=>6 do
form :action=> R(Start) do
p do
strong 'start timer: '
br
label 'client name'
input :name=>'client', :type=>'text', :size=>'5'
input :type=>'submit', :value=>'start'
end
end
end
end
tr do
th 'Client'
th 'Start'
th 'Stop'
th 'Elapsed'
end
(@times || []).each do |time|
tr :style =>"background-color: #{(time.stop ? 'white' : '#FFEEEE')}" do
td time.client
td time.start.strftime(TIME_FORMAT)
if time.stop
td time.stop.strftime(TIME_FORMAT)
else
td {a 'Stop now', :href=>R(Stop,time.id) }
end
unless !time.start
td "#{time.elapsed} hrs"
end
td {a 'kill', :href=>R(Kill, time.id)}
end
end
end
end
end
#
# Layout which controls the appearance
# of all other views.
#
def layout
html do
head do
title 'TrackTime'
end
body do
h1 "welcome to tracktime"
div.content do
self << yield
end
end
end
end
end
@root = "#{RAILS_ROOT}/app/views/"
files = []
Find.find @root do |path|
unless File.directory? path # don't want the directories in our array, since they're really not all that useful
unless File.dirname(path) == "#{@root}layouts" # We don't want any of the files in the layouts directory, since that's not what we're testing
path.gsub!(@root,"")
files << path unless File.basename(path) =~ /^[_]/ # anything start with an underscore is a partial, so we don't want them either.
end
end
end
files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment