Skip to content

Instantly share code, notes, and snippets.

View softcraft-development's full-sized avatar

Craig Walker softcraft-development

View GitHub Profile
@softcraft-development
softcraft-development / hash_evoke.rb
Created June 13, 2010 23:45
Retrieve a value from a Hash. If the key is not in the hash, return the given default value and save it to the hash
class Hash
# Pass either a default value or a block to return it (a la Hash#fetch()).
# If both are passed, the block will take precedence over the default value
def evoke(key, default = nil)
if include?(key)
self[key]
else
self[key] = block_given? ? yield : default
end
end
# Out of the box, Ruby's Rational class doesn't know how to multiply or divide a BigDecimal;
# You get: "TypeError: Rational can't be coerced into BigDecimal" when you try.
class Rational
alias :"old_/" :"/"
def /(a)
begin
# Keep existing behavior if we can
send(:"old_/", a)
rescue TypeError
# Goal: Allow addition of instances to a collection in a factory-built object
# when those instances require references to the parent.
# Typically occurs in Rails when one model has_many instances of another
# See more at:
# http://stackoverflow.com/questions/2937326/populating-an-association-with-children-in-factory-girl
class Factory
def has_many(collection)
# after_build is where you add instances to the factory-built collection.
def test_update_nested
ingredient = Factory.create(:ingredient)
put :update,
:id=>ingredient.recipe.friendly_id,
:recipe=> {
:name=> ingredient.recipe.name,
:ingredients_attributes=>{
"0"=>{
:name=>ingredient.name,
:amount=>ingredient.amount,
{"commit"=>"Update Recipe",
"authenticity_token"=>"7P84gn9DtSosfxahaIan6ZH/zoExgYEbtgmGHLnK18c=",
"_method"=>"put",
"id"=>"test",
"recipe"=>{"name"=>"test",
"ingredients_attributes"=>{"0"=>{"name"=>"test",
"amount"=>"1",
"id"=>"13",
"_destroy"=>"1"}}}}
@softcraft-development
softcraft-development / restful_routing.asax.cs
Created January 18, 2010 21:52
Two RESTful routes for global.asax in ASP.NET MVC
public static void RegisterRoutes(RouteCollection routes)
{
// Matches /Controller/ModelID (with an optional /Action)
routes.MapRoute(
"Details",
"{controller}/{id}/{action}",
new { action = "Details" }
);
// Matches /Controller; typically this represents a list of all models for a particular controller.
@softcraft-development
softcraft-development / find_synonym_remote_targets.sql
Created January 8, 2010 18:52
Show the remote server data source for synonyms targeting linked servers in SQL Server 2005
select
sk.name as LocalSchemaName,
sn.name as SynonymName,
sn.base_object_name as FullTargetName,
substring( sn.base_object_name, 2, charindex(']', sn.base_object_name) - 2) as LinkedServerName,
sv.data_source as RemoteServerName,
substring(
sn.base_object_name,
charindex('[', sn.base_object_name, 2) + 1,
charindex(
@softcraft-development
softcraft-development / numeric_extensions.rb
Created January 6, 2010 06:30
Constrain a value between two values
class Numeric
def between( *values )
low = values.min
high = values.max
[[low,self].max, high].min
end
end
require "test/unit"
require "shoulda"
@softcraft-development
softcraft-development / hash_extensions.rb
Created January 3, 2010 00:04
Add dynamic defaults to a Hash
require 'rubygems'
require 'shoulda'
class Hash
def with_defaults( default_blocks )
new_hash = Hash.new do |hash, key|
value = default_blocks[key]
if value.respond_to? :call
value = value.call
end
@softcraft-development
softcraft-development / concatenate_non_nil_strings.rb
Created December 19, 2009 21:44
Concatenate two strings if not nil
class String
def &(value = nil, default = "")
value ? self + value.to_s : default.to_s
end
end
# Mostly for consistency; not terribly useful
class NilClass
def &(value = nil, default = "")
default.to_s