Class: WhatsUp::MethodFinder
- Defined in:
- lib/whats_up/classic.rb,
lib/whats_up/method_finder.rb
Constant Summary collapse
- @@blacklist =
%w(daemonize display exec exit! fork sleep system syscall what_equals whats_exactly what_matches what_works_with what_works whats_not_blank_with whats_not_blank given ed emacs mate nano vi vim)
- @@infixes =
%w(+ - * / % ** == != =~ !~ !=~ > < >= <= <=> === & | ^ << >>).map(&:to_sym)
- @@prefixes =
%w(+@ -@ ~ !).map(&:to_sym)
Class Method Summary collapse
- .build_check_lambda(expected_result, opts = {}) ⇒ Object
-
.find(an_object, expected_result, opts = {}, *args, &block) ⇒ Object
Find all methods on [an_object] which, when called with [args] return [expected_result].
-
.show(an_object, expected_result, opts = {}, *args, &block) ⇒ Object
Pretty-prints the results of the previous method.
Instance Method Summary collapse
- #==(val) ⇒ Object
-
#initialize(obj, *args) ⇒ MethodFinder
constructor
A new instance of MethodFinder.
Constructor Details
#initialize(obj, *args) ⇒ MethodFinder
Returns a new instance of MethodFinder.
9 10 11 12 |
# File 'lib/whats_up/method_finder.rb', line 9 def initialize(obj, *args) @obj = obj @args = args end |
Class Method Details
.build_check_lambda(expected_result, opts = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/whats_up/method_finder.rb', line 18 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.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]
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/whats_up/method_finder.rb', line 38 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 $stdout = $stderr = DummyOut.new # 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| begin stdout.print "" 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 results end |
.show(an_object, expected_result, opts = {}, *args, &block) ⇒ Object
Pretty-prints the results of the previous method
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/whats_up/method_finder.rb', line 66 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 do |key, value| puts "#{key.ljust max_length} == #{value}" end found end |
Instance Method Details
#==(val) ⇒ Object
13 14 15 |
# File 'lib/whats_up/method_finder.rb', line 13 def ==(val) MethodFinder.show(@obj, val, *@args) end |