Skip to content

Instantly share code, notes, and snippets.

@kchens
kchens / Chapter_8.rb
Created April 7, 2015 04:58
POODR: Chapter 8 - Combining Objects With Composition
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Updating the Bicycle Class
# 1 -------------------Okay
class Bicycle
attr_reader :size, :parts
def initialize(args={})
@size = args[:size]
@kchens
kchens / Chapter_7.rb
Created April 7, 2015 04:57
POODR: Chapter 7 - Sharing Role Behavior With Modules
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Writing the Concrete Code
# 1 -------------------Bad
class Schedule
def scheduled?(schedulable, start_date, end_date)
puts "This #{schedulable.class} " +
"is not scheduled\n" +
" between #{start_date} and #{end_date}"
@kchens
kchens / Chapter_6.rb
Created April 7, 2015 04:54
POODR: Chapter 6 - Acquiring Behavior Through Inheritance
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Starting Class
# 1 -------------------Okay
class Bicycle
attr_reader :size, :tape_color
def initialize(args)
@size = args[:size]
@kchens
kchens / Chapter_5.rb
Created March 25, 2015 23:44
POODR: Chapter 5 - Reducing Costs With Duck Typing
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Overlooking Ducks
# 1 -------------------BAD
class Trip
attr_reader :bicycles, :customers, :vehicle
def prepare(mechanic)
# no depedency on mecahnic
@kchens
kchens / Chapter_4.rb
Created March 25, 2015 23:42
POODR: Chapter 4 - Creating Flexible Interfaces
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Law of Demeter
# set of coding rules that results in loosely coupled objects. It prohibits
# routing a message to a third object via a second object of a different type.
# Paraphrased: "only talk to your immediate neighbors", "use only one dot"
class Trip
def depart
# "train wrecks"
@kchens
kchens / Chapter_3.rb
Created March 25, 2015 23:41
POODR: Chapter 3 - Managing Dependencies
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Needs to Manage Dependencies
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@rim = rim
@kchens
kchens / Chapter_2.rb
Created March 25, 2015 23:10
POODR: Chapter 2 - Designing Classes With Single Repsonsibility
# 1 -------------------BAD
class Gear
attr_reader :chainring, :cog
def initialize(chainring, cog)
@chainring = chainring
@cog = cog
end
def ratio
@kchens
kchens / "Skinny" StockController - Service Object.rb
Created March 24, 2015 21:43
"Skinny" StockController - ServiceObject
# ######################
# Refactored
# ######################
# app/services/stock_service.rb
class StockService
def initialize(user)
@user = user
@stocks = []
@stock_symbols = []
@kchens
kchens / "Skinny" StockController.rb
Last active August 29, 2015 14:17
"Skinny" StockController
# ######################
# Refactored
# ######################
# app/controllers/stocks_controller.rb
require 'stock_quote'
require 'pp'
@kchens
kchens / "Fat" StockController.rb
Created March 24, 2015 21:22
"FAT" StockController - GuideSpark
require 'stock_quote'
require 'pp'
class StocksController < ApplicationController
before_action :load_user
skip_before_filter :verify_authenticity_token
def index
update_stocks
end