Skip to content

Instantly share code, notes, and snippets.

View lucashungaro's full-sized avatar

Lucas Húngaro lucashungaro

View GitHub Profile
@lucashungaro
lucashungaro / gist:3001503
Created June 27, 2012 04:40
DIP snippet 1
class Game < ActiveRecord::Base
belongs_to :category
validates_presence_of :title, :category_id, :description,
:price, :platform, :year
end
 
class GamePriceService
attr_accessor :game
 
# we could use a config file
@lucashungaro
lucashungaro / gist:2946258
Last active October 6, 2015 05:48
Installing ruby 1.9.3 from source on OS X Lion
Install XCode *and* Command Line Tools (just CLT won't be enough, readline won't work =[)
sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2
sudo ln -s /usr/bin/g++ /usr/bin/g++-4.2
brew install wget openssl readline libyaml curl libxslt
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p392.tar.gz
tar xzvf ruby-1.9.3-p392.tar.gz
cd ruby-1.9.3-p392
# Expand print panel by default
defaults write NSGlobalDomain PMPrintingExpandedStateForPrint -bool true
# Disable the “Are you sure you want to open this application?” dialog
defaults write com.apple.LaunchServices LSQuarantine -bool false
# Enable subpixel font rendering on non-Apple LCDs
defaults write NSGlobalDomain AppleFontSmoothing -int 2
# Disable press-and-hold for keys in favor of key repeat
@lucashungaro
lucashungaro / gist:2690990
Created May 14, 2012 00:51
SRP snippet 3
class Game < ActiveRecord::Base
belongs_to :category
validates_presence_of :title, :category_id, :description,
:price, :platform, :year
def price
price = read_attribute(:price)
unless price.present?
update_attribute(:price, GamePriceService.new(self).get_price)
read_attribute(:price)
@lucashungaro
lucashungaro / gist:2690982
Created May 14, 2012 00:51
SRP snippet 2
class Game < ActiveRecord::Base
belongs_to :category
validates_presence_of :title, :category_id, :description,
:price, :platform, :year
end
class GamePriceService
attr_accessor :game
# we could use a config file
@lucashungaro
lucashungaro / gist:2690968
Created May 14, 2012 00:49
SRP snippet 1
class Game < ActiveRecord::Base
belongs_to :category
validates_presence_of :title, :category_id, :description,
:price, :platform, :year
def get_official_price
open("http://thegamedatabase.com/api/game/#{name}/price?api_key=ek2o1je")
end
def print

"Why You Don't Get Mock Objects" by Gregory Moeck

RubyConf 2011 | 2011-09-29 | Gregory Moeck (@gregmoeck) | Slides

  • Recommended as the best book on mocks: Growing Object-Oriented Software, Guided by Tests by Steve Freeman & Nat Pryce
  • Common arguments against mocks
    • They duplicate implementation
    • They lead to brittle tests
  • Mock objects + procedural programming = bad idea
  • If you're doing traditional Rails development (which tends to follow more of a "procedural", do-this-and-then-do-that style), mock objects probably aren't for you
@lucashungaro
lucashungaro / gist:1924747
Created February 27, 2012 15:38
Some ideas on a small framework. Yet another web framework.
class Lists < App
authenticate { AuthenticationService.authenticate(session) } # just an example, it checks for true or false only
authorize { current_user.role? :admin }, :for => :create, :update # same here
all { List.all } # :get /lists
# the param name should always be class.name.singularize
one { List.find(params[:list]) } # :get /lists/:id
instance { List.new(params[:list]) }
@lucashungaro
lucashungaro / gist:1924445
Created February 27, 2012 15:09 — forked from fnando/gist:1920324
Some ideas on a small framework. Yet another web framework.
class Lists < App
all { List.all } # :get /lists
one { List.find(params[:id]) } # :get /lists/:id
instance { List.new(params[:list]) }
to_update { list.update_attributes(params[:list]) }
to_destroy { list.destroy }
# If you want to set an action for this resource only.
get :search do
@lucashungaro
lucashungaro / irbrc.rb
Created August 15, 2011 21:21
hack to load "system" gems not listed in your Gemfile when using Rails console - add this snippet to your ~/.irbrc
# Add all gems installed in the system to the $LOAD_PATH so they can be used in Rails console with Bundler
if defined?(::Bundler)
current_ruby_version = `rbenv version`.split(" ").first
gem_paths = Dir.glob("#{ENV["HOME"]}/.rbenv/versions/#{current_ruby_version}/lib/ruby/gems/**/gems/*/lib")
gem_paths.each do |path|
$LOAD_PATH << path
end
end
# now you can just normally install these gems ('gem install <gem-name>') into your system and keep your Gemfile listing only your app's dependencies. I use this with wirble, awesome_print and looksee and it works just fine. :)