Sha256: fd5cf721285dacb5317a3232e242af4618616b273c4a5a961fa5456e86c3a87c

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

require "pry-singular/version"
require "pry-singular/slop"
require "pry-singular/option"
require "pry"

module PrySingular
  class << self
    def make_commands(*klasses, **options)
      options = Options.new(options)
      klasses.each do |klass|
        create_singular_method_commands(klass, options)
      end
    end

    private

    def create_singular_method_commands(klass, options)
      filtered_singular_methods = filter_methods_by_option(klass.singleton_methods, options)

      filtered_singular_methods.each do |singular_method|
        singular_method_command(singular_method, klass)
      end
    end

    def filter_methods_by_option(methods, options)
      if options.only.any?
        options.remove_methods_other_than_only(methods)
      else
        options.remove_except_methods(methods)
      end
    end

    def singular_method_command(singular_method, klass)
      import_pry_command do
        command singular_method.to_s, "#{klass}.#{singular_method}" do
          last_cui_command = Readline::HISTORY.to_a.last

          klass.class_eval <<~EOS, __FILE__, __LINE__ + 1
            #{PrySingular::Slop.parse_singular_method_command(last_cui_command)}
          EOS
        end
      end
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

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