Class: PryJist::Gist

Inherits:
Pry::ClassCommand
  • Object
show all
Defined in:
lib/pry-jist/gist.rb

Overview

Since:

  • v1.0.0

Instance Method Summary collapse

Instance Method Details

#clipboard_content(content) ⇒ Object

Since:

  • v1.0.0



63
64
65
66
# File 'lib/pry-jist/gist.rb', line 63

def clipboard_content(content)
  ::Gist.copy(content)
  output.puts "Copied content to clipboard!"
end

#comment_expression_result_for_gist(result) ⇒ Object

Since:

  • v1.0.0



88
89
90
91
92
93
94
95
# File 'lib/pry-jist/gist.rb', line 88

def comment_expression_result_for_gist(result)
  content = ""
  result.lines.each_with_index do |line, index|
    content << index == 0 ? "# => #{line}" : "#    #{line}"
  end

  content
end

#gist_content(content, filename) ⇒ Object

Since:

  • v1.0.0



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pry-jist/gist.rb', line 97

def gist_content(content, filename)
  response = ::Gist.gist(
    content,
    filename: filename || "pry_gist.rb",
    public: !!opts[:p] # rubocop:disable Style/DoubleNegation
  )
  return unless response

  url = response['html_url']
  message = "Gist created at URL #{url}"
  begin
    ::Gist.copy(url)
    message << ", which is now in the clipboard."
  rescue ::Gist::ClipboardError # rubocop:disable Lint/HandleExceptions
  end

  output.puts message
end

#input_contentObject

rubocop:disable Metrics/AbcSize

Since:

  • v1.0.0



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pry-jist/gist.rb', line 68

def input_content # rubocop:disable Metrics/AbcSize
  content = ""
  Pry::Command::CodeCollector.input_expression_ranges.each do |range|
    input_expressions = _pry_.input_ring[range] || []
    Array(input_expressions).each_with_index do |code, index|
      corrected_index = index + range.first
      next unless code && code != ""

      content << code
      next unless code !~ /;\Z/

      content << comment_expression_result_for_gist(
        _pry_.config.gist.inspecter.call(_pry_.output_ring[corrected_index])
      ).to_s
    end
  end

  content
end

#options(opt) ⇒ Object

Since:

  • v1.0.0



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
# File 'lib/pry-jist/gist.rb', line 20

def options(opt)
  @input_expression_ranges = []
  @output_result_ranges = []

  opt.on :l, :lines, "Restrict to a subset of lines. Takes a line number or range",
         optional_argument: true, as: Range, default: 1..-1
  opt.on :o, :out, "Select lines from Pry's output result history. Takes an index " \
                   "or range",
         optional_argument: true, as: Range, default: -5..-1 do |r|
    output_result_ranges << (r || (-5..-1))
  end
  opt.on :i, :in, "Select lines from Pry's input expression history. Takes an " \
                  "index or range",
         optional_argument: true, as: Range, default: -5..-1 do |r|
    input_expression_ranges << (r || (-5..-1))
  end
  opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse " \
                     "the ancestors",
         as: :count
  opt.on :d, :doc, "Select lines from the code object's documentation"
  opt.on :login, "Authenticate the gist gem with GitHub"
  opt.on :p, :public, "Create a public gist (default: false)", default: false
  opt.on :clip, "Copy the selected content to clipboard instead, do NOT " \
                "gist it", default: false
end

#processObject

rubocop:disable Metrics/AbcSize

Raises:

  • (Pry::CommandError)

Since:

  • v1.0.0



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pry-jist/gist.rb', line 46

def process # rubocop:disable Metrics/AbcSize
  return ::Gist.login! if opts.present?(:login)

  cc = Pry::Command::CodeCollector.new(args, opts, _pry_)

  raise Pry::CommandError, "Found no code to gist." if cc.content =~ /\A\s*\z/

  if opts.present?(:clip)
    clipboard_content(cc.content)
  else
    # we're overriding the default behavior of the 'in' option (as
    # defined on CodeCollector) with our local behaviour.
    content = opts.present?(:in) ? input_content : cc.content
    gist_content content, cc.file
  end
end