Module: Kernel

Defined in:
lib/lit_pry.rb

Overview

TODO: Investigate RubyGems ‘require` before overriding. SEE: github.com/ruby/ruby/blob/v2_6_3/lib/rubygems/core_ext/kernel_require.rb

Constant Summary collapse

@@lit_processed_paths =
Set.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_lit_binding(line) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/lit_pry.rb', line 98

def self.add_lit_binding(line)
  ['lit ', 'lit(', '🔥 ', '🔥(', '🔥'].each do |needle|
    if line.strip.start_with? needle
      args = line.strip.delete_prefix(needle).delete_suffix(")").chomp
      return "lit(#{args}) { binding.pry if LitCLI.is_prying?; @@is_prying = false } \n"
    end
  end

  false
end

Instance Method Details

#get_binding(__dir__) ⇒ Object



91
92
93
94
95
96
# File 'lib/lit_pry.rb', line 91

def get_binding(__dir__)
  # Don't go to this binding when Pry session activated.
  # The variables have already been evaluated by eval(). NEEDS CONFIRMATION.
  # Setting file_path in eval() negates this fix but will keep just in case.
  return binding unless LitCLI.is_prying?
end

#require_relative(relative_path, current_directory = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
# File 'lib/lit_pry.rb', line 12

def require_relative relative_path, current_directory = nil
  return false if relative_path.nil?

  # Handle absolute path.
  if relative_path.start_with?('/') && File.exist?(relative_path)
    absolute_path = relative_path.split('/')
    file_name = absolute_path.pop
  # Handle relative path.
  else
    # Get directory of the file that called this file.
    if current_directory.nil?
      absolute_path = caller_locations.first.absolute_path.split('/')
      absolute_path.pop
    else
      absolute_path = current_directory.split('/')
    end

    # When path is current directory.
    if relative_path.start_with?('./')
      relative_path.delete_prefix! './'
    end

    # When path is parent directory.
    while relative_path.start_with?('../')
      relative_path.delete_prefix! '../'
      absolute_path.pop
    end

    # When path contains child directories.
    if relative_path.split('/').count > 1
      # Add child directories to absolute path and remove from relative path.
      child_path = relative_path.split('/')
      relative_path = child_path.pop
      child_path.each do |dir|
        absolute_path << dir
      end
    end

    file_name = relative_path
  end

  # Append default extension.
  unless file_name.end_with? '.rb'
    file_name << '.rb'
  end

  file_path = File.join(absolute_path.join('/'), file_name)

  unless @@lit_processed_paths.include? file_path
    @@lit_processed_paths.add file_path

    new_lines = ''
    line_count = 0
    new_lines_count = 0

    File.foreach(file_path) do |line|
      line_count = line_count + 1

      # Pass current directory into the next file's requires.
      if line.strip.start_with? 'require_relative '
        line = line.strip + ", '#{absolute_path.join('/')}'\n"
        new_lines << line
      # Add pry binding on each lit method.
      elsif lit_line = Kernel.add_lit_binding(line)
        new_lines << lit_line
      else
        new_lines << line
      end
    end

    eval(new_lines, get_binding(__dir__ = absolute_path), file_path)

    return true
  # Path has already been required.
  else
    return false
  end
end