diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml
index 283959e203..74e9ae0071 100644
--- a/.github/workflows/ruby.yml
+++ b/.github/workflows/ruby.yml
@@ -9,9 +9,13 @@ name: Tests
on:
push:
- branches: [ main ]
+ branches:
+ - main
+ - 'lazy-loading-beta'
pull_request:
- branches: [ main ]
+ branches:
+ - main
+ - 'lazy-loading-beta'
workflow_dispatch:
permissions:
@@ -59,3 +63,18 @@ jobs:
- name: Run tests
run: bundle exec rake test
+ test-with-lazy-loading:
+ name: Ruby 3.2 with lazy loading
+ runs-on: ubuntu-latest
+ env:
+ BUNDLE_WITHOUT: benchmark
+ steps:
+ - uses: actions/checkout@v6
+ - uses: ruby/setup-ruby@v1
+ with:
+ ruby-version: '3.2'
+ bundler-cache: true
+
+ - name: Run tests with lazy load enabled
+ run: FAKER_LAZY_LOAD=1 bundle exec rake test
+
diff --git a/lib/faker.rb b/lib/faker.rb
index dc02e4977c..a845eb817c 100644
--- a/lib/faker.rb
+++ b/lib/faker.rb
@@ -40,7 +40,11 @@ def random
end
def lazy_loading?
- Thread.current[:faker_lazy_loading] == true
+ if ENV.key?('FAKER_LAZY_LOAD') && !ENV['FAKER_LAZY_LOAD'].nil?
+ ENV['FAKER_LAZY_LOAD'] == '1'
+ else
+ Thread.current[:faker_lazy_loading] == true
+ end
end
def lazy_loading=(value)
@@ -283,7 +287,47 @@ def disable_enforce_available_locales
end
end
end
+
+ if Faker::Config.lazy_loading?
+ def self.load_path(*constants)
+ constants.map do |class_name|
+ class_name
+ .to_s
+ .gsub('::', '/')
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
+ .tr('-', '_')
+ .downcase
+ end.join('/')
+ end
+
+ def self.lazy_load(klass)
+ def klass.const_missing(class_name)
+ load_path = case class_name
+ when :DnD
+ Faker.load_path('faker/games/dnd')
+ else
+ Faker.load_path(name, class_name)
+ end
+
+ begin
+ require(load_path)
+ rescue LoadError
+ require(load_path.gsub('faker/', 'faker/default/'))
+ end
+
+ const_get(class_name)
+ end
+ end
+
+ lazy_load(self)
+ end
end
-# require faker objects
-Dir.glob(File.join(mydir, 'faker', '/**/*.rb')).each { |file| require file }
+unless Faker::Config.lazy_loading?
+ rb_files = []
+ rb_files << File.join(mydir, 'faker', '*.rb')
+ rb_files << File.join(mydir, 'faker', '/**/*.rb')
+
+ Dir.glob(rb_files).each { |file| require file }
+end
diff --git a/lib/faker/blockchain.rb b/lib/faker/blockchain.rb
new file mode 100644
index 0000000000..2b1d76ff9a
--- /dev/null
+++ b/lib/faker/blockchain.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class Blockchain
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/books.rb b/lib/faker/books.rb
new file mode 100644
index 0000000000..8a958e2b4b
--- /dev/null
+++ b/lib/faker/books.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class Books
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/creature.rb b/lib/faker/creature.rb
new file mode 100644
index 0000000000..1bc20c69cb
--- /dev/null
+++ b/lib/faker/creature.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class Creature
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/default.rb b/lib/faker/default.rb
new file mode 100644
index 0000000000..3c55a62119
--- /dev/null
+++ b/lib/faker/default.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class Default
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/books/book.rb b/lib/faker/default/book.rb
similarity index 100%
rename from lib/faker/books/book.rb
rename to lib/faker/default/book.rb
diff --git a/lib/faker/games/game.rb b/lib/faker/default/game.rb
similarity index 100%
rename from lib/faker/games/game.rb
rename to lib/faker/default/game.rb
diff --git a/lib/faker/default/html.rb b/lib/faker/default/html.rb
index a78be30ea4..bf94c2b1d9 100644
--- a/lib/faker/default/html.rb
+++ b/lib/faker/default/html.rb
@@ -14,7 +14,7 @@ class << self
# @faker.version 3.2.1
def heading
level = rand(1..6)
- "#{Lorem.word.capitalize}"
+ "#{Faker::Lorem.word.capitalize}"
end
##
@@ -97,7 +97,7 @@ def unordered_list
#
# @faker.version 3.2.1
def code
- "#{Lorem.sentence(word_count: 1)}"
+ "#{Faker::Lorem.sentence(word_count: 1)}"
end
##
@@ -165,7 +165,7 @@ def link(rel: 'stylesheet')
# Faker::HTML.element(tag: 'div', content: "This is a div with XSS attributes.", attributes: {class: 'xss', onclick: "alert('XSS')"}) #=> "
This is a div with XSS attributes.
"
#
# @faker.version 3.2.1
- def element(tag: 'div', content: Lorem.sentence(word_count: 3), attributes: { class: Lorem.word, onclick: "#{Lorem.word}()" })
+ def element(tag: 'div', content: Faker::Lorem.sentence(word_count: 3), attributes: { class: Faker::Lorem.word, onclick: "#{Faker::Lorem.word}()" })
attribute_string = attributes.map { |key, value| "#{key}=\"#{value}\"" }.join(' ')
"<#{tag} #{attribute_string}>#{content}#{tag}>"
end
diff --git a/lib/faker/default/lorem.rb b/lib/faker/default/lorem.rb
index 1fbd9f4139..69e09f0584 100644
--- a/lib/faker/default/lorem.rb
+++ b/lib/faker/default/lorem.rb
@@ -58,7 +58,7 @@ def words(number: 3, supplemental: false, exclude_words: nil)
#
# @faker.version 2.1.3
def character
- sample(Types::CHARACTERS)
+ sample(Faker::Types::CHARACTERS)
end
##
@@ -78,7 +78,7 @@ def character
#
# @faker.version 2.1.3
def characters(number: 255, min_alpha: 0, min_numeric: 0)
- Alphanumeric.alphanumeric(number: number, min_alpha: min_alpha, min_numeric: min_numeric)
+ Faker::Alphanumeric.alphanumeric(number: number, min_alpha: min_alpha, min_numeric: min_numeric)
end
##
diff --git a/lib/faker/default/markdown.rb b/lib/faker/default/markdown.rb
index 1f12e0f435..e591679123 100644
--- a/lib/faker/default/markdown.rb
+++ b/lib/faker/default/markdown.rb
@@ -13,7 +13,7 @@ class << self
#
# @faker.version 1.8.0
def headers
- "#{fetch('markdown.headers')} #{Lorem.word.capitalize}"
+ "#{fetch('markdown.headers')} #{Faker::Lorem.word.capitalize}"
end
##
@@ -95,7 +95,7 @@ def inline_code
#
# @faker.version 1.8.0
def block_code
- "```ruby\n#{Lorem.sentence(word_count: 1)}\n```"
+ "```ruby\n#{Faker::Lorem.sentence(word_count: 1)}\n```"
end
##
@@ -110,7 +110,7 @@ def block_code
def table
table = []
3.times do
- table << "#{Lorem.word} | #{Lorem.word} | #{Lorem.word}"
+ table << "#{Faker::Lorem.word} | #{Faker::Lorem.word} | #{Faker::Lorem.word}"
end
table.insert(1, '---- | ---- | ----')
table.join("\n")
diff --git a/lib/faker/movies/movie.rb b/lib/faker/default/movie.rb
similarity index 100%
rename from lib/faker/movies/movie.rb
rename to lib/faker/default/movie.rb
diff --git a/lib/faker/default/omniauth.rb b/lib/faker/default/omniauth.rb
index 486152dd31..567c6bdf88 100644
--- a/lib/faker/default/omniauth.rb
+++ b/lib/faker/default/omniauth.rb
@@ -11,8 +11,8 @@ class Omniauth < Base
def initialize(name: nil, email: nil)
super()
- @name = name || "#{Name.first_name} #{Name.last_name}"
- @email = email || Internet.email(name: self.name)
+ @name = name || "#{Faker::Name.first_name} #{Faker::Name.last_name}"
+ @email = email || Faker::Internet.email(name: self.name)
@first_name, @last_name = self.name.split
end
@@ -27,8 +27,8 @@ class << self
# @return [Hash] An auth hash in the format provided by omniauth-google.
#
# @faker.version 1.8.0
- def google(name: nil, email: nil, uid: Number.number(digits: 9).to_s)
- auth = Omniauth.new(name: name, email: email)
+ def google(name: nil, email: nil, uid: Faker::Number.number(digits: 9).to_s)
+ auth = new(name: name, email: email)
{
provider: 'google_oauth2',
uid: uid,
@@ -40,9 +40,9 @@ def google(name: nil, email: nil, uid: Number.number(digits: 9).to_s)
image: image
},
credentials: {
- token: Crypto.md5,
- refresh_token: Crypto.md5,
- expires_at: Time.forward.to_i,
+ token: Faker::Crypto.md5,
+ refresh_token: Faker::Crypto.md5,
+ expires_at: Faker::Time.forward.to_i,
expires: true
},
extra: {
@@ -56,20 +56,20 @@ def google(name: nil, email: nil, uid: Number.number(digits: 9).to_s)
profile: "https://plus.google.com/#{uid}",
picture: image,
gender: gender,
- birthday: Date.backward(days: 36_400).strftime('%Y-%m-%d'),
+ birthday: Faker::Date.backward(days: 36_400).strftime('%Y-%m-%d'),
locale: 'en',
- hd: "#{Company.name.downcase}.com"
+ hd: "#{Faker::Company.name.downcase}.com"
},
id_info: {
iss: 'accounts.google.com',
- at_hash: Crypto.md5,
+ at_hash: Faker::Crypto.md5,
email_verified: true,
- sub: Number.number(digits: 28).to_s,
+ sub: Faker::Number.number(digits: 28).to_s,
azp: 'APP_ID',
email: auth.email,
aud: 'APP_ID',
- iat: Time.forward.to_i,
- exp: Time.forward.to_i,
+ iat: Faker::Time.forward.to_i,
+ exp: Faker::Time.forward.to_i,
openid_id: "https://www.google.com/accounts/o8/id?id=#{uid}"
}
}
@@ -87,8 +87,8 @@ def google(name: nil, email: nil, uid: Number.number(digits: 9).to_s)
# @return [Hash] An auth hash in the format provided by omniauth-facebook.
#
# @faker.version 1.8.0
- def facebook(name: nil, email: nil, username: nil, uid: Number.number(digits: 7).to_s)
- auth = Omniauth.new(name: name, email: email)
+ def facebook(name: nil, email: nil, username: nil, uid: Faker::Number.number(digits: 7).to_s)
+ auth = new(name: name, email: email)
username ||= "#{auth.first_name.downcase[0]}#{auth.last_name.downcase}"
{
provider: 'facebook',
@@ -102,7 +102,7 @@ def facebook(name: nil, email: nil, username: nil, uid: Number.number(digits: 7)
verified: random_boolean
},
credentials: {
- token: Crypto.md5,
+ token: Faker::Crypto.md5,
expires_at: Time.forward.to_i,
expires: true
},
@@ -115,7 +115,7 @@ def facebook(name: nil, email: nil, username: nil, uid: Number.number(digits: 7)
link: "http://www.facebook.com/#{username}",
username: username,
location: {
- id: Number.number(digits: 9).to_s,
+ id: Faker::Number.number(digits: 9).to_s,
name: city_state
},
gender: gender,
@@ -123,7 +123,7 @@ def facebook(name: nil, email: nil, username: nil, uid: Number.number(digits: 7)
timezone: timezone,
locale: 'en_US',
verified: random_boolean,
- updated_time: Time.backward.iso8601
+ updated_time: Faker::Time.backward.iso8601
}
}
}
@@ -139,11 +139,11 @@ def facebook(name: nil, email: nil, username: nil, uid: Number.number(digits: 7)
# @return [Hash] An auth hash in the format provided by omniauth-twitter.
#
# @faker.version 1.8.0
- def twitter(name: nil, nickname: nil, uid: Number.number(digits: 6).to_s)
- auth = Omniauth.new(name: name)
+ def twitter(name: nil, nickname: nil, uid: Faker::Number.number(digits: 6).to_s)
+ auth = new(name: name)
nickname ||= auth.name.downcase.delete(' ')
location = city_state
- description = Lorem.sentence
+ description = Faker::Lorem.sentence
{
provider: 'twitter',
uid: uid,
@@ -159,26 +159,26 @@ def twitter(name: nil, nickname: nil, uid: Number.number(digits: 6).to_s)
}
},
credentials: {
- token: Crypto.md5,
- secret: Crypto.md5
+ token: Faker::Crypto.md5,
+ secret: Faker::Crypto.md5
},
extra: {
access_token: '',
raw_info: {
name: auth.name,
listed_count: random_number_from_range(1..10),
- profile_sidebar_border_color: Color.hex_color,
+ profile_sidebar_border_color: Faker::Color.hex_color,
url: nil,
lang: 'en',
statuses_count: random_number_from_range(1..1000),
profile_image_url: image,
profile_background_image_url_https: image,
location: location,
- time_zone: Address.city,
+ time_zone: Faker::Address.city,
follow_request_sent: random_boolean,
id: uid,
profile_background_tile: random_boolean,
- profile_sidebar_fill_color: Color.hex_color,
+ profile_sidebar_fill_color: Faker::Color.hex_color,
followers_count: random_number_from_range(1..10_000),
default_profile_image: random_boolean,
screen_name: '',
@@ -186,7 +186,7 @@ def twitter(name: nil, nickname: nil, uid: Number.number(digits: 6).to_s)
utc_offset: timezone,
verified: random_boolean,
favourites_count: random_number_from_range(1..10),
- profile_background_color: Color.hex_color,
+ profile_background_color: Faker::Color.hex_color,
is_translator: random_boolean,
friends_count: random_number_from_range(1..10_000),
notifications: random_boolean,
@@ -194,8 +194,8 @@ def twitter(name: nil, nickname: nil, uid: Number.number(digits: 6).to_s)
profile_background_image_url: image,
protected: random_boolean,
description: description,
- profile_link_color: Color.hex_color,
- created_at: Time.backward.strftime('%a %b %d %H:%M:%S %z %Y'),
+ profile_link_color: Faker::Color.hex_color,
+ created_at: Faker::Time.backward.strftime('%a %b %d %H:%M:%S %z %Y'),
id_str: uid,
profile_image_url_https: image,
default_profile: random_boolean,
@@ -222,15 +222,15 @@ def twitter(name: nil, nickname: nil, uid: Number.number(digits: 6).to_s)
# @return [Hash] An auth hash in the format provided by omniauth-linkedin.
#
# @faker.version 1.8.0
- def linkedin(name: nil, email: nil, uid: Number.number(digits: 6).to_s)
+ def linkedin(name: nil, email: nil, uid: Faker::Number.number(digits: 6).to_s)
auth = Omniauth.new(name: name, email: email)
first_name = auth.first_name.downcase
last_name = auth.last_name.downcase
location = city_state
- description = Lorem.sentence
- token = Crypto.md5
- secret = Crypto.md5
- industry = Commerce.department
+ description = Faker::Lorem.sentence
+ token = Faker::Crypto.md5
+ secret = Faker::Crypto.md5
+ industry = Faker::Commerce.department
url = "http://www.linkedin.com/in/#{first_name}#{last_name}"
{
provider: 'linkedin',
@@ -244,7 +244,7 @@ def linkedin(name: nil, email: nil, uid: Number.number(digits: 6).to_s)
location: location,
description: description,
image: image,
- phone: PhoneNumber.phone_number,
+ phone: Faker::PhoneNumber.phone_number,
headline: description,
industry: industry,
urls: {
@@ -275,7 +275,7 @@ def linkedin(name: nil, email: nil, uid: Number.number(digits: 6).to_s)
industry: industry,
lastName: auth.last_name,
location: {
- country: { code: Address.country_code.downcase },
+ country: { code: Faker::Address.country_code.downcase },
name: city_state.split(', ').first
},
pictureUrl: image,
@@ -295,8 +295,8 @@ def linkedin(name: nil, email: nil, uid: Number.number(digits: 6).to_s)
# @return [Hash] An auth hash in the format provided by omniauth-github.
#
# @faker.version 1.8.0
- def github(name: nil, email: nil, uid: Number.number(digits: 8).to_s)
- auth = Omniauth.new(name: name, email: email)
+ def github(name: nil, email: nil, uid: Faker::Number.number(digits: 8).to_s)
+ auth = new(name: name, email: email)
login = auth.name.downcase.tr(' ', '-')
html_url = "https://github.com/#{login}"
api_url = "https://api.github.com/users/#{login}"
@@ -346,8 +346,8 @@ def github(name: nil, email: nil, uid: Number.number(digits: 8).to_s)
public_gists: random_number_from_range(1..1000),
followers: random_number_from_range(1..1000),
following: random_number_from_range(1..1000),
- created_at: Time.backward(days: 36_400).iso8601,
- updated_at: Time.backward(days: 2).iso8601
+ created_at: Faker::Time.backward(days: 36_400).iso8601,
+ updated_at: Faker::Time.backward(days: 2).iso8601
}
}
}
@@ -364,8 +364,8 @@ def github(name: nil, email: nil, uid: Number.number(digits: 8).to_s)
#
# @faker.version 2.3.0
def apple(name: nil, email: nil, uid: nil)
- uid ||= "#{Number.number(digits: 6)}.#{Number.hexadecimal(digits: 32)}.#{Number.number(digits: 4)}"
- auth = Omniauth.new(name: name, email: email)
+ uid ||= "#{Faker::Number.number(digits: 6)}.#{Faker::Number.hexadecimal(digits: 32)}.#{Faker::Number.number(digits: 4)}"
+ auth = new(name: name, email: email)
{
provider: 'apple',
uid: uid,
@@ -376,20 +376,20 @@ def apple(name: nil, email: nil, uid: nil)
last_name: auth.last_name
},
credentials: {
- token: Crypto.md5,
- refresh_token: Crypto.md5,
- expires_at: Time.forward.to_i,
+ token: Faker::Crypto.md5,
+ refresh_token: Faker::Crypto.md5,
+ expires_at: Faker::Time.forward.to_i,
expires: true
},
extra: {
raw_info: {
iss: 'https://appleid.apple.com',
aud: 'CLIENT_ID',
- exp: Time.forward.to_i,
- iat: Time.forward.to_i,
+ exp: Faker::Time.forward.to_i,
+ iat: Faker::Time.forward.to_i,
sub: uid,
- at_hash: Crypto.md5,
- auth_time: Time.forward.to_i,
+ at_hash: Faker::Crypto.md5,
+ auth_time: Faker::Time.forward.to_i,
email: auth.email,
email_verified: true
}
@@ -408,8 +408,8 @@ def apple(name: nil, email: nil, uid: nil)
#
# @faker.version next
def auth0(name: nil, email: nil, uid: nil)
- uid ||= "auth0|#{Number.hexadecimal(digits: 24)}"
- auth = Omniauth.new(name: name, email: email)
+ uid ||= "auth0|#{Faker::Number.hexadecimal(digits: 24)}"
+ auth = new(name: name, email: email)
{
provider: 'auth0',
uid: uid,
@@ -423,9 +423,9 @@ def auth0(name: nil, email: nil, uid: nil)
expires_at: Time.forward.to_i,
expires: true,
token_type: 'Bearer',
- id_token: Crypto.sha256,
- token: Crypto.md5,
- refresh_token: Crypto.md5
+ id_token: Faker::Crypto.sha256,
+ token: Faker::Crypto.md5,
+ refresh_token: Faker::Crypto.md5
},
extra: {
raw_info: {
@@ -434,8 +434,8 @@ def auth0(name: nil, email: nil, uid: nil)
iss: 'https://auth0.com/',
sub: uid,
aud: 'Auth012345',
- iat: Time.forward.to_i,
- exp: Time.forward.to_i
+ iat: Faker::Time.forward.to_i,
+ exp: Faker::Time.forward.to_i
}
}
}
@@ -452,11 +452,11 @@ def timezone
end
def image
- Placeholdit.image
+ Faker::Placeholdit.image
end
def city_state
- "#{Address.city}, #{Address.state}"
+ "#{Faker::Address.city}, #{Faker::Address.state}"
end
def random_number_from_range(range)
diff --git a/lib/faker/quotes/quote.rb b/lib/faker/default/quote.rb
similarity index 100%
rename from lib/faker/quotes/quote.rb
rename to lib/faker/default/quote.rb
diff --git a/lib/faker/default/religion.rb b/lib/faker/default/religion.rb
deleted file mode 100644
index c8c4102b65..0000000000
--- a/lib/faker/default/religion.rb
+++ /dev/null
@@ -1,6 +0,0 @@
-# frozen_string_literal: true
-
-module Faker
- module Religion
- end
-end
diff --git a/lib/faker/sports/sport.rb b/lib/faker/default/sport.rb
similarity index 100%
rename from lib/faker/sports/sport.rb
rename to lib/faker/default/sport.rb
diff --git a/lib/faker/fantasy.rb b/lib/faker/fantasy.rb
new file mode 100644
index 0000000000..fee170bddc
--- /dev/null
+++ b/lib/faker/fantasy.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class Fantasy
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/games.rb b/lib/faker/games.rb
new file mode 100644
index 0000000000..f89a86cbcf
--- /dev/null
+++ b/lib/faker/games.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class Games
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/default/internet.rb b/lib/faker/internet.rb
similarity index 98%
rename from lib/faker/default/internet.rb
rename to lib/faker/internet.rb
index a0c198aaa6..429702622d 100644
--- a/lib/faker/default/internet.rb
+++ b/lib/faker/internet.rb
@@ -2,6 +2,10 @@
module Faker
class Internet < Base
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+
# Private, Host, and Link-Local network address blocks as defined in https://en.wikipedia.org/wiki/IPv4#Special-use_addresses
PRIVATE_IPV4_ADDRESS_RANGES = [
[10..10, 0..255, 0..255, 1..255], # 10.0.0.0/8 - Used for local communications within a private network
@@ -93,8 +97,8 @@ def username(specifier: nil, separators: %w[. _])
end
sample([
- Char.prepare(Name.first_name),
- [Name.first_name, Name.last_name].map do |name|
+ Char.prepare(Faker::Name.first_name),
+ [Faker::Name.first_name, Faker::Name.last_name].map do |name|
Char.prepare(name)
end.join(sample(separators))
])
@@ -235,7 +239,7 @@ def fix_umlauts(string: '')
# @example
# Faker::Internet.domain_word #=> "senger"
def domain_word
- with_locale(:en) { Char.prepare(Company.name.split.first) }
+ with_locale(:en) { Char.prepare(Faker::Company.name.split.first) }
end
## Returns the domain suffix e.g. com, org, co, biz, info etc.
diff --git a/lib/faker/default/internet_http.rb b/lib/faker/internet/http.rb
similarity index 100%
rename from lib/faker/default/internet_http.rb
rename to lib/faker/internet/http.rb
diff --git a/lib/faker/japanese_media.rb b/lib/faker/japanese_media.rb
new file mode 100644
index 0000000000..0ef0c348c2
--- /dev/null
+++ b/lib/faker/japanese_media.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class JapaneseMedia
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/locations.rb b/lib/faker/locations.rb
new file mode 100644
index 0000000000..469365c3c9
--- /dev/null
+++ b/lib/faker/locations.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class Locations
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/movies.rb b/lib/faker/movies.rb
new file mode 100644
index 0000000000..a71fb3fb6e
--- /dev/null
+++ b/lib/faker/movies.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class Movies
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/music/music.rb b/lib/faker/music.rb
similarity index 97%
rename from lib/faker/music/music.rb
rename to lib/faker/music.rb
index fe65f9abe1..7fe5a8293f 100644
--- a/lib/faker/music/music.rb
+++ b/lib/faker/music.rb
@@ -2,6 +2,10 @@
module Faker
class Music < Base
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+
class << self
NOTE_LETTERS = %w[C D E F G A B].freeze
ACCIDENTAL_SIGNS = ['b', '#', ''].freeze
diff --git a/lib/faker/music/bossa_nova.rb b/lib/faker/music/bossa_nova.rb
index 29dc8ea4f7..7d4f9b7f05 100644
--- a/lib/faker/music/bossa_nova.rb
+++ b/lib/faker/music/bossa_nova.rb
@@ -1,7 +1,5 @@
# frozen_string_literal: true
-require_relative 'music'
-
module Faker
class Music
class BossaNova < Base
diff --git a/lib/faker/music/grateful_dead.rb b/lib/faker/music/grateful_dead.rb
index b7d0853e0d..8f1aa33df1 100644
--- a/lib/faker/music/grateful_dead.rb
+++ b/lib/faker/music/grateful_dead.rb
@@ -1,7 +1,5 @@
# frozen_string_literal: true
-require_relative 'music'
-
module Faker
class Music
class GratefulDead < Base
diff --git a/lib/faker/music/pearl_jam.rb b/lib/faker/music/pearl_jam.rb
index c1c2ef59ae..78ebcbe6f8 100644
--- a/lib/faker/music/pearl_jam.rb
+++ b/lib/faker/music/pearl_jam.rb
@@ -1,7 +1,5 @@
# frozen_string_literal: true
-require_relative 'music'
-
module Faker
class Music
class PearlJam < Base
diff --git a/lib/faker/music/rush.rb b/lib/faker/music/rush.rb
index 2af9596011..b002381c3d 100644
--- a/lib/faker/music/rush.rb
+++ b/lib/faker/music/rush.rb
@@ -1,7 +1,5 @@
# frozen_string_literal: true
-require_relative 'music'
-
module Faker
class Music
class Rush < Base
diff --git a/lib/faker/music/smashing_pumpkins.rb b/lib/faker/music/smashing_pumpkins.rb
index 337f78f6aa..92df395fc4 100644
--- a/lib/faker/music/smashing_pumpkins.rb
+++ b/lib/faker/music/smashing_pumpkins.rb
@@ -1,7 +1,5 @@
# frozen_string_literal: true
-require_relative 'music'
-
module Faker
class Music
class SmashingPumpkins < Base
diff --git a/lib/faker/quotes.rb b/lib/faker/quotes.rb
new file mode 100644
index 0000000000..d7c1c006ff
--- /dev/null
+++ b/lib/faker/quotes.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class Quotes
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/religion.rb b/lib/faker/religion.rb
new file mode 100644
index 0000000000..4ea5a9482a
--- /dev/null
+++ b/lib/faker/religion.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class Religion
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/religion/bible.rb b/lib/faker/religion/bible.rb
index bacaef3c99..2530b897e8 100644
--- a/lib/faker/religion/bible.rb
+++ b/lib/faker/religion/bible.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
module Faker
- module Religion
+ class Religion
class Bible < Base
flexible :bible
diff --git a/lib/faker/sports.rb b/lib/faker/sports.rb
new file mode 100644
index 0000000000..b329cf2b73
--- /dev/null
+++ b/lib/faker/sports.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class Sports
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/travel.rb b/lib/faker/travel.rb
new file mode 100644
index 0000000000..c628e6867a
--- /dev/null
+++ b/lib/faker/travel.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class Travel
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/lib/faker/tv_shows.rb b/lib/faker/tv_shows.rb
new file mode 100644
index 0000000000..5fe2b5565d
--- /dev/null
+++ b/lib/faker/tv_shows.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Faker
+ class TvShows
+ if Faker::Config.lazy_loading?
+ Faker.lazy_load(self)
+ end
+ end
+end
diff --git a/test/faker/books/test_book.rb b/test/faker/default/test_book.rb
similarity index 100%
rename from test/faker/books/test_book.rb
rename to test/faker/default/test_book.rb
diff --git a/test/faker/games/test_faker_game.rb b/test/faker/default/test_faker_game.rb
similarity index 100%
rename from test/faker/games/test_faker_game.rb
rename to test/faker/default/test_faker_game.rb
diff --git a/test/faker/default/test_faker_internet_http.rb b/test/faker/default/test_faker_http.rb
similarity index 100%
rename from test/faker/default/test_faker_internet_http.rb
rename to test/faker/default/test_faker_http.rb
diff --git a/test/faker/movies/test_faker_movie.rb b/test/faker/default/test_faker_movie.rb
similarity index 100%
rename from test/faker/movies/test_faker_movie.rb
rename to test/faker/default/test_faker_movie.rb
diff --git a/test/faker/music/test_faker_music.rb b/test/faker/default/test_faker_music.rb
similarity index 100%
rename from test/faker/music/test_faker_music.rb
rename to test/faker/default/test_faker_music.rb
diff --git a/test/faker/quotes/test_faker_quote.rb b/test/faker/default/test_faker_quote.rb
similarity index 94%
rename from test/faker/quotes/test_faker_quote.rb
rename to test/faker/default/test_faker_quote.rb
index ab480e29dc..0258f1f5cc 100644
--- a/test/faker/quotes/test_faker_quote.rb
+++ b/test/faker/default/test_faker_quote.rb
@@ -2,7 +2,7 @@
require_relative '../../test_helper'
-class TestFakerYoda < Test::Unit::TestCase
+class TestFakerQuote < Test::Unit::TestCase
def setup
@tester = Faker::Quote
end
diff --git a/test/faker/sports/test_faker_sport.rb b/test/faker/default/test_faker_sport.rb
similarity index 100%
rename from test/faker/sports/test_faker_sport.rb
rename to test/faker/default/test_faker_sport.rb
diff --git a/test/faker/tv_shows/spongebob.rb b/test/faker/tv_shows/test_spongebob.rb
similarity index 100%
rename from test/faker/tv_shows/spongebob.rb
rename to test/faker/tv_shows/test_spongebob.rb
diff --git a/test/support/mock_env.rb b/test/support/mock_env.rb
new file mode 100644
index 0000000000..eac151c5b3
--- /dev/null
+++ b/test/support/mock_env.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+def mock_env(partial_env_hash)
+ old = ENV.to_hash
+ ENV.update partial_env_hash
+ begin
+ yield
+ ensure
+ ENV.replace old
+ end
+end
diff --git a/test/test_determinism.rb b/test/test_determinism.rb
index 0dd805d125..5e33e63e47 100644
--- a/test/test_determinism.rb
+++ b/test/test_determinism.rb
@@ -82,41 +82,17 @@ def skipped_classes
Bank
Base
Base58
- Books
- Cat
Char
ChileRut
- CLI
Config
- Creature
Date
Deprecator
- Dog
- DragonBall
- Dota
- ElderScrolls
- Fallout
Games
- GamesHalfLife
- HeroesOfTheStorm
Internet
- JapaneseMedia
- LeagueOfLegends
- Locations
- Movies
- Myst
- Overwatch
- OnePiece
- Pokemon
- Religion
- Sports
- SwordArtOnline
- TvShows
Time
+ TvShows
+ Music
VERSION
- Witcher
- WorldOfWarcraft
- Zelda
]
end
end
diff --git a/test/test_faker.rb b/test/test_faker.rb
index cc284ec9c0..5c6a814c4e 100644
--- a/test/test_faker.rb
+++ b/test/test_faker.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require_relative 'test_helper'
+require_relative 'support/mock_env'
module Faker
class TestFake
@@ -80,12 +81,37 @@ def test_deterministic_rand_in_range
assert_equal v, Faker::Base.rand_in_range(0, 1000)
end
- def test_config_lazy_loading
- refute_predicate Faker::Config, :lazy_loading?
+ def test_lazy_loading_with_config
+ mock_env('FAKER_LAZY_LOAD' => nil) do
+ refute_predicate Faker::Config, :lazy_loading?
- Faker::Config.lazy_loading = true
+ Faker::Config.lazy_loading = true
- assert_predicate Faker::Config, :lazy_loading?
+ assert_predicate Faker::Config, :lazy_loading?
+
+ Faker::Config.lazy_loading = false
+
+ refute_predicate Faker::Config, :lazy_loading?
+ end
+ end
+
+ def test_lazy_loading_with_env
+ mock_env('FAKER_LAZY_LOAD' => '1') do
+ assert_predicate Faker::Config, :lazy_loading?
+
+ Faker::Config.lazy_loading = false
+
+ assert_predicate Faker::Config, :lazy_loading?
+ end
+
+ # ignore config if env is set
+ mock_env('FAKER_LAZY_LOAD' => '0') do
+ refute_predicate Faker::Config, :lazy_loading?
+
+ Faker::Config.lazy_loading = true
+
+ refute_predicate Faker::Config, :lazy_loading?
+ end
end
def test_parse