Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 117 additions & 107 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/faker/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def street_name
parse('address.street_name')
end

def street_address(include_secondary = false)
def street_address(include_secondary: false)
numerify(parse('address.street_address') + (include_secondary ? ' ' + secondary_address : ''))
end

Expand All @@ -23,7 +23,7 @@ def building_number
bothify(fetch('address.building_number'))
end

def zip_code(state_abbreviation = '')
def zip_code(state_abbreviation: '')
return bothify(fetch('address.postcode')) if state_abbreviation === ''

# provide a zip code that is valid for the state provided
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/avatar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Avatar < Base
class << self
SUPPORTED_FORMATS = %w(png jpg bmp)

def image(slug = nil, size = '300x300', format = 'png', set = 'set1', bgset = nil)
def image(slug: nil, size: '300x300', format: 'png', set: 'set1', bgset: nil)
raise ArgumentError, "Size should be specified in format 300x300" unless size.match(/^[0-9]+x[0-9]+$/)
raise ArgumentError, "Supported formats are #{SUPPORTED_FORMATS.join(', ')}" unless SUPPORTED_FORMATS.include?(format)
slug ||= Faker::Lorem.words.join
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/boolean.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Faker
class Boolean < Base
class << self
def boolean(true_ratio = 0.5)
def boolean(true_ratio: 0.5)
(rand < true_ratio)
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/faker/code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ def npi

# By default generates 10 sign isbn code in format 123456789-X
# You can pass 13 to generate new 13 sign code
def isbn(base = 10)
def isbn(base: 10)
base == 13 ? generate_base13_isbn : generate_base10_isbn
end

# By default generates 13 sign ean code in format 1234567890123
# You can pass 8 to generate ean8 code
def ean(base = 13)
def ean(base: 13)
base == 8 ? generate_base8_ean : generate_base13_ean
end

def rut
value = Number.number(8)
value = Number.number(digits: 8)
vd = rut_verificator_digit(value)
value << "-#{vd}"
end

# By default generates a Singaporean NRIC ID for someone
# who is born between the age of 18 and 65.
def nric(min_age = 18, max_age = 65)
birthyear = Date.birthday(min_age, max_age).year
def nric(min_age: 18, max_age: 65)
birthyear = Date.birthday(min_age: min_age, max_age: max_age).year
prefix = birthyear < 2000 ? 'S' : 'T'
values = birthyear.to_s[-2..-1]
values << regexify(/\d{5}/)
Expand Down
4 changes: 2 additions & 2 deletions lib/faker/commerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def color
fetch('color.name')
end

def department(max = 3, fixed_amount = false)
def department(max: 3, fixed_amount: false)
num = max if fixed_amount
num ||= 1 + rand(max)

Expand All @@ -27,7 +27,7 @@ def material
fetch('commerce.product_name.material')
end

def price(range=0..100.0)
def price(range: 0..100.0)
random = Random::DEFAULT
(random.rand(range) * 100).floor/100.0
end
Expand Down
20 changes: 10 additions & 10 deletions lib/faker/date.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
module Faker
class Date < Base
class << self
def between(from, to)
def between(from: raise(ArgumentError), to: raise(ArgumentError))
from = get_date_object(from)
to = get_date_object(to)

Faker::Base::rand_in_range(from, to)
end

def between_except(from, to, excepted)
def between_except(from: raise(ArgumentError), to: raise(ArgumentError), except: raise(ArgumentError))
begin
date = between(from, to)
end while date == excepted
date = between(from: from, to: to)
end while date == except

date
end

def forward(days = 365)
def forward(days: 365)
from = ::Date.today + 1
to = ::Date.today + days

between(from, to).to_date
between(from: from, to: to).to_date
end

def backward(days = 365)
def backward(days: 365)
from = ::Date.today - days
to = ::Date.today - 1

between(from, to).to_date
between(from: from, to: to).to_date
end

def birthday(min_age = 18, max_age = 65)
def birthday(min_age: 18, max_age: 65)
t = ::Date.today
top_bound, bottom_bound = prepare_bounds(t, min_age, max_age)
years = handled_leap_years(top_bound, bottom_bound)

from = ::Date.new(years[0], t.month, t.day)
to = ::Date.new(years[1], t.month, t.day)

between(from, to).to_date
between(from: from, to: to).to_date
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def mime_type
fetch('file.mime_type')
end

def file_name(dir = nil, name = nil, ext = nil, directory_separator = '/')
def file_name(dir: nil, name: nil, ext: nil, directory_separator: '/')

dir = Faker::Internet::slug unless dir
name = Faker::Lorem::word().downcase unless name
Expand Down
24 changes: 12 additions & 12 deletions lib/faker/hipster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ def word
random_word.match(/\s/) ? word : random_word
end

def words(num = 3, supplemental = false, spaces_allowed = false)
resolved_num = resolve(num)
def words(count: 3, supplemental: false, spaces_allowed: false)
resolved_num = resolve(count)
word_list = (
translate('faker.hipster.words') +
(supplemental ? translate('faker.lorem.words') : [])
Expand All @@ -19,26 +19,26 @@ def words(num = 3, supplemental = false, spaces_allowed = false)
words.each_with_index { |w, i| words[i] = word if w.match(/\s/) }
end

def sentence(word_count = 4, supplemental = false, random_words_to_add = 6)
words(word_count + rand(random_words_to_add.to_i).to_i, supplemental, true).join(' ').capitalize + '.'
def sentence(word_count: 4, supplemental: false, random_words_to_add: 6)
words(count: word_count + rand(random_words_to_add.to_i).to_i, supplemental: supplemental, spaces_allowed: true).join(' ').capitalize + '.'
end

def sentences(sentence_count = 3, supplemental = false)
def sentences(count: 3, supplemental: false)
[].tap do |sentences|
1.upto(resolve(sentence_count)) do
sentences << sentence(3, supplemental)
1.upto(resolve(count)) do
sentences << sentence(word_count: 3, supplemental: supplemental)
end
end
end

def paragraph(sentence_count = 3, supplemental = false, random_sentences_to_add = 3)
sentences(resolve(sentence_count) + rand(random_sentences_to_add.to_i).to_i, supplemental).join(' ')
def paragraph(sentence_count: 3, supplemental: false, random_sentences_to_add: 3)
sentences(count: resolve(sentence_count) + rand(random_sentences_to_add.to_i).to_i, supplemental: supplemental).join(' ')
end

def paragraphs(paragraph_count = 3, supplemental = false)
def paragraphs(count: 3, supplemental: false)
[].tap do |paragraphs|
1.upto(resolve(paragraph_count)) do
paragraphs << paragraph(3, supplemental)
1.upto(resolve(count)) do
paragraphs << paragraph(sentence_count: 3, supplemental: supplemental)
end
end
end
Expand Down
28 changes: 14 additions & 14 deletions lib/faker/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
module Faker
class Internet < Base
class << self
def email(name = nil)
[user_name(name), domain_name].join('@')
def email(name: nil)
[user_name(specifier: name), domain_name].join('@')
end

def free_email(name = nil)
[user_name(name), fetch('internet.free_email')].join('@')
def free_email(name: nil)
[user_name(specifier: name), fetch('internet.free_email')].join('@')
end

def safe_email(name = nil)
[user_name(name), 'example.'+ %w[org com net].shuffle.first].join('@')
def safe_email(name: nil)
[user_name(specifier: name), 'example.'+ %w[org com net].shuffle.first].join('@')
end

def user_name(specifier = nil, separators = %w(. _))
def user_name(specifier: nil, separators: %w(. _))
with_locale(:en) do
if specifier.kind_of? String
return specifier.scan(/\w+/).shuffle.join(separators.sample).downcase
elsif specifier.kind_of? Integer
tries = 0 # Don't try forever in case we get something like 1_000_000.
begin
result = user_name nil, separators
result = user_name(separators: separators)
tries += 1
end while result.length < specifier and tries < 7
until result.length >= specifier
Expand All @@ -31,7 +31,7 @@ def user_name(specifier = nil, separators = %w(. _))
elsif specifier.kind_of? Range
tries = 0
begin
result = user_name specifier.min, separators
result = user_name(specifier: specifier.min, separators: separators)
tries += 1
end while not specifier.include? result.length and tries < 7
return result[0...specifier.max]
Expand All @@ -46,7 +46,7 @@ def user_name(specifier = nil, separators = %w(. _))
end
end

def password(min_length = 8, max_length = 16, mix_case = true, special_chars = false)
def password(min_length: 8, max_length: 16, mix_case: true, special_chars: false)
temp = Lorem.characters(min_length)
diff_length = max_length - min_length
if diff_length > 0
Expand Down Expand Up @@ -90,7 +90,7 @@ def domain_suffix
fetch('internet.domain_suffix')
end

def mac_address(prefix='')
def mac_address(prefix: '')
prefix_digits = prefix.split(':').map{ |d| d.to_i(16) }
address_digits = (6 - prefix_digits.size).times.map{ rand(256) }
(prefix_digits + address_digits).map{ |d| '%02x' % d }.join(':')
Expand Down Expand Up @@ -144,13 +144,13 @@ def ip_v6_cidr
"#{ip_v6_address}/#{1 + rand(127)}"
end

def url(host = domain_name, path = "/#{user_name}")
def url(host: domain_name, path: "/#{user_name}")
"http://#{host}#{path}"
end

def slug(words = nil, glue = nil)
def slug(words: nil, glue: nil)
glue ||= %w[- _ .].sample
(words || Faker::Lorem::words(2).join(' ')).gsub(' ', glue).downcase
(words || Faker::Lorem::words(count: 2).join(' ')).gsub(' ', glue).downcase
end

def device_token
Expand Down
42 changes: 21 additions & 21 deletions lib/faker/lorem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def word
translate('faker.lorem.words').sample
end

def words(num = 3, supplemental = false)
resolved_num = resolve(num)
def words(count: 3, supplemental: false)
resolved_num = resolve(count)
word_list = (
translate('faker.lorem.words') +
(supplemental ? translate('faker.lorem.supplemental') : [])
Expand All @@ -22,44 +22,44 @@ def character
CHARACTERS.sample
end

def characters(char_count = 255)
char_count = resolve(char_count)
return '' if char_count.to_i < 1
Array.new(char_count) { CHARACTERS.sample }.join
def characters(count = 255)
count = resolve(count)
return '' if count.to_i < 1
Array.new(count) { CHARACTERS.sample }.join
end

def sentence(word_count = 4, supplemental = false, random_words_to_add = 6)
words(word_count + rand(random_words_to_add.to_i), supplemental).join(' ').capitalize + '.'
def sentence(word_count: 4, supplemental: false, random_words_to_add: 6)
words(count: word_count + rand(random_words_to_add.to_i), supplemental: supplemental).join(' ').capitalize + '.'
end

def sentences(sentence_count = 3, supplemental = false)
def sentences(count: 3, supplemental: false)
[].tap do |sentences|
1.upto(resolve(sentence_count)) do
sentences << sentence(3, supplemental)
1.upto(resolve(count)) do
sentences << sentence(word_count: 3, supplemental: supplemental)
end
end
end

def paragraph(sentence_count = 3, supplemental = false, random_sentences_to_add = 3)
sentences(resolve(sentence_count) + rand(random_sentences_to_add.to_i), supplemental).join(' ')
def paragraph(sentence_count: 3, supplemental: false, random_sentences_to_add: 3)
sentences(count: resolve(sentence_count) + rand(random_sentences_to_add.to_i), supplemental: supplemental).join(' ')
end

def paragraphs(paragraph_count = 3, supplemental = false)
def paragraphs(count: 3, supplemental: false)
[].tap do |paragraphs|
1.upto(resolve(paragraph_count)) do
paragraphs << paragraph(3, supplemental)
1.upto(resolve(count)) do
paragraphs << paragraph(sentence_count: 3, supplemental: supplemental)
end
end
end

def question(word_count = 4, supplemental = false, random_words_to_add = 6)
words(word_count + rand(random_words_to_add.to_i).to_i, supplemental).join(' ').capitalize + '?'
def question(word_count: 4, supplemental: false, random_words_to_add: 6)
words(count: word_count + rand(random_words_to_add.to_i).to_i, supplemental: supplemental).join(' ').capitalize + '?'
end

def questions(question_count = 3, supplemental = false)
def questions(count: 3, supplemental: false)
[].tap do |questions|
1.upto(resolve(question_count)) do
questions << question(3, supplemental)
1.upto(resolve(count)) do
questions << question(word_count: 3, supplemental: supplemental)
end
end
end
Expand Down
Loading