Permalink
Browse files

Now uses a block instead of subclasses.

e.g PryAutoPilot.new do
      on ->(frame) { frame.method_name == :bing } do |input|
        input << "ls"
        input.interactive!
      end

      on ->(frame) { true } do |input|
        input << "step"
      end
    end

`on` is now an instance method rather than a class method.
  • Loading branch information...
1 parent 2fb21c1 commit 4c5214899da789e046190f496000ba531e8a1242 @banister banister committed Jul 30, 2012
Showing with 65 additions and 50 deletions.
  1. +10 −21 examples/example2.rb
  2. +24 −29 lib/pry-autopilot.rb
  3. +31 −0 lib/pry-autopilot/input.rb
View
@@ -2,30 +2,19 @@
require 'pry'
require 'pry-autopilot'
-class MyPilot < PryAutopilot
- # on ->(frame) { frame.method_name == :bing } do
- # input << "ls"
- # input << "cd 1/2/3/4/5"
- # input << "ls -m"
- # input << "puts 'odelay!'"
- # interactive!
- # end
-
- # on ->(frame) { true } do
- # input << "ls"
- # input << "step"
- # end
-
- on ->(frame) { frame.class_name == :Pry } do
- input << "puts you're in Pry lol\n"
- input << "doing an ls!\n"
- input << "ls\n"
- input << "cd-ing into your mom\n"
- input << "cd :your_mom\n"
+my_pilot = PryAutopilot.new do
+ on ->(frame) { frame.method_name == :gamma } do |input|
+ input << "puts 'welcome to gamma!'"
+ input.interactive!
+ end
+
+ on ->(frame) { true } do |input|
+ input << "ls"
+ input << "step"
end
end
-Pry.config.input = MyPilot.new
+Pry.config.input = my_pilot
def alpha
x = "hello"
View
@@ -2,57 +2,52 @@
require "pry-autopilot/version"
require "pry-autopilot/frame"
+require "pry-autopilot/input"
class PryAutopilot
- class << self
- attr_reader :predicates
-
- def on(predicate, &block)
- @predicates ||= []
- @predicates << [predicate, block]
- end
- end
-
attr_accessor :input
+ attr_reader :predicates
+ attr_accessor :_pry_
- def initialize
- @input = []
- end
-
- def set_pry(_pry_)
- @pry = _pry_
+ def initialize(fallback_input=Readline, &block)
+ @input = Input.new(self)
+ @fallback_input = fallback_input
+ instance_exec(&block) if block
end
def readline(prompt)
process_predicates if input.empty?
- input.shift || Readline.readline(prompt)
+ input.shift || fallback_readline(prompt)
+ end
+
+ def on(predicate, &block)
+ @predicates ||= []
+ @predicates << [predicate, block]
end
private
def frame
- Frame.new(@pry.current_context)
+ Frame.new(@_pry_.current_context)
+ end
+
+ def fallback_readline(prompt)
+ if @fallback_input.method(:readline).arity == 0
+ @fallback_input.readline
+ else
+ @fallback_input.readline(prompt)
+ end
end
def process_predicates
return if !predicates
predicates.each do |predicate, block|
if predicate.call(frame)
- instance_exec(&block)
+ block.call(input)
end
end
end
-
- def predicates
- self.class.predicates
- end
-
- def interactive!
- input << "_pry_.input = Readline"
- end
end
-Pry.config.correct_indent = true
-
Pry.config.hooks.add_hook(:when_started, :init_autopilot) do |_, _, _pry_|
- _pry_.input.set_pry(_pry_) if _pry_.input.is_a?(PryAutopilot)
+ _pry_.input._pry_ = _pry_ if _pry_.input.is_a?(PryAutopilot)
end
View
@@ -0,0 +1,31 @@
+require 'forwardable'
+
+class PryAutopilot
+ class Input
+ extend Forwardable
+
+ def initialize(pilot)
+ @pilot = pilot
+ @array = []
+ end
+
+ def_delegator :@pilot, :_pry_
+
+ def <<(value)
+ @array << "#{value}\n"
+ self
+ end
+
+ def empty?
+ @array.empty?
+ end
+
+ def shift
+ @array.shift
+ end
+
+ def interactive!
+ @array << "_pry_.input = Readline"
+ end
+ end
+end

0 comments on commit 4c52148

Please sign in to comment.