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
# 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
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
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
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) | |
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
module Lookup | |
module ClassMethods | |
#Any new "macros" go here | |
def lookup(lookup_name) | |
end | |
end | |
def self.included(host_class) | |
host_class.extend(ClassMethods) |
NewerOlder