Skip to content

Instantly share code, notes, and snippets.

@mperham
Created November 19, 2011 22:33
Show Gist options
  • Select an option

  • Save mperham/1379464 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

This is really cool, thanks for sharing it!

@boy-jer

boy-jer commented Dec 29, 2012

Copy link
Copy Markdown

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