Skip to content

Instantly share code, notes, and snippets.

@diegodfsd
Forked from mperham/gist:1379464
Created November 20, 2011 02:45
Show Gist options
  • Save diegodfsd/1379740 to your computer and use it in GitHub Desktop.
Save diegodfsd/1379740 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
def initialize(country_code, id)
@country_code = country_code
@id = id
end
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