Sha256: 3bdbe957bc38bc241d3ccddf689e8b32ceee87bcd23107aaa9b5469a697c7c4e

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 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
              #{Readline::HISTORY.to_a.last.gsub(' ', '')}
            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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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