Skip to content

Instantly share code, notes, and snippets.

@mperham
Created November 19, 2011 22:33
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save mperham/1379464 to your computer and use it in GitHub Desktop.
Save mperham/1379464 to your computer and use it in GitHub Desktop.
Flexibility without Dependency Injection
class TaxCode
GENERATORS = {
:us => lambda { |id| "US-#{id}" },
:br => lambda { |id| "#{id + 9}-BRA" },
}
def self.generate(code, id)
gen = GENERATORS[code] || raise ArgumentError, "No generator for country #{code}"
gen.call(id)
end
end
class User
attr_accessor :country_code
attr_accessor :id
def tax_code
TaxCode.generate(country_code, id)
end
end
@tehviking
Copy link

This is really cool, thanks for sharing it!

@boy-jer
Copy link

boy-jer commented Dec 29, 2012

To complete the loop of the context in which this gist was written, it is in response to this blog post http://kresimirbojcic.com/2011/11/19/dependency-injection-in-ruby.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment