Sha256: 6fd0b7114d14839388c559285411a8e54bfd4bbc56c9ce11bd9c8b168c07e658

Contents?: true

Size: 1.51 KB

Versions: 12

Compression:

Stored size: 1.51 KB

Contents

module PryMoves

  class AddSuffix < Pry::ClassCommand

    group 'Input and Output'
    description "Continue traversing of last object in history."

    banner <<-'BANNER'
      Usage: .method | 123 | :hash_key

      Continue traversing of last object in history

      E.g. `orders` will list array, then `3` will enter `orders[3]`, then `.price` will enter `orders[3].price`
    BANNER

    def process(cmd)
      last_cmd = Pry.history.to_a[-1]
      cmd = "#{last_cmd}#{wrap_suffix(cmd)}"
      _pry_.pager.page "    > #{cmd}\n"
      _pry_.eval cmd
    end

    private

    def wrap_suffix(cmd)
      cmd
    end

  end

  class Method < AddSuffix
    match(/^\.(.+)$/) # when \. moved into group - match doesn't work because it overlaps with pry internal command

    def wrap_suffix(cmd)
      ".#{cmd}"
    end
  end

  class ArgumentCall < AddSuffix
    match(/^(\(.*\).*)/)
  end

  class ArrayIndex < AddSuffix
    match(/^(\d+)$/)

    def wrap_suffix(cmd)
      "[#{cmd}]"
    end
  end

  class ArrayCall < AddSuffix
    match(/^(\[\d+\].*)/)
  end

  class HashKey < AddSuffix
    match(/^(:\w+)$/)

    def wrap_suffix(cmd)
      "[#{cmd}]"
    end
  end


end

SUFFIX_COMMANDS = [
  PryMoves::Method,
  PryMoves::ArrayIndex,
  PryMoves::ArrayCall,
  PryMoves::HashKey
]

SUFFIX_COMMANDS.each do |cmd|
  Pry::Commands.add_command(cmd)
end

Pry::History.class_eval do

  def <<(line)
    return if ["!"].include? line
    return if SUFFIX_COMMANDS.any? do |cls|
      line.match(cls.match)
    end
    push line
  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pry-moves-1.0.12 lib/pry-moves/add_suffix.rb
pry-moves-1.0.11 lib/pry-moves/add_suffix.rb
pry-moves-1.0.10 lib/pry-moves/add_suffix.rb
pry-moves-1.0.9 lib/pry-moves/add_suffix.rb
pry-moves-1.0.8 lib/pry-moves/add_suffix.rb
pry-moves-1.0.7 lib/pry-moves/add_suffix.rb
pry-moves-1.0.6 lib/pry-moves/add_suffix.rb
pry-moves-1.0.5 lib/pry-moves/add_suffix.rb
pry-moves-1.0.4 lib/pry-moves/add_suffix.rb
pry-moves-1.0.3 lib/pry-moves/add_suffix.rb
pry-moves-1.0.2 lib/pry-moves/add_suffix.rb
pry-moves-1.0.1 lib/pry-moves/add_suffix.rb