Sha256: 417bedee3a80afdf4ff3ec2382e356b75da3bd20a47ec3ee6ca5d78b042791c4

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require 'pry-singular/version'
require 'pry-singular/extract_pry_singular_options'
require 'pry-singular/parse_readline'
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
            extend PrySingular::Slop
            klass.class_eval <<-EOS
              #{parse_singular_method_command(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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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