Skip to content

Instantly share code, notes, and snippets.

@Joseworks
Forked from kchens/Chapter_3.rb
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Joseworks/26188f4d09f2c8792e0d to your computer and use it in GitHub Desktop.
Save Joseworks/26188f4d09f2c8792e0d to your computer and use it in GitHub Desktop.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Needs to Manage Dependencies
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@rim = rim
@tire = tire
end
def ratio
chainring / cog.to_f
end
def gear_inches
# Gear knows about wheel
# Gear knows that wheel takes rim and tire
# Gear expect wheel to respond to diameter
ratio * Wheel.new(rim, tire).diameter
# ...
# if wheel changes, this method is broken
end
end
class Wheel
attr_reader :rim, :tire
def initialize(rim, tire)
@rim = rim
@tire = tire
end
def diameter
rim + (tire * 2)
end
end
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Inject Dependencies
# 1 -------------------GOOD
class Gear
attr_reader :charing, :cog, :wheel
def initialize(charing, cog, wheel)
@chainring = chainring
@cog = cog
@wheel = wheel
end
def gear_inches
ratio * wheel.diameter
end
end
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Isolate Instance Creation -- ONLY IF YOU CAN'T INJECT DEPENDENCY
# 2a -------------------OKAY
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@wheel = Wheel.new(rim, tire)
end
# reduce dependency in gear_inches
def gear_inches
ratio * wheel.diameter
end
end
# 2b -------------------OKAY
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@rim = rim
@tire = tire
end
# reduce dependency in gear_inches
def gear_inches
ratio * wheel.diameter
end
def wheel
@wheel ||= Wheel.new(rim, tire)
end
end
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Isolate Vulnerable External Messages
# 3 -------------------GOOD
def gear_inches
foo = some_intermediate_result * diameter
end
def diameter
wheel.diameter
end
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Remove Argument-Order Dependencies
# 4 -------------------BAD
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring, cog, wheel)
@chainring = chainring
@cog = cog
@wheel = wheel
end
end
Gear.new(52, 11, WHeel.new(26, 1.5)).gear_inches
# 4 -------------------GOOD
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(args)
@chainring = args[:chainring]
@cog = args[:cog]
@wheel = args[:wheel]
end
end
Gear.new({:chainring => 52, :cog => 11, :wheel => Wheel.new(26, 1.5)}).gear_inches
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Define Defaults
# 5 -------------------GOOD
def initialize(args)
# CANNOT set the key to nil or false, #fetch can
@chainring = args[:chainring] || 40
@cog = args[:cog] || 18
@wheel = args[:wheel]
end
# 5 -------------------BEST
def initialize(args)
@chainring = args.fetch(:chainring, 40)
@cog = args.fetch(:cog, 18)
@wheel = args[:wheel]
end
# 5 -------------------BEST
def initialize(args)
# If the defaults are large, implement defaults method
args = defaults.merge(args)
@chainring = args[:chainring]
end
def defaults
{:chainring => 40, :cog => 18}
end
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Isolate Multiparameter Initialization w/Modules
# 6 -------------------GOOD
module SomeFramework
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring, cog, wheel)
@chainring = chainring
@cog = cog
@wheel = wheel
end
end
end
module GearWrapper
def self.gear(args)
SomeFramework::Gear.new(args[:chainring],
args[:cog],
args[:wheel])
end
end
GearWrapper.gear(
:chainring => 52,
:cog => 11,
:wheel => Wheel.new(26, 1.5)).gear_inches
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment