Skip to content

Instantly share code, notes, and snippets.

@Nimster
Nimster / gist:1129084
Created August 6, 2011 06:37
Rails lookup v1 - Rails macro setup
module Lookup
module ClassMethods
#Any new "macros" go here
def lookup(lookup_name)
end
end
def self.included(host_class)
host_class.extend(ClassMethods)
@Nimster
Nimster / gist:1129113
Created August 6, 2011 07:04
Rails lookup v5 - Migrations to create lookup tables
class CreateCarTypeLookupForCar < ActiveRecord::Migration
def self.up
create_table :car_types do |t|
t.string :name
t.timestamps #Btw you can remove these, I don't much like them in type tables anyway
end
remove_column :cars, :type #Let's assume you have one of those now…
add_column :cars, :type, :integer #Maybe put not_null constraints here.
end
@Nimster
Nimster / gist:1129091
Created August 6, 2011 06:46
Rails lookup v2 - Create the lookup type
def lookup(as_name)
mycls = self #Class I'm defined in
#We now define the CarType class, as if we were in a file car_type.rb
cls = Class.new(ActiveRecord::Base) do #Define a new class, extending AR::Base
#CarType should have the has_many :cars link
has_many mycls.name.tableize.to_sym
#These are optional. You can define any additional constraints you like.
validates_uniqueness_of :name
@Nimster
Nimster / gist:1129101
Created August 6, 2011 06:56
Rails lookup v3 - String accessors for the lookup type
def lookup(as_name)
#...
#Now, define the foreign key from Car to CarType.
belongs_to lookup_name.to_s.to_sym, :foreign_key => "#{as_name}".to_sym
validates "#{as_name.to_s}_id".to_sym, :presence => true
#Now we define the "delegates" that will allow us to just set call car.car_type = "Sports"
#Define a setter for car_type
define_method("#{as_name.to_s}_id=") do |id|
@Nimster
Nimster / gist:1129105
Created August 6, 2011 06:59
Rails lookup v4 - Prefill the caches
def lookup(as_name)
#...
all_vals = cls.all
cls.class_variable_set(:@@rcaches, all_vals.inject({}) do |r, obj|
r[obj.name] = obj.id
r
end)
cls.class_variable_set(:@@caches, all_vals.inject([]) do |r, obj|
r[obj.id] = obj.name
@Nimster
Nimster / Rakefile
Created September 16, 2011 20:34 — forked from jamiecobbett/Rakefile
Run a rake task for each rakefile in directories under the current one
# Adapted from http://stackoverflow.com/questions/1686779/multifile-rake-build
# Runs a task (in this case the default task) for each Rakefile nested in the current directory
task :default do
FileList["*/**/Rakefile"].each do |project|
next if project =~ /^admin_console/
next if project =~ /^logging/
# clear current tasks
Rake::Task.clear
#load tasks from this project
load project
@Nimster
Nimster / gist:2054777
Created March 17, 2012 03:46
Important piece of functional programming and allocation of objects
# Often when doing functional style programming you get concerned that copying or
# wrapping objects might cost a lot in terms of performance. But the great thing
# about an immutable object is that I don't need to create a new one if I am
# handed an immutable copy.
# Follow me on twitter @nimrodpriell or see my blog at http://www.educated-guess.com
# for more bits of knowledge
# This is what we want to figure out the performance of. It is equivalent to calling
# frozenset(frozenset(frozenset(... m times (l)))...))
def frozens(l,m=100):
@Nimster
Nimster / gist:2156727
Created March 22, 2012 06:49
How to compile GraphViz 2.8.2 to get the python bindings on Mac OS X Lion 10.7.3 with Xcode 4.3.1 from source
./configure --enable-python27=yes --with-png --enable-ltdl-install --with-included-ltdl --enable-perl=no CC="gcc -arch x86_64" CXX="g++ -arch x86_64"
make
sudo make install
@Nimster
Nimster / python_hints.py
Created April 8, 2012 03:57
Quick reference to some common python idioms/capabilities - mainly to serve as a reminder for me
collections.defaultdict(lambda: 0) # Hash map with default values
l = [t for t in l if not t in s] # Given a list l and a set s, returns l without all the elements in s
fun_with_many_args(*[1, 2, 3]) # Splat an array into an argument list
fun_with_named_args(**{'arg1': 1, 'arg2': 2}) # Splat a map (dictionary) for named argument functions!
@Nimster
Nimster / gist:2338235
Created April 8, 2012 16:18
Rails summary
COMMANDS
rails new demo #Create a new rails project under 'demo'
rails server #Start a local server to test the project, port 3000
rails generate controller ControllerName action1 action2
# For example 'rails generate controller say hello goodbye' gives
# say/hello and say/goodbye links, which are implemented by a class
# app/controllers/say_controller.rb with hello and goodbye methods.