Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

tehviking commented Nov 21, 2011

This is really cool, thanks for sharing it!

@boy-jer

This comment has been minimized.

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
You can’t perform that action at this time.