Skip to content

Instantly share code, notes, and snippets.

@grigio
Forked from mperham/gist:1379464
Created November 8, 2012 19:01
Show Gist options
  • Save grigio/4040789 to your computer and use it in GitHub Desktop.
Save grigio/4040789 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
u = User.new
u.id=1234
u.country_code = :br
u.tax_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment