whats_up 1.1.2 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +11 -2
- data/lib/whats_up/dummy_out.rb +7 -0
- data/lib/whats_up/frozen_section.rb +17 -0
- data/lib/whats_up/method_finder.rb +132 -0
- data/lib/whats_up/version.rb +1 -1
- data/lib/whats_up.rb +30 -145
- data/spec/frozen_section_spec.rb +23 -0
- metadata +7 -2
data/README.md
CHANGED
@@ -38,9 +38,12 @@ See if this suits your console cravings:
|
|
38
38
|
=> {:psych_to_yaml=>"--- 3.45\n...\n", :to_yaml=>"--- 3.45\n...\n", :pretty_inspect=>"3.45\n"}
|
39
39
|
|
40
40
|
> 3.what_equals 4, 1
|
41
|
-
3 + 1
|
41
|
+
3 + 1 == 4
|
42
42
|
=> {:+=>4}
|
43
43
|
|
44
|
+
> 3.given(1).what_equals 4
|
45
|
+
3 + 1 == 4
|
46
|
+
|
44
47
|
Just what you need in the console.
|
45
48
|
|
46
49
|
Notice the last example: you can pass parameters after the desired result. whats_up will tell you
|
@@ -52,6 +55,12 @@ This modest little update retains the original `what?` method, but that was from
|
|
52
55
|
2006, before the Ruby community had a rough consensus that `?` methods should be returning a true or
|
53
56
|
false value (or at least something useable as such). I've aliased that method as `what_equals`.
|
54
57
|
|
58
|
+
You can add arguments by either supplying additional arguments after the expected result or by using
|
59
|
+
the `given` method. So the following two queries are equivalent:
|
60
|
+
|
61
|
+
5.given(1).what_equals 6
|
62
|
+
5.what_equals 6, 1
|
63
|
+
|
55
64
|
Note also the addition of helpers like `whats_exactly`, which will only find exact matches, and
|
56
65
|
`what_matches`, which will match a regular expression:
|
57
66
|
|
@@ -83,7 +92,7 @@ current methods and `whats_not_blank_with` that ignores any false, falsy or empt
|
|
83
92
|
"hello".upto(2) == #<Enumerator: "hello":upto(2)>
|
84
93
|
# ...
|
85
94
|
|
86
|
-
> "hello".
|
95
|
+
> "hello".given(2).whats_not_blank
|
87
96
|
"hello" * 2 == "hellohello"
|
88
97
|
"hello" % 2 == "hello"
|
89
98
|
"hello"[2] == "l"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module WhatsUp
|
2
|
+
class FrozenSection
|
3
|
+
def initialize(object, vars = {})
|
4
|
+
@object = object
|
5
|
+
vars.each do |key, value|
|
6
|
+
instance_variable_set "@#{key}", value
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def show_methods
|
13
|
+
@args = args unless args.empty?
|
14
|
+
WhatsUp::MethodFinder.show(@object, expected_result, opts, *@args)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
module WhatsUp
|
2
|
+
class MethodFinder
|
3
|
+
@@blacklist = %w(daemonize display exec exit! fork sleep system syscall what_equals
|
4
|
+
whats_exactly what_matches what_works_with what_works whats_not_blank_with
|
5
|
+
whats_not_blank given ed emacs mate nano vi vim)
|
6
|
+
@@infixes = %w(+ - * / % ** == != =~ !~ !=~ > < >= <= <=> === & | ^ << >>).map(&:to_sym)
|
7
|
+
@@prefixes = %w(+@ -@ ~ !).map(&:to_sym)
|
8
|
+
|
9
|
+
def initialize(obj, *args)
|
10
|
+
@obj = obj
|
11
|
+
@args = args
|
12
|
+
end
|
13
|
+
def ==(val)
|
14
|
+
MethodFinder.show(@obj, val, *@args)
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def build_check_lambda(expected_result, opts = {})
|
19
|
+
if opts[:force_regex]
|
20
|
+
expected_result = Regexp.new(expected_result.to_s) unless expected_result.is_a?(Regexp)
|
21
|
+
-> value { expected_result === value.to_s }
|
22
|
+
elsif expected_result.is_a?(Regexp) && !opts[:force_exact]
|
23
|
+
-> value { expected_result === value.to_s }
|
24
|
+
elsif opts[:force_exact]
|
25
|
+
-> value { expected_result.eql?(value) }
|
26
|
+
elsif opts[:show_all]
|
27
|
+
if opts[:exclude_blank]
|
28
|
+
-> value { !value.nil? && !value.empty? }
|
29
|
+
else
|
30
|
+
-> value { true }
|
31
|
+
end
|
32
|
+
else
|
33
|
+
-> value { expected_result == value }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Find all methods on [an_object] which, when called with [args] return [expected_result]
|
38
|
+
def find(an_object, expected_result, opts = {}, *args, &block)
|
39
|
+
check_result = build_check_lambda(expected_result, opts)
|
40
|
+
|
41
|
+
# Prevent any writing to the terminal
|
42
|
+
stdout, stderr = $stdout, $stderr
|
43
|
+
$stdout = $stderr = DummyOut.new
|
44
|
+
|
45
|
+
# Use only methods with valid arity that aren't blacklisted
|
46
|
+
methods = an_object.methods
|
47
|
+
methods.select! { |n| an_object.method(n).arity <= args.size && !@@blacklist.include?(n) }
|
48
|
+
|
49
|
+
# Collect all methods equaling the expected result
|
50
|
+
results = methods.inject({}) do |res, name|
|
51
|
+
begin
|
52
|
+
stdout.print ""
|
53
|
+
value = an_object.clone.method(name).call(*args, &block)
|
54
|
+
res[name] = value if check_result.call(value)
|
55
|
+
rescue
|
56
|
+
end
|
57
|
+
res
|
58
|
+
end
|
59
|
+
|
60
|
+
# Restore printing to the terminal
|
61
|
+
$stdout, $stderr = stdout, stderr
|
62
|
+
results
|
63
|
+
end
|
64
|
+
|
65
|
+
# Pretty-prints the results of the previous method
|
66
|
+
def show(an_object, expected_result, opts = {}, *args, &block)
|
67
|
+
opts = {
|
68
|
+
force_regex: false,
|
69
|
+
force_exact: false,
|
70
|
+
show_all: false,
|
71
|
+
exclude_blank: false
|
72
|
+
}.merge(opts)
|
73
|
+
|
74
|
+
found = find(an_object, expected_result, opts, *args, &block)
|
75
|
+
prettified = prettify_found(an_object, found, *args)
|
76
|
+
max_length = prettified.map { |k, v| k.length }.max
|
77
|
+
|
78
|
+
prettified.each do |key, value|
|
79
|
+
puts "#{key.ljust max_length} == #{value}"
|
80
|
+
end
|
81
|
+
|
82
|
+
found
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
# Pretty prints a method depending on whether it's an operator, has arguments, is array/hash
|
88
|
+
# syntax, etc. For example:
|
89
|
+
#
|
90
|
+
#
|
91
|
+
def prettify_found(an_object, found, *args)
|
92
|
+
args = args.map { |o| o.inspect }.join(", ")
|
93
|
+
pretty_object = truncate_inspect(an_object, to: 40)
|
94
|
+
|
95
|
+
found.map do |key, value|
|
96
|
+
pretty_key = if @@infixes.include?(key)
|
97
|
+
"#{pretty_object} #{key} #{args}"
|
98
|
+
elsif @@prefixes.include?(key)
|
99
|
+
"#{key.to_s.sub /\@$/, ""}#{pretty_object}"
|
100
|
+
elsif key == :[]
|
101
|
+
"#{pretty_object}[#{args}]"
|
102
|
+
elsif args != ""
|
103
|
+
"#{pretty_object}.#{key}(#{args})"
|
104
|
+
else
|
105
|
+
"#{pretty_object}.#{key}"
|
106
|
+
end
|
107
|
+
|
108
|
+
pretty_value = truncate_inspect(value, to: 120)
|
109
|
+
|
110
|
+
[pretty_key, pretty_value]
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# Inspects an object and returns a string representation, truncating it to length in the
|
115
|
+
# provided <tt>to:</tt> argument if necessary
|
116
|
+
def truncate_inspect(object, opts = {})
|
117
|
+
max_length = opts[:to] || 80
|
118
|
+
full = object.inspect
|
119
|
+
|
120
|
+
if full.length > max_length
|
121
|
+
available_length = max_length - 5 # to account for the " ... "
|
122
|
+
left_cutoff = available_length * 2 / 3.0
|
123
|
+
right_cutoff = available_length - left_cutoff - 1
|
124
|
+
|
125
|
+
"#{full[0..left_cutoff]} ... #{full[-right_cutoff..-1]}"
|
126
|
+
else
|
127
|
+
full
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
data/lib/whats_up/version.rb
CHANGED
data/lib/whats_up.rb
CHANGED
@@ -24,25 +24,36 @@
|
|
24
24
|
# Last updated: 2006/05/20
|
25
25
|
|
26
26
|
class Object
|
27
|
-
def
|
28
|
-
|
27
|
+
def given(*args)
|
28
|
+
if frozen?
|
29
|
+
WhatsUp::FrozenSection.new self, args: args
|
30
|
+
else
|
31
|
+
@args = args
|
32
|
+
self
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def what_equals(expected_result, *args)
|
37
|
+
show_methods expected_result, {}, *args
|
29
38
|
end
|
30
39
|
|
31
|
-
def whats_exactly(*
|
32
|
-
|
40
|
+
def whats_exactly(expected_result, *args)
|
41
|
+
show_methods expected_result, { force_exact: true }, *args
|
33
42
|
end
|
34
43
|
|
35
|
-
def what_matches(*
|
36
|
-
|
44
|
+
def what_matches(expected_result, *args)
|
45
|
+
show_methods expected_result, { force_regex: true }, *args
|
37
46
|
end
|
38
47
|
|
39
|
-
def what_works_with(*
|
40
|
-
|
48
|
+
def what_works_with(*args)
|
49
|
+
show_methods nil, { show_all: true }, *args
|
41
50
|
end
|
51
|
+
alias :what_works :what_works_with
|
42
52
|
|
43
|
-
def whats_not_blank_with(*
|
44
|
-
|
53
|
+
def whats_not_blank_with(*args)
|
54
|
+
show_methods nil, { show_all: true, exclude_blank: true }, *args
|
45
55
|
end
|
56
|
+
alias :whats_not_blank :whats_not_blank_with
|
46
57
|
|
47
58
|
# Make sure cloning doesn't cause anything to fail via type errors
|
48
59
|
alias_method :__clone__, :clone
|
@@ -51,143 +62,17 @@ class Object
|
|
51
62
|
rescue TypeError
|
52
63
|
self
|
53
64
|
end
|
54
|
-
end
|
55
65
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
66
|
+
private
|
67
|
+
|
68
|
+
def show_methods(expected_result, opts = {}, *args)
|
69
|
+
@args = args unless args.empty?
|
70
|
+
WhatsUp::MethodFinder.show(self, expected_result, opts, *@args)
|
71
|
+
end
|
60
72
|
end
|
61
73
|
|
62
74
|
module WhatsUp
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
@@infixes = %w(+ - * / % ** == != =~ !~ !=~ > < >= <= <=> === & | ^ << >>).map(&:to_sym)
|
67
|
-
@@prefixes = %w(+@ -@ ~ !).map(&:to_sym)
|
68
|
-
|
69
|
-
def initialize(obj, *args)
|
70
|
-
@obj = obj
|
71
|
-
@args = args
|
72
|
-
end
|
73
|
-
def ==(val)
|
74
|
-
MethodFinder.show(@obj, val, *@args)
|
75
|
-
end
|
76
|
-
|
77
|
-
class << self
|
78
|
-
def build_check_lambda(expected_result, opts = {})
|
79
|
-
if opts[:force_regex]
|
80
|
-
expected_result = Regexp.new(expected_result.to_s) unless expected_result.is_a?(Regexp)
|
81
|
-
-> value { expected_result === value.to_s }
|
82
|
-
elsif expected_result.is_a?(Regexp) && !opts[:force_exact]
|
83
|
-
-> value { expected_result === value.to_s }
|
84
|
-
elsif opts[:force_exact]
|
85
|
-
-> value { expected_result.eql?(value) }
|
86
|
-
elsif opts[:show_all]
|
87
|
-
if opts[:exclude_blank]
|
88
|
-
-> value { !value.nil? && !value.empty? }
|
89
|
-
else
|
90
|
-
-> value { true }
|
91
|
-
end
|
92
|
-
else
|
93
|
-
-> value { expected_result == value }
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
# Find all methods on [an_object] which, when called with [args] return [expected_result]
|
98
|
-
def find(an_object, expected_result, opts = {}, *args, &block)
|
99
|
-
check_result = build_check_lambda(expected_result, opts)
|
100
|
-
|
101
|
-
# Prevent any writing to the terminal
|
102
|
-
stdout, stderr = $stdout, $stderr
|
103
|
-
$stdout = $stderr = DummyOut.new
|
104
|
-
|
105
|
-
# Use only methods with valid arity that aren't blacklisted
|
106
|
-
methods = an_object.methods
|
107
|
-
methods.select! { |n| an_object.method(n).arity <= args.size && !@@blacklist.include?(n) }
|
108
|
-
|
109
|
-
# Collect all methods equaling the expected result
|
110
|
-
results = methods.inject({}) do |res, name|
|
111
|
-
begin
|
112
|
-
stdout.print ""
|
113
|
-
value = an_object.clone.method(name).call(*args, &block)
|
114
|
-
res[name] = value if check_result.call(value)
|
115
|
-
rescue
|
116
|
-
end
|
117
|
-
res
|
118
|
-
end
|
119
|
-
|
120
|
-
# Restore printing to the terminal
|
121
|
-
$stdout, $stderr = stdout, stderr
|
122
|
-
results
|
123
|
-
end
|
124
|
-
|
125
|
-
# Pretty-prints the results of the previous method
|
126
|
-
def show(an_object, opts = {}, *args, &block)
|
127
|
-
opts = {
|
128
|
-
force_regex: false,
|
129
|
-
force_exact: false,
|
130
|
-
show_all: false,
|
131
|
-
exclude_blank: false
|
132
|
-
}.merge(opts)
|
133
|
-
|
134
|
-
expected_result = opts[:show_all] ? nil : args.shift
|
135
|
-
found = find(an_object, expected_result, opts, *args, &block)
|
136
|
-
prettified = prettify_found(an_object, found, *args)
|
137
|
-
max_length = prettified.map { |k, v| k.length }.max
|
138
|
-
|
139
|
-
prettified.each do |key, value|
|
140
|
-
puts "#{key.ljust max_length} == #{value}"
|
141
|
-
end
|
142
|
-
|
143
|
-
found
|
144
|
-
end
|
145
|
-
|
146
|
-
private
|
147
|
-
|
148
|
-
# Pretty prints a method depending on whether it's an operator, has arguments, is array/hash
|
149
|
-
# syntax, etc. For example:
|
150
|
-
#
|
151
|
-
#
|
152
|
-
def prettify_found(an_object, found, *args)
|
153
|
-
args = args.map { |o| o.inspect }.join(", ")
|
154
|
-
pretty_object = truncate_inspect(an_object, to: 40)
|
155
|
-
|
156
|
-
found.map do |key, value|
|
157
|
-
pretty_key = if @@infixes.include?(key)
|
158
|
-
"#{pretty_object} #{key} #{args}"
|
159
|
-
elsif @@prefixes.include?(key)
|
160
|
-
"#{key.to_s.sub /\@$/, ""}#{pretty_object}"
|
161
|
-
elsif key == :[]
|
162
|
-
"#{pretty_object}[#{args}]"
|
163
|
-
elsif args != ""
|
164
|
-
"#{pretty_object}.#{key}(#{args})"
|
165
|
-
else
|
166
|
-
"#{pretty_object}.#{key}"
|
167
|
-
end
|
168
|
-
|
169
|
-
pretty_value = truncate_inspect(value, to: 120)
|
170
|
-
|
171
|
-
[pretty_key, pretty_value]
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
# Inspects an object and returns a string representation, truncating it to length in the
|
176
|
-
# provided <tt>to:</tt> argument if necessary
|
177
|
-
def truncate_inspect(object, opts = {})
|
178
|
-
max_length = opts[:to] || 80
|
179
|
-
full = object.inspect
|
180
|
-
|
181
|
-
if full.length > max_length
|
182
|
-
available_length = max_length - 5 # to account for the " ... "
|
183
|
-
left_cutoff = available_length * 2 / 3.0
|
184
|
-
right_cutoff = available_length - left_cutoff - 1
|
185
|
-
|
186
|
-
"#{full[0..left_cutoff]} ... #{full[-right_cutoff..-1]}"
|
187
|
-
else
|
188
|
-
full
|
189
|
-
end
|
190
|
-
end
|
191
|
-
end
|
192
|
-
end
|
75
|
+
autoload :DummyOut, "whats_up/dummy_out"
|
76
|
+
autoload :FrozenSection, "whats_up/frozen_section"
|
77
|
+
autoload :MethodFinder, "whats_up/method_finder"
|
193
78
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.dirname(__FILE__) + "/spec_helper"
|
3
|
+
|
4
|
+
describe FrozenSection do
|
5
|
+
describe "initialization" do
|
6
|
+
before :each do
|
7
|
+
@number = 2
|
8
|
+
@string = "hello"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should not occur given an unfrozen object" do
|
12
|
+
@number.given(1).should be_a(Fixnum)
|
13
|
+
@string.given(1).should be_a(String)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should occur given a frozen object" do
|
17
|
+
@number.freeze
|
18
|
+
@string.freeze
|
19
|
+
@number.given(1).should be_a(FrozenSection)
|
20
|
+
@string.given(1).should be_a(FrozenSection)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: whats_up
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -77,7 +77,11 @@ files:
|
|
77
77
|
- Rakefile
|
78
78
|
- lib/whats_up.rb
|
79
79
|
- lib/whats_up/classic.rb
|
80
|
+
- lib/whats_up/dummy_out.rb
|
81
|
+
- lib/whats_up/frozen_section.rb
|
82
|
+
- lib/whats_up/method_finder.rb
|
80
83
|
- lib/whats_up/version.rb
|
84
|
+
- spec/frozen_section_spec.rb
|
81
85
|
- spec/spec_helper.rb
|
82
86
|
- whats_up.gemspec
|
83
87
|
homepage: http://brymck.herokuapp.com/
|
@@ -100,9 +104,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
104
|
version: '0'
|
101
105
|
requirements: []
|
102
106
|
rubyforge_project:
|
103
|
-
rubygems_version: 1.8.
|
107
|
+
rubygems_version: 1.8.21
|
104
108
|
signing_key:
|
105
109
|
specification_version: 3
|
106
110
|
summary: Determine what methods can be called on an object that return a given value
|
107
111
|
test_files:
|
112
|
+
- spec/frozen_section_spec.rb
|
108
113
|
- spec/spec_helper.rb
|