custom_faker_provider.md
🛠️ Crafting Custom Faker Providers for Domain-specific Data
Faker allows you to define custom providers to generate domain-specific data. This is ideal when your application requires specialized formats or complex patterns that built-in generators don’t cover. You can create a provider by subclassing Faker::Base
, defining methods, and then registering it with Faker::Base.add_provider
. Once registered, you can call it like any other Faker generator.
module Faker
class MyDomain < Base
flexible :my_domain
class << self
def transaction_id
"TXN-#{rand(1000..9999)}-#{sample(["USD","EUR","JPY"])}"
end
def tracking_code
Array.new(3) { rand(65..90).chr }.join + rand(10**6).to_s.rjust(6, '0')
end
end
end
end
# Register the provider globally
Faker::Base.add_provider(Faker::MyDomain)
# Usage
Faker::MyDomain.transaction_id #=> "TXN-4823-EUR"
Faker::MyDomain.tracking_code #=> "QWG042519"