This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Lookup | |
module ClassMethods | |
#Any new "macros" go here | |
def lookup(lookup_name) | |
end | |
end | |
def self.included(host_class) | |
host_class.extend(ClassMethods) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
OlderNewer