Skip to content
Open
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
23 changes: 21 additions & 2 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -59,3 +63,18 @@ jobs:

- name: Run tests
run: bundle exec rake test
test-with-lazy-loading:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we need to run the tests for every ruby version. Just one is enough?

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

50 changes: 47 additions & 3 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions lib/faker/blockchain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Faker
class Blockchain
if Faker::Config.lazy_loading?
Faker.lazy_load(self)
end
end
end
9 changes: 9 additions & 0 deletions lib/faker/books.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Faker
class Books
if Faker::Config.lazy_loading?
Faker.lazy_load(self)
end
end
end
9 changes: 9 additions & 0 deletions lib/faker/creature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Faker
class Creature
if Faker::Config.lazy_loading?
Faker.lazy_load(self)
end
end
end
9 changes: 9 additions & 0 deletions lib/faker/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Faker
class Default
if Faker::Config.lazy_loading?
Faker.lazy_load(self)
end
end
end
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions lib/faker/default/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class << self
# @faker.version 3.2.1
def heading
level = rand(1..6)
"<h#{level}>#{Lorem.word.capitalize}</h#{level}>"
"<h#{level}>#{Faker::Lorem.word.capitalize}</h#{level}>"
end

##
Expand Down Expand Up @@ -97,7 +97,7 @@ def unordered_list
#
# @faker.version 3.2.1
def code
"<code>#{Lorem.sentence(word_count: 1)}</code>"
"<code>#{Faker::Lorem.sentence(word_count: 1)}</code>"
end

##
Expand Down Expand Up @@ -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')"}) #=> "<div class=\"xss\" onclick=\"alert('XSS')\">This is a div with XSS attributes.</div>"
#
# @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
Expand Down
4 changes: 2 additions & 2 deletions lib/faker/default/lorem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand All @@ -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

##
Expand Down
6 changes: 3 additions & 3 deletions lib/faker/default/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand Down Expand Up @@ -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

##
Expand All @@ -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")
Expand Down
File renamed without changes.
Loading