Sha256: 48cd7d8d26a15f73d7e6dcdb4c855e0b0dca1b235ef5741a55496d1986857c5f

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require "pry-singular/version"
require 'pry-singular/extract_pry_singular_options'
require 'pry'

module PrySingular
  class << self
    def set_class(*klasses)
      options = klasses.extract_pry_singular_options!
      normalize_pry_singular_options!(options)
      klasses.each do |klass|
        import_class_command(klass, options)
      end
    end

    private

    def normalize_pry_singular_options!(options)
      options[:only] = Array(options[:only])
      options[:except] = Array(options[:except])
    end

    def import_class_command(klass, options)
      singular_methods = adapt_options_singleton_methods(klass, options)
      set_pry_command do
        singular_methods.each do |klass_method|
          command "#{klass_method}", "#{klass}.#{klass_method}" do
            klass.class_eval <<-EOS
              #{parse_readline!(Readline::HISTORY.to_a.last)}
            EOS
          end
        end
      end
    end

    def set_pry_command(&block)
      commands = Pry::CommandSet.new &block
      Pry.config.commands.import(commands)
    end

    def adapt_options_singleton_methods(klass, options)
      if options[:only].any?
        return options[:only].select { |method_name| klass.respond_to?(method_name) }
      end
      klass.singleton_methods - options[:except]
    end

    def parse_readline!(readline_history_element)
      method, args = readline_history_element.split(" ", 2)
      method + ' ' + args.gsub(' ', '')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pry-singular-0.1.4 lib/pry-singular.rb