Class: GemBench::Jersey
- Inherits:
-
Object
- Object
- GemBench::Jersey
- Defined in:
- lib/gem_bench/jersey.rb
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#gem_name ⇒ Object
readonly
Returns the value of attribute gem_name.
-
#gem_path ⇒ Object
readonly
Returns the value of attribute gem_path.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#trades ⇒ Object
readonly
Returns the value of attribute trades.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
-
#as_klass ⇒ Object
Will raise NameError if called before #doff_and_don.
-
#doff_and_don(&block) ⇒ Object
Generates tempfiles and requires them, resulting in a loaded gem that will not have namespace collisions when alongside the original-namespaced gem.
-
#initialize(gem_name:, trades:, metadata: {}, verbose: false) ⇒ Jersey
constructor
A new instance of Jersey.
- #primary_namespace ⇒ Object
- #required? ⇒ Boolean
Constructor Details
#initialize(gem_name:, trades:, metadata: {}, verbose: false) ⇒ Jersey
Returns a new instance of Jersey.
35 36 37 38 39 40 41 42 |
# File 'lib/gem_bench/jersey.rb', line 35 def initialize(gem_name:, trades:, metadata: {}, verbose: false) @gem_name = gem_name @gem_path = Gem.loaded_specs[gem_name]&.full_gem_path @gem_lib_dir = Gem @trades = trades @metadata = @verbose = verbose end |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
32 33 34 |
# File 'lib/gem_bench/jersey.rb', line 32 def files @files end |
#gem_name ⇒ Object (readonly)
Returns the value of attribute gem_name.
28 29 30 |
# File 'lib/gem_bench/jersey.rb', line 28 def gem_name @gem_name end |
#gem_path ⇒ Object (readonly)
Returns the value of attribute gem_path.
29 30 31 |
# File 'lib/gem_bench/jersey.rb', line 29 def gem_path @gem_path end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
31 32 33 |
# File 'lib/gem_bench/jersey.rb', line 31 def @metadata end |
#trades ⇒ Object (readonly)
Returns the value of attribute trades.
30 31 32 |
# File 'lib/gem_bench/jersey.rb', line 30 def trades @trades end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
33 34 35 |
# File 'lib/gem_bench/jersey.rb', line 33 def verbose @verbose end |
Instance Method Details
#as_klass ⇒ Object
Will raise NameError if called before #doff_and_don
106 107 108 |
# File 'lib/gem_bench/jersey.rb', line 106 def as_klass Object.const_get(primary_namespace) if gem_path end |
#doff_and_don(&block) ⇒ Object
Generates tempfiles and requires them, resulting
in a loaded gem that will not have namespace
collisions when alongside the original-namespaced gem.
If a block is provided the contents of each file will be yielded to the block,
after all namespace substitutions are complete, but before the contents
are written to the re-namespaced gem. The return value of the block will be
written to the file in this scenario.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/gem_bench/jersey.rb', line 57 def doff_and_don(&block) return puts "Skipping #{gem_name} (not loaded on #{RUBY_VERSION})" unless gem_path puts "Doffing #{gem_path}" if verbose Dir.mktmpdir do |directory| files = [] Dir[File.join(gem_path, "lib", "**", "*.rb")].map do |file| if verbose puts file puts File.basename(file) puts "--------------------------------" end dirname = File.dirname(file) puts "dirname: #{dirname}" if verbose is_at_gem_root = dirname[(-4)..(-1)] == "/lib" puts "is_at_gem_root: #{is_at_gem_root}" if verbose lib_split = dirname.split("/lib/")[-1] puts "lib_split: #{lib_split}" if verbose # lib_split could be like: # - "ruby/gems/3.2.0/gems/method_source-1.1.0/lib" # - "method_source" # Se we check to make sure it is actually a subdir of the gem's lib directory full_path = File.join(gem_path, "lib", lib_split) relative_path = !is_at_gem_root && Dir.exist?(full_path) && lib_split puts "relative_path: #{relative_path}" if verbose filename = File.basename(file)[0..-4] puts "filename: #{filename}" if verbose if relative_path dir_path = File.join(directory, relative_path) Dir.mkdir(dir_path) unless Dir.exist?(dir_path) puts "creating #{filename} in #{dir_path}" if verbose files << create_tempfile_copy(file, filename, dir_path, :dd1, &block) else puts "directory not relative (#{directory}) for file #{filename}" if verbose files << create_tempfile_copy(file, filename, directory, :dd2, &block) end end load_gem_copy(files) end nil end |
#primary_namespace ⇒ Object
101 102 103 |
# File 'lib/gem_bench/jersey.rb', line 101 def primary_namespace trades.values.first end |
#required? ⇒ Boolean
44 45 46 |
# File 'lib/gem_bench/jersey.rb', line 44 def required? gem_path && trades.values.all? { |new_namespace| Object.const_defined?(new_namespace) } end |