Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created September 14, 2012 23:13
Show Gist options
  • Save myronmarston/3725544 to your computer and use it in GitHub Desktop.
Save myronmarston/3725544 to your computer and use it in GitHub Desktop.
Alternate pattern for Shock costing from Sandi Metz's GoGaRuCo Talk
class Shock
def intialize(type)
@type = type
end
def cost
coster_for(type).compute
end
private
def coster_for(type)
coster_implementations.fetch(type) do
raise ArgumentError, "Unrecoganized cost type: #{type}"
end.new
end
def self.coster_implementations
@coster_implementations ||= {}
end
class BaseCoster
def self.register_as(type)
Shock.coster_implementations[type] = self
end
end
class FrontShockCoster < BaseCoster
def compute
end
register_as :front
end
class RearShockCoster < BaseCoster
def compute
end
register_as :rear
end
class LeftyShockCoster < BaseCoster
def compute
end
register_as :lefty
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment