Created
November 26, 2011 00:23
-
-
Save croaky/1394739 to your computer and use it in GitHub Desktop.
Dependency Injection in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TaxCodeStrategies | |
class UnitedStates | |
def generate(id) | |
"US-#{id}" | |
end | |
end | |
class Brazil | |
def generate(id) | |
"#{id}-BR" | |
end | |
end | |
end | |
describe TaxCodeStrategies::UnitedStates, "#generate" do | |
let(:strategy) { TaxCodeStrategies::UnitedStates.new } | |
it "prepends US- to given ID" do | |
strategy.generate(123456).should == "US-123456" | |
end | |
end | |
describe TaxCodeStrategies::Brazil, "#generate" do | |
let(:strategy) { TaxCodeStrategies::Brazil.new } | |
it "appends -BR to given ID" do | |
strategy.generate(123456).should == "123456-BR" | |
end | |
end | |
class User | |
attr_accessor :id, :tax_code_strategy | |
def initialize(id, tax_code_strategy = TaxCodeStrategies::UnitedStates) | |
self.id = id | |
self.tax_code_strategy = tax_code_strategy.new | |
end | |
def tax_code | |
tax_code_strategy.generate(id) | |
end | |
end | |
describe User, "#tax_code" do | |
let(:user) { User.new(123456) } | |
let(:fake_strategy) do | |
Class.new do | |
def generate(id) | |
"SWISS-#{id}" | |
end | |
end.new | |
end | |
it "can be any strategy that responds to #generate" do | |
user.tax_code_strategy = fake_strategy | |
user.tax_code.should == "SWISS-123456" | |
end | |
end | |
describe User, "#tax_code_strategy" do | |
let(:user) { User.new(123456) } | |
it "uses US strategy by default" do | |
user.tax_code_strategy.should be_instance_of(TaxCodeStrategies::UnitedStates) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TaxCode | |
GENERATORS = { | |
:us => lambda { |id| "US-#{id}" }, | |
:br => lambda { |id| "#{id}-BR" } | |
} | |
def self.generate(code, id) | |
gen = GENERATORS[code] || raise(ArgumentError, "No generator for country #{code}") | |
gen.call(id) | |
end | |
end | |
describe TaxCode, ".generate" do | |
context "when Brazil" do | |
it "appends -BR to given ID" do | |
TaxCode.generate(:br, 123456).should == "123456-BR" | |
end | |
end | |
context "when United States" do | |
it "prepends US- to given ID" do | |
TaxCode.generate(:us, 123456).should == "US-123456" | |
end | |
end | |
end | |
class User | |
attr_accessor :id, :country_code | |
def initialize(id, country_code = :us) | |
self.id = id | |
self.country_code = country_code | |
end | |
def tax_code | |
TaxCode.generate(country_code, id) | |
end | |
end | |
describe User, "#tax_code" do | |
let(:user) { User.new(123456) } | |
before do | |
TaxCode.stub(generate: "SWISS-123456") | |
end | |
it "can be any TaxCode country code" do | |
user.country_code = :swiss | |
user.tax_code.should == "SWISS-123456" | |
end | |
end | |
describe User, "#country_code" do | |
let(:user) { User.new(123456) } | |
it "uses US country code by default" do | |
user.country_code.should == :us | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment