lib/pry-singular.rb in pry-singular-0.1.7 vs lib/pry-singular.rb in pry-singular-0.1.8
- old
+ new
@@ -1,50 +1,50 @@
-require 'pry-singular/version'
-require 'pry-singular/extract_pry_singular_options'
-require 'pry-singular/parse_readline'
-require 'pry'
+require "pry-singular/version"
+require "pry-singular/slop"
+require "pry-singular/option"
+require "pry"
module PrySingular
class << self
- def set_class(*klasses)
- options = klasses.extract_pry_singular_options!
- normalize_pry_singular_options!(options)
+ def make_commands(*klasses, **options)
+ options = Options.new(options)
klasses.each do |klass|
- import_class_command(klass, options)
+ create_singular_method_commands(klass, options)
end
end
private
- def normalize_pry_singular_options!(options)
- options[:only] = Array(options[:only])
- options[:except] = Array(options[:except])
- end
+ def create_singular_method_commands(klass, options)
+ filtered_singular_methods = filter_methods_by_option(klass.singleton_methods, options)
- 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
+ filtered_singular_methods.each do |singular_method|
+ singular_method_command(singular_method, klass)
end
end
- def set_pry_command(&block)
- commands = Pry::CommandSet.new &block
- Pry.config.commands.import(commands)
+ 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 adapt_options_singleton_methods(klass, options)
- if options[:only].any?
- return options[:only].select { |method_name| klass.respond_to?(method_name) }
+ 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
- klass.singleton_methods - options[:except]
+ def import_pry_command(&block)
+ commands = Pry::CommandSet.new(&block)
+ Pry.config.commands.import(commands)
end
end
end