Skip to content

Commit 7193b32

Browse files
Add Lazy loading config (#3244)
* feat(lazy-loading): add `Faker::Config.lazy_loading?` (#3226) Adds configuration option to enable/disable lazy loading so it can be explicitly enabled during beta testing. * Introduce lazy loading as an optional config (#3228) * Lazy load generators To keep this change backwards compatible, we introduce lazy loading as a config option. We need to load main classes first for nested generators such as 'music' and when lazy loading is not enabled. 'internet/http'. This will avoid the need for renaming these generators. Co-authored-by: Thiago Araujo <thd.araujo@gmail.com> * Clean up skipped generators from determinism test * Rename test file following conventions * Run lazy loading smoke test in CI * Check for true values when setting ENV['FAKER_LAZY_LOAD'] Covers the case of users setting the env using truthy values. * Better code for checking for ENV values --------- Co-authored-by: Thiago Araujo <thd.araujo@gmail.com> --------- Co-authored-by: Thiago Araujo <707418+thdaraujo@users.noreply.github.com> Co-authored-by: Thiago Araujo <thd.araujo@gmail.com>
1 parent 737ae42 commit 7193b32

46 files changed

Lines changed: 343 additions & 117 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ruby.yml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ name: Tests
99

1010
on:
1111
push:
12-
branches: [ main ]
12+
branches:
13+
- main
14+
- 'lazy-loading-beta'
1315
pull_request:
14-
branches: [ main ]
16+
branches:
17+
- main
18+
- 'lazy-loading-beta'
1519
workflow_dispatch:
1620

1721
permissions:
@@ -58,3 +62,18 @@ jobs:
5862

5963
- name: Run tests
6064
run: bundle exec rake test
65+
test-with-lazy-loading:
66+
name: Ruby 3.2 with lazy loading
67+
runs-on: ubuntu-latest
68+
env:
69+
BUNDLE_WITHOUT: benchmark
70+
steps:
71+
- uses: actions/checkout@v6
72+
- uses: ruby/setup-ruby@v1
73+
with:
74+
ruby-version: '3.2'
75+
bundler-cache: true
76+
77+
- name: Run tests with lazy load enabled
78+
run: FAKER_LAZY_LOAD=1 bundle exec rake test
79+

lib/faker.rb

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ def random=(new_random)
3838
def random
3939
Thread.current[:faker_config_random] || Random
4040
end
41+
42+
def lazy_loading?
43+
if ENV.key?('FAKER_LAZY_LOAD') && !ENV['FAKER_LAZY_LOAD'].nil?
44+
%w[true TRUE 1].include?(ENV.fetch('FAKER_LAZY_LOAD', nil))
45+
else
46+
Thread.current[:faker_lazy_loading] == true
47+
end
48+
end
49+
50+
def lazy_loading=(value)
51+
Thread.current[:faker_lazy_loading] = value
52+
end
4153
end
4254
end
4355

@@ -275,7 +287,47 @@ def disable_enforce_available_locales
275287
end
276288
end
277289
end
290+
291+
if Faker::Config.lazy_loading?
292+
def self.load_path(*constants)
293+
constants.map do |class_name|
294+
class_name
295+
.to_s
296+
.gsub('::', '/')
297+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
298+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
299+
.tr('-', '_')
300+
.downcase
301+
end.join('/')
302+
end
303+
304+
def self.lazy_load(klass)
305+
def klass.const_missing(class_name)
306+
load_path = case class_name
307+
when :DnD
308+
Faker.load_path('faker/games/dnd')
309+
else
310+
Faker.load_path(name, class_name)
311+
end
312+
313+
begin
314+
require(load_path)
315+
rescue LoadError
316+
require(load_path.gsub('faker/', 'faker/default/'))
317+
end
318+
319+
const_get(class_name)
320+
end
321+
end
322+
323+
lazy_load(self)
324+
end
278325
end
279326

280-
# require faker objects
281-
Dir.glob(File.join(mydir, 'faker', '/**/*.rb')).each { |file| require file }
327+
unless Faker::Config.lazy_loading?
328+
rb_files = []
329+
rb_files << File.join(mydir, 'faker', '*.rb')
330+
rb_files << File.join(mydir, 'faker', '/**/*.rb')
331+
332+
Dir.glob(rb_files).each { |file| require file }
333+
end

lib/faker/blockchain.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module Faker
4+
class Blockchain
5+
if Faker::Config.lazy_loading?
6+
Faker.lazy_load(self)
7+
end
8+
end
9+
end

lib/faker/books.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module Faker
4+
class Books
5+
if Faker::Config.lazy_loading?
6+
Faker.lazy_load(self)
7+
end
8+
end
9+
end

lib/faker/creature.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module Faker
4+
class Creature
5+
if Faker::Config.lazy_loading?
6+
Faker.lazy_load(self)
7+
end
8+
end
9+
end

lib/faker/default.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module Faker
4+
class Default
5+
if Faker::Config.lazy_loading?
6+
Faker.lazy_load(self)
7+
end
8+
end
9+
end

lib/faker/default/html.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class << self
1414
# @faker.version 3.2.1
1515
def heading
1616
level = rand(1..6)
17-
"<h#{level}>#{Lorem.word.capitalize}</h#{level}>"
17+
"<h#{level}>#{Faker::Lorem.word.capitalize}</h#{level}>"
1818
end
1919

2020
##
@@ -97,7 +97,7 @@ def unordered_list
9797
#
9898
# @faker.version 3.2.1
9999
def code
100-
"<code>#{Lorem.sentence(word_count: 1)}</code>"
100+
"<code>#{Faker::Lorem.sentence(word_count: 1)}</code>"
101101
end
102102

103103
##
@@ -165,7 +165,7 @@ def link(rel: 'stylesheet')
165165
# Faker::HTML.element(tag: 'div', content: "This is a div with XSS attributes.", attributes: {class: 'xss', onclick: "alert('XSS')"}) #=> "<div class=\"xss\" onclick=\"alert('XSS')\">This is a div with XSS attributes.</div>"
166166
#
167167
# @faker.version 3.2.1
168-
def element(tag: 'div', content: Lorem.sentence(word_count: 3), attributes: { class: Lorem.word, onclick: "#{Lorem.word}()" })
168+
def element(tag: 'div', content: Faker::Lorem.sentence(word_count: 3), attributes: { class: Faker::Lorem.word, onclick: "#{Faker::Lorem.word}()" })
169169
attribute_string = attributes.map { |key, value| "#{key}=\"#{value}\"" }.join(' ')
170170
"<#{tag} #{attribute_string}>#{content}</#{tag}>"
171171
end

lib/faker/default/lorem.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def words(number: 3, supplemental: false, exclude_words: nil)
5858
#
5959
# @faker.version 2.1.3
6060
def character
61-
sample(Types::CHARACTERS)
61+
sample(Faker::Types::CHARACTERS)
6262
end
6363

6464
##
@@ -78,7 +78,7 @@ def character
7878
#
7979
# @faker.version 2.1.3
8080
def characters(number: 255, min_alpha: 0, min_numeric: 0)
81-
Alphanumeric.alphanumeric(number: number, min_alpha: min_alpha, min_numeric: min_numeric)
81+
Faker::Alphanumeric.alphanumeric(number: number, min_alpha: min_alpha, min_numeric: min_numeric)
8282
end
8383

8484
##

0 commit comments

Comments
 (0)