Skip to content

Commit

Permalink
Rubocop 1.0 fixes (#2182)
Browse files Browse the repository at this point in the history
* rubocop -a --only Style/AccessorGrouping

* rubocop -a --only Style/RedundantRegexpEscape

* Adjust broken commend indentation with the previous commit

* rubocop -A --only Style/StringConcatenation

* rubocop -a --only Style/RedundantParentheses

* rubocop -a --only Style/RedundantAssignment

* rubocop -A --only Style/CaseLikeIf

* rubocop -a --only Lint/RedundantStringCoercion

* rubocop -a --only Style/ClassEqualityComparison

* rubocop -a --only Layout/ElseAlignment

* rubocop -a --only Layout/EndAlignment

* rubocop -a --only Layout/TrailingWhitespace

* rubocop -a --only Layout/EmptyLinesAroundMethodBody

* rubocop -a --only Layout/SpaceAroundMethodCallOperator

* rubocop -A --only Style/ExplicitBlockArgument

* Remove empty test file

This empty file was introduced at the initial commit for this michael_scott faker: bcb8cf0
The actual tests are written in another file: test/faker/tv_shows/test_michael_scott.rb

* rubocop -a --only Layout/IndentationWidth

* Address rubocop Lint/MissingSuper offence

* Address rubocop Lint/MixedRegexpCaptureTypes offences

* Address rubocop Style/StringConcatenation offence

* Address rubocop Style/OptionalBooleanParameter offence

* Address rubocop Style/RedundantRegexpCharacterClass offences

* `method_missing` here falls back to `raise` so never calls `super`

* Faker::Music is not a Hash. It has #keys but has no #each_key
  • Loading branch information
amatsuda committed Oct 28, 2020
1 parent 9ea5a3d commit 7046990
Show file tree
Hide file tree
Showing 68 changed files with 211 additions and 206 deletions.
21 changes: 10 additions & 11 deletions lib/faker.rb
Expand Up @@ -34,8 +34,7 @@ class Config
@random = nil

class << self
attr_writer :locale
attr_writer :random
attr_writer :locale, :random

def locale
Faker.load_i18n
Expand Down Expand Up @@ -100,13 +99,13 @@ def bothify(string)
def regexify(reg)
reg = reg.source if reg.respond_to?(:source) # Handle either a Regexp or a String that looks like a Regexp
reg
.gsub(%r{^\/?\^?}, '').gsub(%r{\$?\/?$}, '') # Ditch the anchors
.gsub(%r{^/?\^?}, '').gsub(%r{\$?/?$}, '') # Ditch the anchors
.gsub(/\{(\d+)\}/, '{\1,\1}').gsub(/\?/, '{0,1}') # All {2} become {2,2} and ? become {0,1}
.gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # [12]{1,2} becomes [12] or [12][12]
.gsub(/(\([^\)]+\))\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # (12|34){1,2} becomes (12|34) or (12|34)(12|34)
.gsub(/(\([^)]+\))\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # (12|34){1,2} becomes (12|34) or (12|34)(12|34)
.gsub(/(\\?.)\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # A{1,2} becomes A or AA or \d{3} becomes \d\d\d
.gsub(/\((.*?)\)/) { |match| sample(match.gsub(/[\(\)]/, '').split('|')) } # (this|that) becomes 'this' or 'that'
.gsub(/\[([^\]]+)\]/) { |match| match.gsub(/(\w\-\w)/) { |range| sample(Array(Range.new(*range.split('-')))) } } # All A-Z inside of [] become C (or X, or whatever)
.gsub(/\((.*?)\)/) { |match| sample(match.gsub(/[()]/, '').split('|')) } # (this|that) becomes 'this' or 'that'
.gsub(/\[([^\]]+)\]/) { |match| match.gsub(/(\w-\w)/) { |range| sample(Array(Range.new(*range.split('-')))) } } # All A-Z inside of [] become C (or X, or whatever)
.gsub(/\[([^\]]+)\]/) { |_match| sample(Regexp.last_match(1).split('')) } # All [ABC] become B (or A or C)
.gsub('\d') { |_match| sample(Numbers) }
.gsub('\w') { |_match| sample(Letters) }
Expand All @@ -116,7 +115,7 @@ def regexify(reg)
# with an array of values and selecting one of them.
def fetch(key)
fetched = sample(translate("faker.#{key}"))
if fetched&.match(%r{^\/}) && fetched&.match(%r{\/$}) # A regex
if fetched&.match(%r{^/}) && fetched&.match(%r{/$}) # A regex
regexify(fetched)
else
fetched
Expand All @@ -128,7 +127,7 @@ def fetch(key)
def fetch_all(key)
fetched = translate("faker.#{key}")
fetched = fetched.last if fetched.size <= 1
if !fetched.respond_to?(:sample) && fetched.match(%r{^\/}) && fetched.match(%r{\/$}) # A regex
if !fetched.respond_to?(:sample) && fetched.match(%r{^/}) && fetched.match(%r{/$}) # A regex
regexify(fetched)
else
fetched
Expand All @@ -140,7 +139,7 @@ def fetch_all(key)
# formatted translation: e.g., "#{first_name} #{last_name}".
def parse(key)
fetched = fetch(key)
parts = fetched.scan(/(\(?)#\{([A-Za-z]+\.)?([^\}]+)\}([^#]+)?/).map do |prefix, kls, meth, etc|
parts = fetched.scan(/(\(?)#\{([A-Za-z]+\.)?([^}]+)\}([^#]+)?/).map do |prefix, kls, meth, etc|
# If the token had a class Prefix (e.g., Name.first_name)
# grab the constant, otherwise use self
cls = kls ? Faker.const_get(kls.chop) : self
Expand Down Expand Up @@ -186,14 +185,14 @@ def translate(*args, **opts)
end

# Executes block with given locale set.
def with_locale(tmp_locale = nil)
def with_locale(tmp_locale = nil, &block)
Faker.load_i18n

current_locale = Faker::Config.own_locale
Faker::Config.locale = tmp_locale

disable_enforce_available_locales do
I18n.with_locale(tmp_locale) { yield }
I18n.with_locale(tmp_locale, &block)
end
ensure
Faker::Config.locale = current_locale
Expand Down
8 changes: 4 additions & 4 deletions lib/faker/blockchain/aeternity.rb
Expand Up @@ -14,7 +14,7 @@ class << self
# #=> "ak_zvU8YQLagjcfng7Tg8yCdiZ1rpiWNp1PBn3vtUs44utSvbJVR"
#
def address
'ak_' + rand_strings
"ak_#{rand_strings}"
end

##
Expand All @@ -27,7 +27,7 @@ def address
# #=> "th_147nDP22h3pHrLt2qykTH4txUwQh1ccaXp"
#
def transaction
'th_' + rand_strings(51)
"th_#{rand_strings(51)}"
end

##
Expand All @@ -40,7 +40,7 @@ def transaction
# #=> "ct_Hk2JsNeWGEYQEHHQCfcBeGrwbhtYSwFTPdDhW2SvjFYVojyhW"
#
def contract
'ct_' + rand_strings
"ct_#{rand_strings}"
end

##
Expand All @@ -53,7 +53,7 @@ def contract
# #=> "ok_28QDg7fkF5qiKueSdUvUBtCYPJdmMEoS73CztzXCRAwMGKHKZh"
#
def oracle
'ok_' + rand_strings(51)
"ok_#{rand_strings(51)}"
end

protected
Expand Down
4 changes: 2 additions & 2 deletions lib/faker/books/dune.rb
Expand Up @@ -89,7 +89,7 @@ def quote(legacy_character = NOT_GIVEN, character: nil)
end
end

fetch('dune.quotes.' + character)
fetch("dune.quotes.#{character}")
end

##
Expand Down Expand Up @@ -124,7 +124,7 @@ def saying(legacy_source = NOT_GIVEN, source: nil)
end
end

fetch('dune.sayings.' + source)
fetch("dune.sayings.#{source}")
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/faker/books/lovecraft.rb
Expand Up @@ -92,7 +92,7 @@ def sentence(legacy_word_count = NOT_GIVEN, legacy_random_words_to_add = NOT_GIV
keywords << :random_words_to_add if legacy_random_words_to_add != NOT_GIVEN
end

words(number: word_count + rand(random_words_to_add.to_i).to_i, spaces_allowed: open_compounds_allowed).join(' ').capitalize + '.'
"#{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 @@ -270,9 +270,9 @@ def paragraph_by_chars(legacy_characters = NOT_GIVEN, characters: 256)

paragraph = paragraph(sentence_count: 3)

paragraph += ' ' + paragraph(sentence_count: 3) while paragraph.length < characters
paragraph += " #{paragraph(sentence_count: 3)}" while paragraph.length < characters

paragraph[0...characters - 1] + '.'
"#{paragraph[0...characters - 1]}."
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/faker/default/address.rb
Expand Up @@ -54,7 +54,7 @@ def street_address(legacy_include_secondary = NOT_GIVEN, include_secondary: fals
keywords << :include_secondary if legacy_include_secondary != NOT_GIVEN
end

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

##
Expand Down Expand Up @@ -133,7 +133,7 @@ def zip_code(legacy_state_abbreviation = NOT_GIVEN, state_abbreviation: '')

# provide a zip code that is valid for the state provided
# see http://www.fincen.gov/forms/files/us_state_territory_zip_codes.pdf
bothify(fetch('address.postcode_by_state.' + state_abbreviation))
bothify(fetch("address.postcode_by_state.#{state_abbreviation}"))
end

##
Expand Down Expand Up @@ -247,7 +247,7 @@ def country_by_code(legacy_code = NOT_GIVEN, code: 'US')
keywords << :code if legacy_code != NOT_GIVEN
end

fetch('address.country_by_code.' + code)
fetch("address.country_by_code.#{code}")
end

##
Expand All @@ -265,7 +265,7 @@ def country_name_to_code(legacy_name = NOT_GIVEN, name: 'united_states')
keywords << :name if legacy_name != NOT_GIVEN
end

fetch('address.country_by_name.' + name)
fetch("address.country_by_name.#{name}")
end

##
Expand Down
8 changes: 3 additions & 5 deletions lib/faker/default/bank.rb
Expand Up @@ -140,15 +140,13 @@ def checksum(num_string)
def compile_routing_number
digit_one_two = %w[00 01 02 03 04 05 06 07 08 09 10 11 12]
((21..32).to_a + (61..72).to_a + [80]).each { |x| digit_one_two << x.to_s }
routing_num = digit_one_two.sample + rand_numstring + rand_numstring + rand_numstring + rand_numstring + rand_numstring + rand_numstring + rand_numstring
routing_num
digit_one_two.sample + rand_numstring + rand_numstring + rand_numstring + rand_numstring + rand_numstring + rand_numstring + rand_numstring
end

def compile_bsb_number
digit_one_two = %w[01 03 06 08 11 12 73 76 78 30]
state = (2..7).to_a.map(&:to_s).sample
bsb_num = digit_one_two.sample + state + rand_numstring + rand_numstring + rand_numstring
bsb_num
digit_one_two.sample + state + rand_numstring + rand_numstring + rand_numstring
end

# Calculates the mandatory checksum in 3rd and 4th characters in IBAN format
Expand Down Expand Up @@ -182,7 +180,7 @@ def compile_fraction(routing_num)
prefix = (1..50).to_a.map(&:to_s).sample
numerator = routing_num.split('')[5..8].join.to_i.to_s
denominator = routing_num.split('')[0..4].join.to_i.to_s
prefix + '-' + numerator + '/' + denominator
"#{prefix}-#{numerator}/#{denominator}"
end

def rand_numstring
Expand Down
6 changes: 3 additions & 3 deletions lib/faker/default/beer.rb
Expand Up @@ -80,7 +80,7 @@ def malts
#
# @faker.version 1.6.2
def ibu
rand(10..100).to_s + ' IBU'
"#{rand(10..100)} IBU"
end

##
Expand All @@ -93,7 +93,7 @@ def ibu
#
# @faker.version 1.6.2
def alcohol
rand(2.0..10.0).round(1).to_s + '%'
"#{rand(2.0..10.0).round(1)}%"
end

##
Expand All @@ -106,7 +106,7 @@ def alcohol
#
# @faker.version 1.6.2
def blg
rand(5.0..20.0).round(1).to_s + '°Blg'
"#{rand(5.0..20.0).round(1)}°Blg"
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/faker/default/chile_rut.rb
Expand Up @@ -46,9 +46,10 @@ def dv
partial_result
end
partial_check_digit = 11 - (digit_sum % 11)
if partial_check_digit == 11
case partial_check_digit
when 11
'0'
elsif partial_check_digit == 10
when 10
'k'
else
partial_check_digit.to_s
Expand Down
4 changes: 2 additions & 2 deletions lib/faker/default/code.rb
Expand Up @@ -160,7 +160,7 @@ def sin
partial = Array.new(7) { Faker::Config.random.rand(0..9) }.join

# Generate 9th digit
check_digit = generate_sin_check_digit(registry + partial + '0').to_s
check_digit = generate_sin_check_digit("#{registry}#{partial}0").to_s

registry + partial + check_digit
end
Expand Down Expand Up @@ -218,7 +218,7 @@ def generate_base10_isbn
def generate_base13_isbn
values = regexify(/\d{12}/)
remainder = sum(values) { |value, index| index.even? ? value.to_i : value.to_i * 3 } % 10
values << "-#{((10 - remainder) % 10)}"
values << "-#{(10 - remainder) % 10}"
end

def sum(values)
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/commerce.rb
Expand Up @@ -121,7 +121,7 @@ def price(legacy_range = NOT_GIVEN, legacy_as_string = NOT_GIVEN, range: 0..100.
price = (rand(range) * 100).floor / 100.0
if as_string
price_parts = price.to_s.split('.')
price = price_parts[0] + '.' + price_parts[-1].ljust(2, '0')
price = "#{price_parts[0]}.#{price_parts[-1].ljust(2, '0')}"
end
price
end
Expand Down
12 changes: 5 additions & 7 deletions lib/faker/default/company.rb
Expand Up @@ -464,13 +464,11 @@ def luhn_algorithm(number)
end
end

control_digit = if (sum % 10).zero?
0
else
(sum / 10 + 1) * 10 - sum
end

control_digit
if (sum % 10).zero?
0
else
(sum / 10 + 1) * 10 - sum
end
end

def abn_checksum(abn)
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/food.rb
Expand Up @@ -93,7 +93,7 @@ def spice
#
# @faker.version 1.7.0
def measurement
fetch('food.measurement_sizes') + ' ' + fetch('food.measurements')
"#{fetch('food.measurement_sizes')} #{fetch('food.measurements')}"
end

##
Expand Down
6 changes: 3 additions & 3 deletions lib/faker/default/hipster.rb
Expand Up @@ -82,7 +82,7 @@ def sentence(legacy_word_count = NOT_GIVEN, legacy_supplemental = NOT_GIVEN, leg
keywords << :random_words_to_add if legacy_random_words_to_add != NOT_GIVEN
end

words(number: word_count + rand(random_words_to_add.to_i).to_i, supplemental: supplemental, spaces_allowed: open_compounds_allowed).join(' ').capitalize + '.'
"#{words(number: word_count + rand(random_words_to_add.to_i).to_i, supplemental: supplemental, spaces_allowed: open_compounds_allowed).join(' ').capitalize}."
end

##
Expand Down Expand Up @@ -185,9 +185,9 @@ def paragraph_by_chars(legacy_characters = NOT_GIVEN, legacy_supplemental = NOT_

paragraph = paragraph(sentence_count: 3, supplemental: supplemental)

paragraph += ' ' + paragraph(sentence_count: 3, supplemental: supplemental) while paragraph.length < characters
paragraph += " #{paragraph(sentence_count: 3, supplemental: supplemental)}" while paragraph.length < characters

paragraph[0...characters - 1] + '.'
"#{paragraph[0...characters - 1]}."
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/faker/default/id_number.rb
Expand Up @@ -197,7 +197,7 @@ def chilean_id
digits = Faker::Number.number(digits: 8)
verification_code = chilean_verification_code(digits)

digits.to_s + '-' + verification_code.to_s
"#{digits}-#{verification_code}"
end

private
Expand Down Expand Up @@ -256,7 +256,7 @@ def brazilian_document_checksum(digits)
end * 10
end

def brazilian_document_digit(checksum, id = false)
def brazilian_document_digit(checksum, id: false)
remainder = checksum % 11
id ? brazilian_id_digit(remainder) : brazilian_citizen_number_digit(remainder)
end
Expand Down
7 changes: 4 additions & 3 deletions lib/faker/default/internet.rb
Expand Up @@ -37,7 +37,7 @@ def safe_email(legacy_name = NOT_GIVEN, name: nil)

construct_email(
sanitize_email_local_part(username(specifier: name)),
'example.' + sample(%w[org com net])
"example.#{sample(%w[org com net])}"
)
end

Expand All @@ -50,7 +50,8 @@ def username(legacy_specifier = NOT_GIVEN, legacy_separators = NOT_GIVEN, specif
with_locale(:en) do
return shuffle(specifier.scan(/[[:word:]]+/)).join(sample(separators)).downcase if specifier.respond_to?(:scan)

if specifier.is_a?(Integer)
case specifier
when Integer
# If specifier is Integer and has large value, Argument error exception is raised to overcome memory full error
raise ArgumentError, 'Given argument is too large' if specifier > 10**6

Expand All @@ -62,7 +63,7 @@ def username(legacy_specifier = NOT_GIVEN, legacy_separators = NOT_GIVEN, specif
break unless result.length < specifier && tries < 7
end
return result * (specifier / result.length + 1) if specifier.positive?
elsif specifier.is_a?(Range)
when Range
tries = 0
result = nil
loop do
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/invoice.rb
Expand Up @@ -43,7 +43,7 @@ def creditor_reference(legacy_ref = NOT_GIVEN, ref: '')

ref = reference if ref.empty?

'RF' + iban_checksum('RF', ref) + ref
"RF#{iban_checksum('RF', ref)}#{ref}"
end

##
Expand Down

0 comments on commit 7046990

Please sign in to comment.