Class: WhatsUp::MethodFinder
Overview
A singleton class used to iterate over the methods of an object trying to match any returned values with an expected result. ny matches will then be pretty printed to the console.
Constant Summary collapse
- @@blacklist =
A list of symbols indicated which methods to always ignore
%w(daemonize debug debugger display ed emacs exactly? exec exit! fork given matches? mate nano not_blank? sleep stub stub! stub_chain syscall system unstub unstub! vi vim what? what_equals what_matches what_works what_works_with whats_exactly whats_not_blank whats_not_blank_with works?).map(&:to_sym)
- @@infixes =
A list of symbols for infix operators for which Ruby has special syntax
%w(+ - * / % ** == != =~ !~ !=~ > < >= <= <=> === & | ^ << >>).map(&:to_sym)
- @@prefixes =
A list of symbols for prefix operators for which Ruby has special syntax
%w(+@ -@ ~ !).map(&:to_sym)
Class Method Summary collapse
-
.build_check_lambda(expected_result, opts = {}) ⇒ Object
Builds a lambda for checking against the provided
expected_resultgiven a hash of options. -
.find(an_object, expected_result, opts = {}, *args, &block) ⇒ Object
Find all methods on
an_objectwhich, when called withargsreturnexpected_result. -
.show(an_object, expected_result, opts = {}, *args, &block) ⇒ Object
Pretty prints the results of #find.
Class Method Details
.build_check_lambda(expected_result, opts = {}) ⇒ Object
Builds a lambda for checking against the provided expected_result given a hash of options. Given the value of a method, the result of this lambda will determine whether that method and value are included in the output of a whats_up method.
Options
-
:exclude_blank- Exclude blank values -
:force_exact- Force values to be exactly equal -
:force_regex- Coerce theexpected_resultinto a regular expression for pattern matching -
:show_all- Show the results of all methods
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/whats_up/method_finder.rb', line 29 def build_check_lambda(expected_result, opts = {}) if opts[:force_regex] expected_result = Regexp.new(expected_result.to_s) unless expected_result.is_a?(Regexp) ->(value) { expected_result === value.to_s } elsif expected_result.is_a?(Regexp) && !opts[:force_exact] ->(value) { expected_result === value.to_s } elsif opts[:force_exact] ->(value) { expected_result.eql?(value) } elsif opts[:show_all] if opts[:exclude_blank] ->(value) { !value.nil? && !(value.respond_to?(:empty?) && value.empty?) } else ->(value) { true } end else ->(value) { expected_result == value } end end |
.find(an_object, expected_result, opts = {}, *args, &block) ⇒ Object
Find all methods on an_object which, when called with args return expected_result
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/whats_up/method_finder.rb', line 49 def find(an_object, expected_result, opts = {}, *args, &block) check_result = build_check_lambda(expected_result, opts) # Prevent any writing to the terminal stdout, stderr = $stdout, $stderr unless $stdout.is_a?(DummyOut) $stdout = $stderr = DummyOut.new restore_std = true end # Use only methods with valid arity that aren't blacklisted methods = an_object.methods methods.select! { |n| an_object.method(n).arity <= args.size && !@@blacklist.include?(n) } # Collect all methods equaling the expected result results = methods.inject({}) do |res, name| stdout.print "" begin value = an_object.clone.method(name).call(*args, &block) res[name] = value if check_result.call(value) rescue end res end # Restore printing to the terminal $stdout, $stderr = stdout, stderr if restore_std results end |
.show(an_object, expected_result, opts = {}, *args, &block) ⇒ Object
Pretty prints the results of #find
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/whats_up/method_finder.rb', line 80 def show(an_object, expected_result, opts = {}, *args, &block) opts = { exclude_blank: false, force_exact: false, force_regex: false, show_all: false }.merge(opts) found = find(an_object, expected_result, opts, *args, &block) prettified = prettify_found(an_object, found, *args) max_length = prettified.map { |k, v| k.length }.max prettified.each { |k, v| puts "#{k.ljust max_length} == #{v}" } found end |