Class: RequireHooks::Context
- Inherits:
-
Object
- Object
- RequireHooks::Context
- Defined in:
- lib/require-hooks/api.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
- #perform_source_transform(path) ⇒ Object
- #run_around_load_callbacks(path) ⇒ Object
- #source_transform? ⇒ Boolean
- #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 |
# 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 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
29 30 31 32 |
# File 'lib/require-hooks/api.rb', line 29 def empty? return @empty unless @empty.nil? @empty = @around_load.empty? && @source_transform.empty? && @hijack_load.empty? end |
#hijack? ⇒ Boolean
38 39 40 |
# File 'lib/require-hooks/api.rb', line 38 def hijack? @hijack_load.any? end |
#match?(path) ⇒ Boolean
23 24 25 26 27 |
# File 'lib/require-hooks/api.rb', line 23 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 |
#perform_source_transform(path) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/require-hooks/api.rb', line 52 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 |
#run_around_load_callbacks(path) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/require-hooks/api.rb', line 42 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
34 35 36 |
# File 'lib/require-hooks/api.rb', line 34 def source_transform? @source_transform.any? end |
#to_key ⇒ Object
19 20 21 |
# File 'lib/require-hooks/api.rb', line 19 def to_key [patterns, exclude_patterns] end |
#try_hijack_load(path, source) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/require-hooks/api.rb', line 64 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 |