Class: RequireHooks::Context
- Inherits:
-
Object
- Object
- RequireHooks::Context
- Defined in:
- lib/require-hooks/api.rb,
lib/require-hooks/mode/bootsnap.rb
Instance Attribute Summary collapse
-
#around_load ⇒ Object
readonly
Returns the value of attribute around_load.
-
#exclude_patterns ⇒ Object
readonly
Returns the value of attribute exclude_patterns.
-
#hijack_load ⇒ Object
readonly
Returns the value of attribute hijack_load.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
-
#source_transform ⇒ Object
readonly
Returns the value of attribute source_transform.
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #hijack? ⇒ Boolean
-
#initialize(patterns: nil, exclude_patterns: nil) ⇒ Context
constructor
A new instance of Context.
- #match?(path) ⇒ Boolean
- #merge!(another_ctx) ⇒ Object
- #perform_source_transform(path) ⇒ Object
- #readonly? ⇒ Boolean
- #run_around_load_callbacks(path) ⇒ Object
- #source_transform? ⇒ Boolean
- #to_cache_key ⇒ Object
- #to_key ⇒ Object
- #try_hijack_load(path, source) ⇒ Object
Constructor Details
#initialize(patterns: nil, exclude_patterns: nil) ⇒ Context
Returns a new instance of Context.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/require-hooks/api.rb', line 8 def initialize(patterns: nil, exclude_patterns: nil) @patterns = patterns.freeze @exclude_patterns = exclude_patterns.freeze @around_load = [] @source_transform = [] @hijack_load = [] @empty = nil @readonly = nil end |
Instance Attribute Details
#around_load ⇒ Object (readonly)
Returns the value of attribute around_load.
5 6 7 |
# File 'lib/require-hooks/api.rb', line 5 def around_load @around_load end |
#exclude_patterns ⇒ Object (readonly)
Returns the value of attribute exclude_patterns.
5 6 7 |
# File 'lib/require-hooks/api.rb', line 5 def exclude_patterns @exclude_patterns end |
#hijack_load ⇒ Object (readonly)
Returns the value of attribute hijack_load.
5 6 7 |
# File 'lib/require-hooks/api.rb', line 5 def hijack_load @hijack_load end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
5 6 7 |
# File 'lib/require-hooks/api.rb', line 5 def patterns @patterns end |
#source_transform ⇒ Object (readonly)
Returns the value of attribute source_transform.
5 6 7 |
# File 'lib/require-hooks/api.rb', line 5 def source_transform @source_transform end |
Instance Method Details
#empty? ⇒ Boolean
30 31 32 33 |
# File 'lib/require-hooks/api.rb', line 30 def empty? return @empty unless @empty.nil? @empty = @around_load.empty? && @source_transform.empty? && @hijack_load.empty? end |
#hijack? ⇒ Boolean
45 46 47 |
# File 'lib/require-hooks/api.rb', line 45 def hijack? @hijack_load.any? end |
#match?(path) ⇒ Boolean
24 25 26 27 28 |
# File 'lib/require-hooks/api.rb', line 24 def match?(path) return false unless !patterns || patterns.any? { |pattern| File.fnmatch?(pattern, path) } return false if exclude_patterns&.any? { |pattern| File.fnmatch?(pattern, path) } true end |
#merge!(another_ctx) ⇒ Object
81 82 83 84 85 |
# File 'lib/require-hooks/api.rb', line 81 def merge!(another_ctx) around_load.concat(another_ctx.around_load) source_transform.concat(another_ctx.source_transform) hijack_load.concat(another_ctx.hijack_load) end |
#perform_source_transform(path) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/require-hooks/api.rb', line 59 def perform_source_transform(path) return unless @source_transform.any? source = nil @source_transform.each do |transform| source = transform.call(path, source) || source end source end |
#readonly? ⇒ Boolean
35 36 37 38 39 |
# File 'lib/require-hooks/api.rb', line 35 def readonly? return @readonly unless @readonly.nil? @readonly = @source_transform.empty? && @hijack_load.empty? end |
#run_around_load_callbacks(path) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/require-hooks/api.rb', line 49 def run_around_load_callbacks(path) return yield if @around_load.empty? chain = @around_load.reverse.inject do |acc_proc, next_proc| proc { |path, &block| acc_proc.call(path) { next_proc.call(path, &block) } } end chain.call(path) { yield } end |
#source_transform? ⇒ Boolean
41 42 43 |
# File 'lib/require-hooks/api.rb', line 41 def source_transform? @source_transform.any? end |
#to_cache_key ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/require-hooks/mode/bootsnap.rb', line 102 def to_cache_key Zlib.crc32( (around_load + source_transform + hijack_load).map do |pr| RubyVM::InstructionSequence.disasm(pr) end.join("\n") ).to_s end |
#to_key ⇒ Object
20 21 22 |
# File 'lib/require-hooks/api.rb', line 20 def to_key [patterns, exclude_patterns] end |
#try_hijack_load(path, source) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/require-hooks/api.rb', line 71 def try_hijack_load(path, source) return unless @hijack_load.any? @hijack_load.each do |hijack| result = hijack.call(path, source) return result if result end nil end |