Created
August 11, 2009 11:26
-
-
Save lwe/165751 to your computer and use it in GitHub Desktop.
Swiss-ify random data generated by faker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# make faker a bit more swiss-friendly :) | |
module Faker | |
class Company | |
# Add swiss suffixes like AG, GmbH etc. | |
def self.suffix | |
%w(AG GmbH und\ Söhne und\ Partner &\ Co. Gruppe LLC Inc.).rand | |
end | |
end | |
class Internet | |
# Add extensions like ch, de, li and at | |
def self.domain_suffix | |
%w(com net biz ch de li at ch ch).rand | |
end | |
end | |
class Address | |
# Swiss ZIP codes are like 5000 or 3000 etc... | |
def self.ch_zipcode | |
"#{rand(9)+1}#{Faker.numerify('###')}" | |
end | |
# just some country codes... | |
def self.country_code | |
%w(CH CH CH DE AT US LI US HK VN).rand | |
end | |
end | |
class PhoneNumber | |
COMPANY_FORMATS = ['0800 ### ###', '0800 ## ## ##', '0## ### ## ##', '0## ### ## ##', '+41 ## ### ## ##', '0900 ### ###'] | |
SIMPLE_FORMATS = ['0## ### ## ##', '+41## ### ## ##'] | |
MOBILE_FORMATS = ['07M ### ## ##', '+417M ### ## ##', '0041 7M ### ## ##'] | |
# Allows creation of different phone numbers | |
def self.ch_phone_number(kind = :simple) | |
Faker.numerify const_get("#{kind.to_s.upcase}_FORMATS").rand.gsub('M', "#{rand(4)+6}") | |
end | |
end | |
end |
In 1.9.3 there's Array#sample
, though this code is pretty old, so no idea if it still works as expected, good luck :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In what context does this code work? In Ruby 1.9.3, I get:
So
[].rand
does not seem to work.