Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save drKreso/1381080 to your computer and use it in GitHub Desktop.

Select an option

Save drKreso/1381080 to your computer and use it in GitHub Desktop.
Flexibility with and without Dependency Injection
require 'minitest/autorun'
class UsTaxCode
def generate(id)
"US-#{id}"
end
end
class BrazilTaxCode
def generate(id)
"#{id}-BR"
end
end
class Customer
attr_accessor :id
def initialize(id)
self.id = id
end
def tax_code(tax_code_generator = UsTaxCode)
tax_code_generator.new.generate(id)
end
end
class WhimsicalTaxCode
def generate(id)
"****#{id.to_s.reverse}****"
end
end
describe Customer do
it "has an american tax code by default" do
Customer.new(123456).tax_code.must_equal "US-123456"
end
it "can compute any type of tax code" do
customer = Customer.new(123456)
customer.tax_code(BrazilTaxCode).must_equal "123456-BR"
customer.tax_code(UsTaxCode).must_equal "US-123456"
customer.tax_code(WhimsicalTaxCode).must_equal "****654321****"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment