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

Refactor Faker::IDNumber to Faker::IdNumber to be more consistent with other generator's naming convention. #2858

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main
- [Faker::Hipster](doc/default/hipster.md)
- [Faker::Hobby](doc/default/hobby.md)
- [Faker::House](doc/default/house.md)
- [Faker::IDNumber](doc/default/id_number.md)
- [Faker::IdNumber](doc/default/id_number.md)
- [Faker::IndustrySegments](doc/default/industry_segments.md)
- [Faker::Internet](doc/default/internet.md)
- [Faker::Invoice](doc/default/invoice.md)
Expand Down
40 changes: 20 additions & 20 deletions doc/default/id_number.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
# Faker::IDNumber
# Faker::IdNumber

```ruby
# Generate a valid US Social Security number
Faker::IDNumber.valid #=> "552-56-3593"
Faker::IdNumber.valid #=> "552-56-3593"

# Generate an invalid US Social Security number
Faker::IDNumber.invalid #=> "311-72-0000"
Faker::IdNumber.invalid #=> "311-72-0000"

# Generate a Spanish citizen identifier (DNI)
Faker::IDNumber.spanish_citizen_number #=> "53290236-H"
Faker::IdNumber.spanish_citizen_number #=> "53290236-H"

# Generate a Spanish foreign born citizen identifier (NIE)
Faker::IDNumber.spanish_foreign_citizen_number #=> "Z-1600870-Y"
Faker::IdNumber.spanish_foreign_citizen_number #=> "Z-1600870-Y"

# Generate a valid South African ID Number
Faker::IDNumber.south_african_id_number #=> "8105128870184"
Faker::IdNumber.south_african_id_number #=> "8105128870184"
# or
Faker::IDNumber.valid_south_african_id_number #=> "8105128870184"
Faker::IdNumber.valid_south_african_id_number #=> "8105128870184"

# Generate an invalid South African ID Number
Faker::IDNumber.invalid_south_african_id_number #=> "1642972065088"
Faker::IdNumber.invalid_south_african_id_number #=> "1642972065088"

# Generate a Brazilian citizen number (CPF)
# Keyword arguments: formatted
Faker::IDNumber.brazilian_citizen_number #=> "53540542221"
Faker::IDNumber.brazilian_citizen_number(formatted: true) #=> "535.405.422-21"
Faker::IdNumber.brazilian_citizen_number #=> "53540542221"
Faker::IdNumber.brazilian_citizen_number(formatted: true) #=> "535.405.422-21"

# Generate a Brazilian ID Number (RG)
# Keyword arguments: formatted
Faker::IDNumber.brazilian_id #=> "493054029"
Faker::IDNumber.brazilian_id(formatted: true) #=> "49.305.402-9"
Faker::IdNumber.brazilian_id #=> "493054029"
Faker::IdNumber.brazilian_id(formatted: true) #=> "49.305.402-9"

# Generate a Chilean ID (Rut with 8 digits)
# For more advanced cases, please refer to Faker::ChileRut
Faker::IDNumber.chilean_id #=> "15620613-K"
Faker::IdNumber.chilean_id #=> "15620613-K"

# Generate a Croatian ID number (OIB)
# Keyword arguments: international
Faker::IDNumber.croatian_id #=> "88467617508"
Faker::IDNumber.croatian_id(international: true) #=> "HR88467617508"
Faker::IdNumber.croatian_id #=> "88467617508"
Faker::IdNumber.croatian_id(international: true) #=> "HR88467617508"

# Generate a Danish ID number (CPR)
# Keyword arguments: formatted, gender, birthday
Faker::IDNumber.danish_id_number #=> "050390-9980"
Faker::IDNumber.danish_id_number(formatted: true) #=> "050390-9980"
Faker::IDNumber.danish_id_number(birthday: Date.new(1990, 3, 5)) #=> "050390-9980"
Faker::IDNumber.danish_id_number(gender: :female) #=> "050390-9980"
Faker::IdNumber.danish_id_number #=> "050390-9980"
Faker::IdNumber.danish_id_number(formatted: true) #=> "050390-9980"
Faker::IdNumber.danish_id_number(birthday: Date.new(1990, 3, 5)) #=> "050390-9980"
Faker::IdNumber.danish_id_number(gender: :female) #=> "050390-9980"

# Generate a valid French Social Security number (INSEE number)
Faker::IDNumber.french_insee_number #=> "22510589696868"
Faker::IdNumber.french_insee_number #=> "22510589696868"
```
40 changes: 20 additions & 20 deletions lib/faker/default/id_number.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Faker
class IDNumber < Base
class IdNumber < Base
CHECKS = 'TRWAGMYFPDXBNJZSQVHLCKE'
INVALID_SSN = [
/0{3}-\d{2}-\d{4}/,
Expand All @@ -25,7 +25,7 @@ class << self
# @return [String]
#
# @example
# Faker::IDNumber.valid #=> "552-56-3593"
# Faker::IdNumber.valid #=> "552-56-3593"
#
# @faker.version 1.6.0
def valid
Expand All @@ -38,7 +38,7 @@ def valid
# @return [String]
#
# @example
# Faker::IDNumber.invalid #=> "311-72-0000"
# Faker::IdNumber.invalid #=> "311-72-0000"
#
# @faker.version 1.6.0
def invalid
Expand Down Expand Up @@ -71,7 +71,7 @@ def ssn_valid
# @return [String]
#
# @example
# Faker::IDNumber.spanish_citizen_number #=> "53290236-H"
# Faker::IdNumber.spanish_citizen_number #=> "53290236-H"
#
# @faker.version 1.9.0
def spanish_citizen_number
Expand All @@ -87,7 +87,7 @@ def spanish_citizen_number
# @return [String]
#
# @example
# Faker::IDNumber.spanish_foreign_citizen_number #=> "Z-1600870-Y"
# Faker::IdNumber.spanish_foreign_citizen_number #=> "Z-1600870-Y"
#
# @faker.version 1.9.0
def spanish_foreign_citizen_number
Expand All @@ -106,8 +106,8 @@ def spanish_foreign_citizen_number
# @return [String]
#
# @example
# Faker::IDNumber.south_african_id_number #=> "8105128870184"
# Faker::IDNumber.valid_south_african_id_number #=> "8105128870184"
# Faker::IdNumber.south_african_id_number #=> "8105128870184"
# Faker::IdNumber.valid_south_african_id_number #=> "8105128870184"
#
# @faker.version 1.9.2
def valid_south_african_id_number
Expand All @@ -129,7 +129,7 @@ def valid_south_african_id_number
# @return [String]
#
# @example
# Faker::IDNumber.invalid_south_african_id_number #=> "1642972065088"
# Faker::IdNumber.invalid_south_african_id_number #=> "1642972065088"
#
# @faker.version 1.9.2
def invalid_south_african_id_number
Expand All @@ -156,8 +156,8 @@ def invalid_south_african_id_number
# @return [String]
#
# @example
# Faker::IDNumber.brazilian_citizen_number #=> "53540542221"
# Faker::IDNumber.brazilian_citizen_number(formatted: true) #=> "535.405.422-21"
# Faker::IdNumber.brazilian_citizen_number #=> "53540542221"
# Faker::IdNumber.brazilian_citizen_number(formatted: true) #=> "535.405.422-21"
#
# @faker.version 1.9.2
def brazilian_citizen_number(formatted: false)
Expand All @@ -177,8 +177,8 @@ def brazilian_citizen_number(formatted: false)
# @return [String]
#
# @example
# Faker::IDNumber.brazilian_id #=> "493054029"
# Faker::IDNumber.brazilian_id(formatted: true) #=> "49.305.402-9"
# Faker::IdNumber.brazilian_id #=> "493054029"
# Faker::IdNumber.brazilian_id(formatted: true) #=> "49.305.402-9"
#
# @faker.version 2.1.2
def brazilian_id(formatted: false)
Expand All @@ -196,7 +196,7 @@ def brazilian_id(formatted: false)
# @return [String]
#
# @example
# Faker::IDNumber.chilean_id #=> "15620613-K"
# Faker::IdNumber.chilean_id #=> "15620613-K"
#
# @faker.version 2.1.2
def chilean_id
Expand All @@ -213,8 +213,8 @@ def chilean_id
# @return [String]
#
# @example
# Faker::IDNumber.croatian_id #=> "88467617508"
# Faker::IDNumber.croatian_id(international: true) #=> "HR88467617508"
# Faker::IdNumber.croatian_id #=> "88467617508"
# Faker::IdNumber.croatian_id(international: true) #=> "HR88467617508"
#
# @faker.version next
def croatian_id(international: false)
Expand All @@ -238,10 +238,10 @@ def croatian_id(international: false)
# @return [String]
#
# @example
# Faker::IDNumber.danish_id_number #=> "0503909980"
# Faker::IDNumber.danish_id_number(formatted: true) #=> "050390-9980"
# Faker::IDNumber.danish_id_number(birthday: Date.new(1990, 3, 5)) #=> "0503909980"
# Faker::IDNumber.danish_id_number(gender: :female) #=> "0503909980"
# Faker::IdNumber.danish_id_number #=> "0503909980"
# Faker::IdNumber.danish_id_number(formatted: true) #=> "050390-9980"
# Faker::IdNumber.danish_id_number(birthday: Date.new(1990, 3, 5)) #=> "0503909980"
# Faker::IdNumber.danish_id_number(gender: :female) #=> "0503909980"
#
# @faker.version next
def danish_id_number(formatted: false, birthday: Faker::Date.birthday, gender: nil)
Expand Down Expand Up @@ -275,7 +275,7 @@ def danish_id_number(formatted: false, birthday: Faker::Date.birthday, gender: n
# @return [String]
#
# @example
# Faker::IDNumber.french_insee_number #=> "53290236-H"
# Faker::IdNumber.french_insee_number #=> "53290236-H"
#
# @faker.version next
def french_insee_number
Expand Down
6 changes: 3 additions & 3 deletions lib/faker/default/south_africa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class << self
#
# @faker.version 1.9.2
def id_number
Faker::IDNumber.south_african_id_number
Faker::IdNumber.south_african_id_number
end

##
Expand All @@ -26,7 +26,7 @@ def id_number
#
# @faker.version 1.9.2
def valid_id_number
Faker::IDNumber.valid_south_african_id_number
Faker::IdNumber.valid_south_african_id_number
end

##
Expand All @@ -39,7 +39,7 @@ def valid_id_number
#
# @faker.version 1.9.2
def invalid_id_number
Faker::IDNumber.invalid_south_african_id_number
Faker::IdNumber.invalid_south_african_id_number
end

##
Expand Down
27 changes: 27 additions & 0 deletions lib/helpers/deprecated_class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Faker
def self.const_missing(const_name)
new_class = DeprecatedClass.names[const_name.to_sym]
super(const_name) unless new_class
warn "DEPRECATION WARNING: the class Faker::#{const_name} is deprecated. Use Faker::#{new_class} instead."
Object.const_get("Faker::#{new_class}")
end

class Movies
def self.const_missing(const_name)
Faker.const_missing("Movies::#{const_name}")
end
end

class DeprecatedClass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of naming this Deprecator? I find it a better name because class is not adding too much to the name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking if we can do something like how ActiveSupport does but a simplified version for deprecating generators: https://github.com/rails/rails/blob/main/activesupport/lib/active_support/deprecation/constant_accessor.rb

It would be a module mixin that would add the const_missing and intercept calls to the deprecated module. The other mixin method would let you register the deprecated and the new modules.

If we could this in a module to be included, we can just add it to the generator to deprecate_constant passing the generator and the replacement, ie:

# id_number.rb

module Faker
  class IdNumber < Base
    include Faker::Deprecator

    deprecate_constant(Faker::IDNumber, Faker::IdNumber)
  end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this is a good idea, will implement it that way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thank you! That would fit nicely with the others to be deprecated. Let us know if you need any help.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @stefannibrasil ,
Sorry for the taking so long on this but haven't had a chance to touch this till this morning.
I am not able to figure out a way to intercept the const_missing calls to the Faker module itself. i.e Faker::Constant_name. I tried to prepend the extension/module to the Faker module itself but that doesn't seem to work. will continue to look at it but interested to hear if you have way to get around that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Jamal-A-Mohamed thank you for your patience! I was almost there and @thdaraujo helped me with the final details. Here's how we could make it work: https://github.com/faker-ruby/faker/compare/main...hexdevs:faker:deprecator?expand=1

Not sure if you can cherry-pick this commit 8f54cbe to this branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Great Job to you both.
I cherry picked that from your guy's commit and pushed it up.
I would need to re-read to see what I was missing.
Let me know if you want me to squash commits and or write more tests.

Copy link
Contributor

@stefannibrasil stefannibrasil Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay! Okay, almost there. I was testing some things on another project and pushed just one more commit here: efe80d0

Could you cherry-pick it again? That would be the only thing left before merging this. Thank you for your patience and your work on this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you for helping out on this one! 🎉

# rubocop:disable Lint/SymbolConversion
def self.names
{
'IDNumber': 'IdNumber',
'Movies::HarryPotterDeprecated': 'Movies::HarryPotter'
}
end
# rubocop:enable Lint/SymbolConversion
end
end
2 changes: 1 addition & 1 deletion lib/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6960,7 +6960,7 @@ en-US:
- "#{PhoneNumber.area_code}-#{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
- "#{PhoneNumber.area_code}.#{PhoneNumber.exchange_code}.#{PhoneNumber.subscriber_number}"
id_number:
valid: "#{IDNumber.ssn_valid}"
valid: "#{IdNumber.ssn_valid}"
invalid:
- 000-##-####
- "###-00-####"
Expand Down
2 changes: 1 addition & 1 deletion lib/locales/en/id_number.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
en:
faker:
id_number:
valid: "#{IDNumber.ssn_valid}"
valid: "#{IdNumber.ssn_valid}"
invalid: ['000-##-####', '###-00-####', '###-##-0000', '666-##-####', '9##-##-####']
27 changes: 27 additions & 0 deletions test/faker/default/test_faker_deprecated_class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require_relative '../../test_helper'

class TestFakerDeprecatedClass < Test::Unit::TestCase
def setup
@tester = Faker::DeprecatedClass
end

def test_non_nested_class_with_deprecated_class
actual_stdout, actual_stderr = capture_output do
Faker::IDNumber.valid
end

assert_empty actual_stdout
assert_match('DEPRECATION WARNING: the class Faker::IDNumber is deprecated. Use Faker::IdNumber instead.', actual_stderr)
end

def test_nested_class_with_deprecated_class
actual_stdout, actual_stderr = capture_output do
Faker::Movies::HarryPotterDeprecated.character
end

assert_empty actual_stdout
assert_match('DEPRECATION WARNING: the class Faker::Movies::HarryPotterDeprecated is deprecated. Use Faker::Movies::HarryPotter instead.', actual_stderr)
end
end
Loading