Skip to content

Commit

Permalink
Refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyObtiva committed Oct 2, 2020
1 parent e2b8f44 commit d6e9ff4
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 133 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -302,7 +302,7 @@ There are many more options and powerful features in [puts_debuggerer](https://r
Add the following to bundler's `Gemfile`.

```ruby
gem 'puts_debuggerer', '~> 0.10.1'
gem 'puts_debuggerer', '~> 0.10.2'
```

This is the recommended way for [Rails](rubyonrails.org) apps. Optionally, you may create an initializer under `config/initializers` named `puts_debuggerer_options.rb` to enable further customizations as per the [Options](#options) section below.
Expand All @@ -312,7 +312,7 @@ This is the recommended way for [Rails](rubyonrails.org) apps. Optionally, you m
Or manually install and require library.

```bash
gem install puts_debuggerer -v0.10.1
gem install puts_debuggerer -v0.10.2
```

```ruby
Expand Down
132 changes: 3 additions & 129 deletions lib/puts_debuggerer.rb
@@ -1,19 +1,9 @@
require 'stringio'

require 'puts_debuggerer/core_ext/kernel'
require 'puts_debuggerer/core_ext/logger'
require 'puts_debuggerer/core_ext/logging/logger'
require 'puts_debuggerer/run_determiner'
require 'puts_debuggerer/source_file'

# in case 'logger' is not required
class Logger
end

# in case 'logging' is not required
module Logging
class Logger
end
end

module PutsDebuggerer
SOURCE_LINE_COUNT_DEFAULT = 1
HEADER_DEFAULT = '*'*80
Expand Down Expand Up @@ -68,8 +58,7 @@ def format_obj(obj)
CALLER_DEPTH_ZERO_OPAL = -1 #depth includes pd + with_options method + nested block + build_pd_data method
STACK_TRACE_CALL_LINE_NUMBER_REGEX = /\:(\d+)\:in /
STACK_TRACE_CALL_SOURCE_FILE_REGEX = /[ ]*([^:]+)\:\d+\:in /
# STACK_TRACE_CALL_SOURCE_FILE_REGEX_OPAL = /https?\:\/\/[^\/]+\/assets\/([^-]+)\.self-/
STACK_TRACE_CALL_SOURCE_FILE_REGEX_OPAL = /\((http[^\)]+)\)/
STACK_TRACE_CALL_SOURCE_FILE_REGEX_OPAL = /(http[^\)]+)/

class << self
# Application root path to exclude when printing out file path
Expand Down Expand Up @@ -506,118 +495,3 @@ def determine_printer(options)
PutsDebuggerer.caller = nil
PutsDebuggerer.run_at = nil
PutsDebuggerer.source_line_count = nil

# Provides caller line number starting 1 level above caller of
# this method.
#
# Example:
#
# # lib/example.rb # line 1
# puts "Print out __caller_line_number__" # line 2
# puts __caller_line_number__ # line 3
#
# prints out `3`
def __caller_line_number__(caller_depth=0)
return if RUBY_PLATFORM == 'opal'
caller[caller_depth] && caller[caller_depth][PutsDebuggerer::STACK_TRACE_CALL_LINE_NUMBER_REGEX, 1].to_i
end

# Provides caller file starting 1 level above caller of
# this method.
#
# Example:
#
# # File Name: lib/example.rb
# puts __caller_file__
#
# prints out `lib/example.rb`
def __caller_file__(caller_depth=0)
regex = RUBY_PLATFORM == 'opal' ? PutsDebuggerer::STACK_TRACE_CALL_SOURCE_FILE_REGEX_OPAL : PutsDebuggerer::STACK_TRACE_CALL_SOURCE_FILE_REGEX
caller[caller_depth] && caller[caller_depth][regex, 1]
end


# Provides caller source line starting 1 level above caller of
# this method.
#
# Example:
#
# puts __caller_source_line__
#
# prints out `puts __caller_source_line__`
def __caller_source_line__(caller_depth=0, source_line_count=nil, source_file=nil, source_line_number=nil)
source_line_number ||= __caller_line_number__(caller_depth+1)
source_file ||= __caller_file__(caller_depth+1)
source_line = ''
if source_file == '(irb)'
source_line = conf.io.line(source_line_number) # TODO handle multi-lines in source_line_count
else
source_line = PutsDebuggerer::SourceFile.new(source_file).source(source_line_count, source_line_number)
end
source_line
end

private

def __with_pd_options__(options=nil)
options ||= {}
permanent_options = PutsDebuggerer.options
PutsDebuggerer.options = options.select {|option, _| PutsDebuggerer.options.keys.include?(option)}
print_engine_options = options.delete_if {|option, _| PutsDebuggerer.options.keys.include?(option)}
yield print_engine_options
PutsDebuggerer.options = permanent_options
end

def __build_pd_data__(object, print_engine_options=nil, source_line_count=nil, run_number=nil, pd_inspect=false, logger_formatter_decorated=false, logging_layouts_decorated=false)
depth = RUBY_PLATFORM == 'opal' ? PutsDebuggerer::CALLER_DEPTH_ZERO_OPAL : PutsDebuggerer::CALLER_DEPTH_ZERO
depth += 1 if pd_inspect
depth += 4 if pd_inspect && logger_formatter_decorated
depth += 8 if pd_inspect && logging_layouts_decorated

pd_data = {
announcer: PutsDebuggerer.announcer,
file: __caller_file__(depth)&.sub(PutsDebuggerer.app_path.to_s, ''),
line_number: __caller_line_number__(depth),
pd_expression: __caller_pd_expression__(depth, source_line_count),
run_number: run_number,
object: object,
object_printer: PutsDebuggerer::OBJECT_PRINTER_DEFAULT.call(object, print_engine_options, source_line_count, run_number)
}
pd_data[:caller] = __caller_caller__(depth)
['header', 'wrapper', 'footer'].each do |boundary_option|
pd_data[boundary_option.to_sym] = PutsDebuggerer.send(boundary_option) if PutsDebuggerer.send("#{boundary_option}?")
end
pd_data
end

# Returns the caller stack trace of the caller of pd
def __caller_caller__(depth)
return unless PutsDebuggerer.caller?
start_depth = depth.to_i + 1
caller_depth = PutsDebuggerer.caller == -1 ? -1 : (start_depth + PutsDebuggerer.caller)
caller[start_depth..caller_depth].to_a
end

def __format_pd_expression__(expression, object)
"\n > #{expression}\n =>"
end

def __caller_pd_expression__(depth=0, source_line_count=nil)
# Caller Source Line Depth 2 = 1 to pd method + 1 to caller
source_line = __caller_source_line__(depth+1, source_line_count)
source_line = __extract_pd_expression__(source_line)
source_line = source_line.gsub(/(^'|'$)/, '"') if source_line.start_with?("'") && source_line.end_with?("'")
source_line = source_line.gsub(/(^\(|\)$)/, '') if source_line.start_with?("(") && source_line.end_with?(")")
source_line
end

# Extracts pd source line expression.
#
# Example:
#
# __extract_pd_expression__("pd (x=1)")
#
# outputs `(x=1)`
def __extract_pd_expression__(source_line)
source_line.to_s.strip
end
121 changes: 120 additions & 1 deletion lib/puts_debuggerer/core_ext/kernel.rb
@@ -1,3 +1,5 @@
require 'stringio'

module Kernel
# Prints object with bonus info such as file name, line number and source
# expression. Optionally prints out header and footer.
Expand Down Expand Up @@ -41,7 +43,7 @@ def pd(*objects)
if PutsDebuggerer::RunDeterminer.run_pd?(object, run_at)
__with_pd_options__(options) do |print_engine_options|
run_number = PutsDebuggerer::RunDeterminer.run_number(object, run_at)
formatter_pd_data = __build_pd_data__(object, print_engine_options, PutsDebuggerer.source_line_count, run_number, pd_inspect, logger_formatter_decorated, logging_layouts_decorated) #depth adds build method
formatter_pd_data = __build_pd_data__(object, print_engine_options: print_engine_options, source_line_count: PutsDebuggerer.source_line_count, run_number: run_number, pd_inspect: pd_inspect, logger_formatter_decorated: logger_formatter_decorated, logging_layouts_decorated: logging_layouts_decorated)
stdout = $stdout
$stdout = sio = StringIO.new
PutsDebuggerer.formatter.call(formatter_pd_data)
Expand Down Expand Up @@ -95,4 +97,121 @@ def pd_inspect
pd self, printer: false, pd_inspect: true
end
alias pdi pd_inspect

# Provides caller line number starting 1 level above caller of
# this method.
#
# Example:
#
# # lib/example.rb # line 1
# puts "Print out __caller_line_number__" # line 2
# puts __caller_line_number__ # line 3
#
# prints out `3`
def __caller_line_number__(caller_depth=0)
return if RUBY_PLATFORM == 'opal'
caller[caller_depth] && caller[caller_depth][PutsDebuggerer::STACK_TRACE_CALL_LINE_NUMBER_REGEX, 1].to_i
end

# Provides caller file starting 1 level above caller of
# this method.
#
# Example:
#
# # File Name: lib/example.rb
# puts __caller_file__
#
# prints out `lib/example.rb`
def __caller_file__(caller_depth=0)
regex = RUBY_PLATFORM == 'opal' ? PutsDebuggerer::STACK_TRACE_CALL_SOURCE_FILE_REGEX_OPAL : PutsDebuggerer::STACK_TRACE_CALL_SOURCE_FILE_REGEX
caller[caller_depth] && caller[caller_depth][regex, 1]
end


# Provides caller source line starting 1 level above caller of
# this method.
#
# Example:
#
# puts __caller_source_line__
#
# prints out `puts __caller_source_line__`
def __caller_source_line__(caller_depth=0, source_line_count=nil, source_file=nil, source_line_number=nil)
source_line_number ||= __caller_line_number__(caller_depth+1)
source_file ||= __caller_file__(caller_depth+1)
source_line = ''
if source_file == '(irb)'
source_line = conf.io.line(source_line_number) # TODO handle multi-lines in source_line_count
else
source_line = PutsDebuggerer::SourceFile.new(source_file).source(source_line_count, source_line_number)
end
source_line
end

private

def __with_pd_options__(options=nil)
options ||= {}
permanent_options = PutsDebuggerer.options
PutsDebuggerer.options = options.select {|option, _| PutsDebuggerer.options.keys.include?(option)}
print_engine_options = options.delete_if {|option, _| PutsDebuggerer.options.keys.include?(option)}
yield print_engine_options
PutsDebuggerer.options = permanent_options
end

def __build_pd_data__(object, print_engine_options:nil, source_line_count:nil, run_number:nil, pd_inspect:false, logger_formatter_decorated:false, logging_layouts_decorated:false)
depth = RUBY_PLATFORM == 'opal' ? PutsDebuggerer::CALLER_DEPTH_ZERO_OPAL : PutsDebuggerer::CALLER_DEPTH_ZERO
if pd_inspect
depth += 1
depth += 4 if logger_formatter_decorated
depth += 8 if logging_layouts_decorated
end

pd_data = {
announcer: PutsDebuggerer.announcer,
file: __caller_file__(depth)&.sub(PutsDebuggerer.app_path.to_s, ''),
line_number: __caller_line_number__(depth),
pd_expression: __caller_pd_expression__(depth, source_line_count),
run_number: run_number,
object: object,
object_printer: PutsDebuggerer::OBJECT_PRINTER_DEFAULT.call(object, print_engine_options, source_line_count, run_number)
}
pd_data[:caller] = __caller_caller__(depth)
['header', 'wrapper', 'footer'].each do |boundary_option|
pd_data[boundary_option.to_sym] = PutsDebuggerer.send(boundary_option) if PutsDebuggerer.send("#{boundary_option}?")
end
pd_data
end

# Returns the caller stack trace of the caller of pd
def __caller_caller__(depth)
return unless PutsDebuggerer.caller?
start_depth = depth.to_i + 1
caller_depth = PutsDebuggerer.caller == -1 ? -1 : (start_depth + PutsDebuggerer.caller)
caller[start_depth..caller_depth].to_a
end

def __format_pd_expression__(expression, object)
"\n > #{expression}\n =>"
end

def __caller_pd_expression__(depth=0, source_line_count=nil)
# Caller Source Line Depth 2 = 1 to pd method + 1 to caller
source_line = __caller_source_line__(depth+1, source_line_count)
source_line = __extract_pd_expression__(source_line)
source_line = source_line.gsub(/(^'|'$)/, '"') if source_line.start_with?("'") && source_line.end_with?("'")
source_line = source_line.gsub(/(^\(|\)$)/, '') if source_line.start_with?("(") && source_line.end_with?(")")
source_line
end

# Extracts pd source line expression.
#
# Example:
#
# __extract_pd_expression__("pd (x=1)")
#
# outputs `(x=1)`
def __extract_pd_expression__(source_line)
source_line.to_s.strip
end
end
3 changes: 3 additions & 0 deletions lib/puts_debuggerer/core_ext/logger.rb
@@ -0,0 +1,3 @@
# in case 'logger' is not required
class Logger
end
5 changes: 5 additions & 0 deletions lib/puts_debuggerer/core_ext/logging/logger.rb
@@ -0,0 +1,5 @@
# in case 'logging' is not required
module Logging
class Logger
end
end
1 change: 0 additions & 1 deletion lib/puts_debuggerer/source_file.rb
Expand Up @@ -15,7 +15,6 @@ def source(source_line_count, source_line_number)

def source_lines(source_line_count, source_line_number)
lines = []
return lines if RUBY_PLATFORM == 'opal'
begin
while @file.lineno < source_line_number + source_line_count
file_line_number = @file.lineno + 1
Expand Down

0 comments on commit d6e9ff4

Please sign in to comment.