Skip to content

Instantly share code, notes, and snippets.

@croaky
Forked from mperham/gist:1379464
Created November 22, 2011 15:36
Show Gist options
  • Save croaky/1385953 to your computer and use it in GitHub Desktop.
Save croaky/1385953 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment