Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove support for positional arguments (deprecated on v2) #2576

Merged
merged 1 commit into from
Oct 5, 2022
Merged
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
52 changes: 0 additions & 52 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,58 +259,6 @@ def disable_enforce_available_locales
ensure
I18n.enforce_available_locales = old_enforce_available_locales
end

private

def warn_for_deprecated_arguments
keywords = []
yield(keywords)

return if keywords.empty?

method_name = caller.first.match(/`(?<method_name>.*)'/)[:method_name]

keywords.each.with_index(1) do |keyword, index|
i = case index
when 1 then '1st'
when 2 then '2nd'
when 3 then '3rd'
else "#{index}th"
end

warn_with_uplevel(<<~MSG, uplevel: 5)
Passing `#{keyword}` with the #{i} argument of `#{method_name}` is deprecated. Use keyword argument like `#{method_name}(#{keyword}: ...)` instead.
MSG
end

warn(<<~MSG)

To automatically update from positional arguments to keyword arguments,
install rubocop-faker and run:

rubocop \\
--require rubocop-faker \\
--only Faker/DeprecatedArguments \\
--auto-correct

MSG
end

# Workaround for emulating `warn '...', uplevel: 1` in Ruby 2.4 or lower.
def warn_with_uplevel(message, uplevel: 1)
at = parse_caller(caller[uplevel]).join(':')
warn "#{at}: #{message}"
end

def parse_caller(at)
# rubocop:disable Style/GuardClause
if /^(.+?):(\d+)(?::in `.*')?/ =~ at
file = Regexp.last_match(1)
line = Regexp.last_match(2).to_i
[file, line]
end
# rubocop:enable Style/GuardClause
end
end
end
end
Expand Down
12 changes: 2 additions & 10 deletions lib/faker/books/dune.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ def planet
# #=> "He who controls the spice, controls the universe!"
#
# @faker.version 1.9.3
def quote(legacy_character = NOT_GIVEN, character: nil)
warn_for_deprecated_arguments do |keywords|
keywords << :character if legacy_character != NOT_GIVEN
end

def quote(character: nil)
quoted_characters = translate('faker.dune.quotes').keys

if character.nil?
Expand Down Expand Up @@ -106,11 +102,7 @@ def quote(legacy_character = NOT_GIVEN, character: nil)
# #=> "May thy knife chip and shatter."
#
# @faker.version 1.9.3
def saying(legacy_source = NOT_GIVEN, source: nil)
warn_for_deprecated_arguments do |keywords|
keywords << :source if legacy_source != NOT_GIVEN
end

def saying(source: nil)
sourced_sayings = translate('faker.dune.sayings').keys

if source.nil?
Expand Down
45 changes: 7 additions & 38 deletions lib/faker/books/lovecraft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ def location
# #=> "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fht...
#
# @faker.version 1.9.3
def fhtagn(legacy_number = NOT_GIVEN, number: 1)
warn_for_deprecated_arguments do |keywords|
keywords << :number if legacy_number != NOT_GIVEN
end

def fhtagn(number: 1)
Array.new(number) { fetch('lovecraft.fhtagn') }.join('. ')
end

Expand Down Expand Up @@ -86,12 +82,7 @@ def tome
# #=> "Effulgence unmentionable gambrel."
#
# @faker.version 1.9.3
def sentence(legacy_word_count = NOT_GIVEN, legacy_random_words_to_add = NOT_GIVEN, word_count: 4, random_words_to_add: 6, open_compounds_allowed: true)
warn_for_deprecated_arguments do |keywords|
keywords << :word_count if legacy_word_count != NOT_GIVEN
keywords << :random_words_to_add if legacy_random_words_to_add != NOT_GIVEN
end

def sentence(word_count: 4, random_words_to_add: 6, open_compounds_allowed: true)
"#{words(number: word_count + rand(random_words_to_add.to_i).to_i, spaces_allowed: open_compounds_allowed).join(' ').capitalize}."
end

Expand Down Expand Up @@ -138,12 +129,7 @@ def word
# # ]
#
# @faker.version 1.9.3
def words(legacy_number = NOT_GIVEN, legacy_spaces_allowed = NOT_GIVEN, number: 3, spaces_allowed: false)
warn_for_deprecated_arguments do |keywords|
keywords << :number if legacy_number != NOT_GIVEN
keywords << :spaces_allowed if legacy_spaces_allowed != NOT_GIVEN
end

def words(number: 3, spaces_allowed: false)
resolved_num = resolve(number)
word_list = translate('faker.lovecraft.words')
word_list *= ((resolved_num / word_list.length) + 1)
Expand Down Expand Up @@ -176,11 +162,7 @@ def words(legacy_number = NOT_GIVEN, legacy_spaces_allowed = NOT_GIVEN, number:
# # ]
#
# @faker.version 1.9.3
def sentences(legacy_number = NOT_GIVEN, number: 3)
warn_for_deprecated_arguments do |keywords|
keywords << :number if legacy_number != NOT_GIVEN
end

def sentences(number: 3)
[].tap do |sentences|
1.upto(resolve(number)) do
sentences << sentence(word_count: 3)
Expand All @@ -207,12 +189,7 @@ def sentences(legacy_number = NOT_GIVEN, number: 3)
# #=> "Stench cyclopean fainted antiquarian nameless. Antiquarian ululate tenebrous non-euclidean effulgence."
#
# @faker.version 1.9.3
def paragraph(legacy_sentence_count = NOT_GIVEN, legacy_random_sentences_to_add = NOT_GIVEN, sentence_count: 3, random_sentences_to_add: 3)
warn_for_deprecated_arguments do |keywords|
keywords << :sentence_count if legacy_sentence_count != NOT_GIVEN
keywords << :random_sentences_to_add if legacy_random_sentences_to_add != NOT_GIVEN
end

def paragraph(sentence_count: 3, random_sentences_to_add: 3)
sentences(number: resolve(sentence_count) + rand(random_sentences_to_add.to_i).to_i).join(' ')
end

Expand All @@ -238,11 +215,7 @@ def paragraph(legacy_sentence_count = NOT_GIVEN, legacy_random_sentences_to_add
# # ]
#
# @faker.version 1.9.3
def paragraphs(legacy_number = NOT_GIVEN, number: 3)
warn_for_deprecated_arguments do |keywords|
keywords << :number if legacy_number != NOT_GIVEN
end

def paragraphs(number: 3)
[].tap do |paragraphs|
1.upto(resolve(number)) do
paragraphs << paragraph(sentence_count: 3)
Expand All @@ -263,11 +236,7 @@ def paragraphs(legacy_number = NOT_GIVEN, number: 3)
# #=> "Effulgence madness noisome. Fungus stygian mortal madness amorphous dank. Decadent noisome hideous effulgence. Tentacles charne."
#
# @faker.version 1.9.3
def paragraph_by_chars(legacy_characters = NOT_GIVEN, characters: 256)
warn_for_deprecated_arguments do |keywords|
keywords << :characters if legacy_characters != NOT_GIVEN
end

def paragraph_by_chars(characters: 256)
paragraph = paragraph(sentence_count: 3)

paragraph += " #{paragraph(sentence_count: 3)}" while paragraph.length < characters
Expand Down
30 changes: 5 additions & 25 deletions lib/faker/default/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ class << self
# #=> "Northfort, California"
#
# @faker.version 0.3.0
def city(legacy_options = NOT_GIVEN, options: {})
warn_for_deprecated_arguments do |keywords|
keywords << :options if legacy_options != NOT_GIVEN
end

def city(options: {})
parse(options[:with_state] ? 'address.city_with_state' : 'address.city')
end

Expand All @@ -49,11 +45,7 @@ def street_name
# Faker::Address.street_address #=> "282 Kevin Brook"
#
# @faker.version 0.3.0
def street_address(legacy_include_secondary = NOT_GIVEN, include_secondary: false)
warn_for_deprecated_arguments do |keywords|
keywords << :include_secondary if legacy_include_secondary != NOT_GIVEN
end

def street_address(include_secondary: false)
numerify(parse('address.street_address') + (include_secondary ? " #{secondary_address}" : ''))
end

Expand Down Expand Up @@ -121,11 +113,7 @@ def mail_box
# Faker::Address.zip_code(state_abbreviation: 'CO') #=> "80011"
#
# @faker.version 0.3.0
def zip_code(legacy_state_abbreviation = NOT_GIVEN, state_abbreviation: '')
warn_for_deprecated_arguments do |keywords|
keywords << :state_abbreviation if legacy_state_abbreviation != NOT_GIVEN
end

def zip_code(state_abbreviation: '')
if state_abbreviation.empty?
letterified_string = letterify(fetch('address.postcode'))
return numerify(letterified_string, leading_zero: true)
Expand Down Expand Up @@ -242,11 +230,7 @@ def country
# Faker::Address.country_by_code(code: 'NL') #=> "Netherlands"
#
# @faker.version 1.9.2
def country_by_code(legacy_code = NOT_GIVEN, code: 'US')
warn_for_deprecated_arguments do |keywords|
keywords << :code if legacy_code != NOT_GIVEN
end

def country_by_code(code: 'US')
fetch("address.country_by_code.#{code}")
end

Expand All @@ -260,11 +244,7 @@ def country_by_code(legacy_code = NOT_GIVEN, code: 'US')
# Faker::Address.country_name_to_code(name: 'united_states') #=> "US"
#
# @faker.version 1.9.2
def country_name_to_code(legacy_name = NOT_GIVEN, name: 'united_states')
warn_for_deprecated_arguments do |keywords|
keywords << :name if legacy_name != NOT_GIVEN
end

def country_name_to_code(name: 'united_states')
fetch("address.country_by_name.#{name}")
end

Expand Down
10 changes: 2 additions & 8 deletions lib/faker/default/alphanumeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ class << self
# Faker::Alphanumeric.alpha(number: 10) #=> "zlvubkrwga"
#
# @faker.version 1.9.2
def alpha(legacy_number = NOT_GIVEN, number: 32)
warn_for_deprecated_arguments do |keywords|
keywords << :number if legacy_number != NOT_GIVEN
end
def alpha(number: 32)
char_count = resolve(number)
return '' if char_count.to_i < 1

Expand All @@ -46,10 +43,7 @@ def alpha(legacy_number = NOT_GIVEN, number: 32)
# Faker::Alphanumeric.alphanumeric(number: 10, min_alpha: 3, min_numeric: 3) #=> "3yfq2phx8b"
#
# @faker.version 2.1.3
def alphanumeric(legacy_number = NOT_GIVEN, number: 32, min_alpha: 0, min_numeric: 0)
warn_for_deprecated_arguments do |keywords|
keywords << :number if legacy_number != NOT_GIVEN
end
def alphanumeric(number: 32, min_alpha: 0, min_numeric: 0)
char_count = resolve(number)
return '' if char_count.to_i < 1
raise ArgumentError, 'min_alpha must be greater than or equal to 0' if min_alpha&.negative?
Expand Down
11 changes: 1 addition & 10 deletions lib/faker/default/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ def author
parse('app.author')
end

# rubocop:disable Metrics/ParameterLists

##
# Produces a String representing a semantic version identifier.
#
Expand All @@ -62,16 +60,9 @@ def author
# Faker::App.semantic_version(patch: 5..6) #=> "7.2.6"
#
# @faker.version 1.4.3
def semantic_version(legacy_major = NOT_GIVEN, legacy_minor = NOT_GIVEN, legacy_patch = NOT_GIVEN, major: 0..9, minor: 0..9, patch: 1..9)
warn_for_deprecated_arguments do |keywords|
keywords << :major if legacy_major != NOT_GIVEN
keywords << :minor if legacy_minor != NOT_GIVEN
keywords << :patch if legacy_patch != NOT_GIVEN
end

def semantic_version(major: 0..9, minor: 0..9, patch: 1..9)
[major, minor, patch].map { |chunk| sample(Array(chunk)) }.join('.')
end
# rubocop:enable Metrics/ParameterLists
end
end
end
13 changes: 1 addition & 12 deletions lib/faker/default/avatar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class << self
# @private
SUPPORTED_FORMATS = %w[png jpg bmp].freeze

# rubocop:disable Metrics/ParameterLists

##
# Produces a URL for an avatar from robohash.org
#
Expand Down Expand Up @@ -38,23 +36,14 @@ class << self
# #=> "https://robohash.org/my-own-slug.bmp?size=50x50&set=set1&bgset=bg1"
#
# @faker.version 1.4.3
def image(legacy_slug = NOT_GIVEN, legacy_size = NOT_GIVEN, legacy_format = NOT_GIVEN, legacy_set = NOT_GIVEN, legacy_bgset = NOT_GIVEN, slug: nil, size: '300x300', format: 'png', set: 'set1', bgset: nil)
warn_for_deprecated_arguments do |keywords|
keywords << :slug if legacy_slug != NOT_GIVEN
keywords << :size if legacy_size != NOT_GIVEN
keywords << :format if legacy_format != NOT_GIVEN
keywords << :set if legacy_set != NOT_GIVEN
keywords << :bgset if legacy_bgset != NOT_GIVEN
end

def image(slug: nil, size: '300x300', format: 'png', set: 'set1', bgset: nil)
raise ArgumentError, 'Size should be specified in format 300x300' unless size =~ /^[0-9]+x[0-9]+$/
raise ArgumentError, "Supported formats are #{SUPPORTED_FORMATS.join(', ')}" unless SUPPORTED_FORMATS.include?(format)

slug ||= Faker::Lorem.words.join
bgset_query = "&bgset=#{bgset}" if bgset
"https://robohash.org/#{slug}.#{format}?size=#{size}&set=#{set}#{bgset_query}"
end
# rubocop:enable Metrics/ParameterLists
end
end
end
12 changes: 2 additions & 10 deletions lib/faker/default/bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ class << self
# Faker::Bank.account_number(digits: 13) #=> 673858237902
#
# @faker.version 1.9.1
def account_number(legacy_digits = NOT_GIVEN, digits: 10)
warn_for_deprecated_arguments do |keywords|
keywords << :digits if legacy_digits != NOT_GIVEN
end

def account_number(digits: 10)
output = ''

output += rand.to_s[2..] while output.length < digits
Expand All @@ -40,14 +36,10 @@ def account_number(legacy_digits = NOT_GIVEN, digits: 10)
# Faker::Bank.iban(country_code: nil) #=> "DE45186738071857270067"
#
# @faker.version 1.7.0
def iban(legacy_country_code = NOT_GIVEN, country_code: 'GB')
def iban(country_code: 'GB')
# Each country has its own format for bank accounts
# Many of them use letters in certain parts of the account
# Using regex patterns we can create virtually any type of bank account
warn_for_deprecated_arguments do |keywords|
keywords << :country_code if legacy_country_code != NOT_GIVEN
end

country_code ||= iban_country_code

begin
Expand Down
5 changes: 1 addition & 4 deletions lib/faker/default/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ class << self
# Faker::Boolean.boolean(true_ratio: 0.2) #=> false
#
# @faker.version 1.6.2
def boolean(legacy_true_ratio = NOT_GIVEN, true_ratio: 0.5)
warn_for_deprecated_arguments do |keywords|
keywords << :true_ratio if legacy_true_ratio != NOT_GIVEN
end
def boolean(true_ratio: 0.5)
(rand < true_ratio)
end
end
Expand Down
14 changes: 2 additions & 12 deletions lib/faker/default/chile_rut.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ class << self
# Faker::ChileRut.rut(min_rut: 20890156, fixed: true) #=> 20890156
#
# @faker.version 1.9.2
def rut(legacy_min_rut = NOT_GIVEN, legacy_fixed = NOT_GIVEN, min_rut: 1, fixed: false)
warn_for_deprecated_arguments do |keywords|
keywords << :min_rut if legacy_min_rut != NOT_GIVEN
keywords << :fixed if legacy_fixed != NOT_GIVEN
end

def rut(min_rut: 1, fixed: false)
@last_rut = fixed ? min_rut : rand_in_range(min_rut, 99_999_999)
end

Expand Down Expand Up @@ -83,12 +78,7 @@ def check_digit
# Faker::ChileRut.full_rut(min_rut: 30686957, fixed: true) #=> "30686957-4"
#
# @faker.version next
def full_rut(legacy_min_rut = NOT_GIVEN, legacy_fixed = NOT_GIVEN, min_rut: 0, fixed: false, formatted: false)
warn_for_deprecated_arguments do |keywords|
keywords << :min_rut if legacy_min_rut != NOT_GIVEN
keywords << :fixed if legacy_fixed != NOT_GIVEN
end

def full_rut(min_rut: 0, fixed: false, formatted: false)
if formatted
"#{rut(min_rut: min_rut, fixed: fixed).to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1.').reverse}-#{dv}"
else
Expand Down
Loading