Skip to content

Instantly share code, notes, and snippets.

{ scopeName = 'source.ruby.rails.aasm';
fileTypes = ( 'rb', 'rxml', 'builder' );
patterns = (
{ name = 'meta.rails.model';
comment = "Uses lookahead to match classes that (may) inherit from ActiveRecord::Base; includes 'source.ruby' to avoid infinite recursion";
begin = '(^\s*)(?=class\s+.+ActiveRecord::Base)';
end = '^\1(?=end)\b';
patterns = (
{ include = '$self'; },
{ include = 'source.ruby'; },
@rwilcox
rwilcox / create_fake_csv_files.py
Created June 28, 2010 04:34
Create CSV files with random data in them
#!/usr/bin/env python
# encoding: utf-8
"""
create_cvs_files.py
Create a number of fake CVS files. These files have an arbitrary length (from
5 lines to 600), and each have 6 columns.
Requires python-faker (http://github.com/threadsafelabs/python-faker)
@rwilcox
rwilcox / python_vs_ruby_datetime.markdown
Created July 6, 2010 20:46
Python date handling vs Ruby date handling

Intro

Tweet by rwilcox: Ok, someday I'm going to write that Ruby vs Python date/time handling comparison blog article.. (probably not in the foreseeable, but still)

This markdown starts the outline for a blog article I'm going to write. If you have any suggestions, fork this gist and add them. (or add comments to this gist!)

  • Basic usage: setting date object to Jan 3, 1979
  • Basic usage: setting datetime object to Jan 3, 1979, 5:04am
@rwilcox
rwilcox / nil_pattern_experiment.py
Created July 18, 2010 11:49
Python null handling pattern exploration
from __future__ import with_statement # not needed in 2.6 I think? WD-rpw 07-17-2010
# This file/gist explores nil handling in Python
#
# Hey look, there's a stackoverload question about the nill pattern in Python
# <http://stackoverflow.com/questions/2014105/null-pattern-in-python-underused>
#
# Where the vote seems to be that the exceptions are good (I agree),
# and the pattern should look like:
# if foo.bar is not None and foo.bar.baz:
@rwilcox
rwilcox / incoming_migration_check.rake
Created September 10, 2010 16:12
Keep migrations straight when you (a) have a lot of them or (b) use data_migration plugin
namespace :db do
namespace :migrate do
desc "Do you need to run db:migrate or db:data:migrate? `rake db:migrate:incoming_check'[SHA1 RANGE]'`"
task :incoming_check, :git_refs, :needs => :environment do |task, args|
# User story:
# as a developer, I should be able to see what migrations have changed
# in a series of commits -- what new ones and what old ones - so I can know
# what migration commands I need to run (rake db:migrate,
# rake db:data:migrate PATH=..., (if you're using the data_migration plugin!) or maybe just alert you that
# you should complain to your fellow devs because someone changed an old migration!)
# Requires Python 2.7 (class decorators and all)
#
# So my point in this is to be able to quickly generate behaviors for classes/features
# in software.
#
# I want to go from a line of
# @it("should ... ") statements, generated quickly in (OmniOutliner, some markdown
# document or whatever), and be able to wrap a Python TestCase around it quickly
#
# I also want to be able to extract the @it statements from the Python class
@rwilcox
rwilcox / installing_python_packages_programatically.py
Created December 26, 2010 17:32
How to use PIP to install Python packages programmatically.py
#!/usr/bin/env python
"""
PIP can stand for PIP Installs packages Programmatically, too!
(and here's how)
Requires:
* Python 2.6 or higher (for print-as-a-function)
* PIP 0.8.2
"""
@rwilcox
rwilcox / subclasses_of.rb
Created February 12, 2011 17:49
Returns all classes that have (a class) in their subclass/ancestor chain
class Base; end;
class Parent < Base; end
class Grandchild < Parent; end
def subclasses_of(a_class)
classes = []
ObjectSpace.each_object(Class) do |klass|
decendents = []
@rwilcox
rwilcox / what_did_you_do_today.rb
Created February 12, 2011 19:21
Metaprogramming I did today
# Question: can you tell what's going on here?
classes = subclasses_of(ActiveRecord::Base)
classes.each do |klass|
klass.class_eval do
before_save do |record|
    record.class.columns.each do |c|
      if ( (c.type == :datetime) && (record.send("#{c.name}_changed?")) && record.send("#{c.name}").present? )
        unmunged_datetime = record.send(c.name) + 5.hours
@rwilcox
rwilcox / remote send
Created March 10, 2011 01:19
For when I get back to my mac
# I do a lot of " I want this text over here, on this other machine."
# todo when I get back to my Mac: hook these into LaunchBar
alias dbcopy="cat > ~/Dropbox/.dbpasteboard"
alias dbpaste="cat ~/Dropbox/.dbpasteboard"