Skip to content

Instantly share code, notes, and snippets.

@DGaffney
DGaffney / Drying up Tests
Created January 20, 2011 20:38
This is done in various files (click_tag_test,favorite_tag_test,etc...) and could be more dry....
#Basically, across five different files, we have:
#Model here is one of these: [Click,Favorite,Friendship,Reply,Retweet]
test "create should record model tags" do
ad, user = ads(:sample), users(:sample)
assert_kind_of Fitness, ALGORITHM.calculate_fitness_for(ad, user)
assert some_crap
assert_difference('ModelTag.count'){ Model.create!(models(:sample).attributes) }
end
class AlgorithmManager
def self.return_chosen_algorithm
return AlgorithmManager::Algorithm
end
def self.post_run_procedures(function, *variables)
begin
if variables
self.send(function, variables)
else
self.send(function)
##lib/targeting_algorithm_manager.rb
class TargetingAlgorithmManager
def self.method_missing(name, *args, &blk)
debugger
callbacks = ["before_validation","after_validation","before_save","before_create","after_create","after_save"]
allowed_prefixes = callbacks#&other_prefixes&others&still_more
if name.to_s.partially_included_in?(callbacks)
self.send("#{name.to_s}_#{args[0].class.to_s.underscore}", args[0])
elsif !name.to_s.partially_included_in?(allowed_prefixes)
ruby-1.8.7-p330 :001 > include Geokit::Geocoders
=> Object
ruby-1.8.7-p330 :002 > gg = "Antwerpen, Vlaams Gewest, Belgie"
=> "Antwerpen, Vlaams Gewest, Belgie"
ruby-1.8.7-p330 :003 > geocode = GoogleGeocoder.geocode(gg)
=> #<Geokit::GeoLoc:0x1052f8a40 @province="Antwerp", @street_address=nil, @precision="city", @country_code="BE", @country="Belgium", @all=[#<Geokit::GeoLoc:0x1052f8a40 ...>], @lng=4.39625, @full_address="Antwerp, Belgium", @state="Flemish Region", @suggested_bounds=#<Geokit::Bounds:0x1051e15d0 @ne=#<Geokit::LatLng:0x1051e1440 @lng=4.6523688, @lat=51.3316021>, @sw=#<Geokit::LatLng:0x1051e1508 @lng=4.1401312, @lat=51.1079663>>, @zip=nil, @accuracy=4, @lat=51.21992, @success=true, @provider="google", @city="Antwerp">
ruby-1.8.7-p330 :005 > {}.class
=> Hash
ruby-1.8.7-p330 :006 > hash = {}
=> {}
ruby-1.8.7-p330 :007 > hash["blah"] = 1
=> 1
ruby-1.8.7-p330 :008 > hash
=> {"blah"=>1}
ruby-1.8.7-p330 :009 > hash[:blah]
=> nil
def clean_values(elem)
if elem.empty?
elem = nil
elsif elem.starts_with?("\"") or elem.starts_with?("'")
elem = elem.gsub(/[\'|\"]/, "")
elsif elem.starts_with?("{")
new_val = {}
elem.gsub(/[\{|\}]/,"").split(/\=\>(.*)\,/).collect{|x| x.split("=>")}.collect{|x| new_val[x[0].gsub(/[\'|\"|:]/,"").strip.to_sym] = x[1]}
elem = new_val
elsif elem.starts_with?("[")
class CreateCountries < ActiveRecord::Migration
def self.up
create_table :countries do |t|
t.column :code, :string, :size => 2
t.column :name, :string, :size => 80
end
add_index :countries, :code
Country.reset_column_information
Country.create(:code => 'AF', :name => 'Afghanistan')
users = ["alefterra", "cultura45", "_a_rene", "donnawomack", "sylvaingranier", "labebaax3", "oti_gsxr750", "cuffedIICHUCKS_", "Tie_wasHere", "DaKidGirbaud", "Luismaquilarque", "mochiko406", "shurroxxx", "JOKKER1911",
"dlishdelicia420", "10minutenmail", "Its_KamBro", "_SimplyKristen", "BadAssLula", "ShySoFierce", "mich_fs", "UnderTyr", "alice_holdstock", "chklug", "jschlozman", "Lara_S23", "SnarkySookie", "Ahmedd3027",
"justReVay", "JavierRivmor", "PushStartMktg", "cjvibes", "theblackjoker1", "stropi_", "iioh3", "BertMccrakenfk", "el_ferno_10", "pussytoogood", "jasmin_327", "RCSFsAaa", "elmackio", "CourtStreetCard",
"furiousmidwife", "ncarr18", "Muhamed91", "n9200", "JulezMia", "StephBosten", "liquidstone321", "barbiellinas", "ShanRKurd7", "maoscolorida", "jevihax_OUT", "ambersworld619", "kayrenwelch", "JG_Fashionigger",
"makeupnbeautyy", "MsFancyLola", "TheLifeOf_Quis", "RJ_Dasilva", "fgcarg", "iAM_Arielle", "FT9Venezuela", "red_dread52", "Paipyy", "OnlyTrisha", "FazerLove_xx",
* executing `production'
triggering start callbacks for `deploy'
* executing `multistage:ensure'
triggering after callbacks for `multistage:ensure'
* executing `set_branch'
* executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
triggering before callbacks for `deploy:update_code'
def blah
result = yield 10
puts result==10
end
blah do |number|
number+1
end