methodsolver 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -17
- data/bin/console +2 -9
- data/examples/solve.rb +8 -6
- data/lib/methodsolver.rb +28 -7
- data/lib/methodsolver/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 955fbe18661fe270738fb516eec66989a8b1c2b4
|
4
|
+
data.tar.gz: 10d0c0e22b2fe44acd4c8c6d523756922de03336
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5424178e647cabc858ff4b59260659d248eb4a6d3d23aed7ceaa776ae34a01551ed74f5c45a8e65c1fc1da5bf21df34b9ea0bf10bf82ed6dcb636198c409c804
|
7
|
+
data.tar.gz: 08d46010374694ac5343a1777b23096c531f1f9498e897f85a1acab9168370ecf32423072ca3045ab30d5f6642ab9188fcf540066ce3f56c5c433e1935ec7a8a
|
data/README.md
CHANGED
@@ -5,32 +5,28 @@ Finds ruby methods given a block with placeholder.
|
|
5
5
|
For example:
|
6
6
|
|
7
7
|
```ruby
|
8
|
-
solve {
|
8
|
+
solve {
|
9
|
+
h = { a: 1, bunny: 2, c: 3 }
|
10
|
+
h.______(:bunny)
|
11
|
+
h.keys == [:a, :c]
|
12
|
+
}
|
9
13
|
```
|
10
14
|
|
11
|
-
Will find `#
|
15
|
+
Will find `#delete`
|
12
16
|
|
13
|
-
Use with caution
|
17
|
+
Use with caution!
|
14
18
|
|
15
|
-
The solver attempts to
|
19
|
+
Beware of side effects. The solver attempts to execute the block with arbitrary methods found on the receiver. Append the symbol of dangerous methods to `Methodsolver::BLACKLIST` in order to blacklist them. Setters and bang methods are blacklisted by default.
|
16
20
|
|
17
|
-
##
|
21
|
+
## Usage
|
18
22
|
|
19
|
-
|
23
|
+
Please refer to `examples/solve.rb` (and the rspec tests) for more examples.
|
20
24
|
|
21
|
-
|
22
|
-
cd methodsolver
|
23
|
-
bundle
|
24
|
-
bundle exec pry
|
25
|
+
## Installation
|
25
26
|
|
26
|
-
|
27
|
+
Get this gem:
|
27
28
|
|
28
|
-
|
29
|
-
require 'methodsolver'
|
30
|
-
solve { 'lettuce'.foo == 7 }
|
31
|
-
```
|
32
|
-
|
33
|
-
Please refer to `examples/solve.rb` (and the rspec tests) for more examples.
|
29
|
+
gem install methodsolver
|
34
30
|
|
35
31
|
## Contributing
|
36
32
|
|
data/bin/console
CHANGED
@@ -3,12 +3,5 @@
|
|
3
3
|
require "bundler/setup"
|
4
4
|
require "methodsolver"
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start
|
6
|
+
require "pry"
|
7
|
+
Pry.start
|
data/examples/solve.rb
CHANGED
@@ -1,21 +1,23 @@
|
|
1
1
|
require 'methodsolver'
|
2
2
|
|
3
|
-
words = %w(the quick brown fox jumps over the lazy dog)
|
4
|
-
|
5
3
|
solve {
|
6
|
-
words
|
4
|
+
words = %w(the quick brown fox jumps over the lazy dog)
|
5
|
+
words.foo == 'the'
|
7
6
|
}
|
8
7
|
|
9
8
|
solve {
|
10
|
-
words
|
9
|
+
words = %w(the quick brown fox jumps over the lazy dog)
|
10
|
+
words.foo(%w(fox dog)) == %w(the quick brown jumps over the lazy)
|
11
11
|
}
|
12
12
|
|
13
13
|
solve {
|
14
|
-
|
14
|
+
words = %w(the quick brown fox jumps over the lazy dog)
|
15
|
+
Numeric === words.foo
|
15
16
|
}
|
16
17
|
|
17
18
|
solve {
|
18
|
-
|
19
|
+
words = %w(the quick brown fox jumps over the lazy dog)
|
20
|
+
Hash === words.foo(&:itself)
|
19
21
|
}
|
20
22
|
|
21
23
|
solve {
|
data/lib/methodsolver.rb
CHANGED
@@ -10,7 +10,7 @@ module Methodsolver
|
|
10
10
|
|
11
11
|
begin
|
12
12
|
Object.class_eval('def method_missing(name, *args); throw :undefined_method, [self, name]; end')
|
13
|
-
|
13
|
+
receiver, placeholder = catch :undefined_method do
|
14
14
|
block.call
|
15
15
|
raise ArgumentError, 'no missing method found'
|
16
16
|
end
|
@@ -18,10 +18,12 @@ module Methodsolver
|
|
18
18
|
Object.class_eval('remove_method :method_missing')
|
19
19
|
end
|
20
20
|
|
21
|
+
somewhat_limited_safety_checks(receiver, block) unless options[:unsafe]
|
22
|
+
|
21
23
|
# Find methods that pass the block:
|
22
24
|
|
23
|
-
results = methods_for(
|
24
|
-
method =
|
25
|
+
results = methods_for(receiver).select do |name|
|
26
|
+
method = receiver.method(name) rescue next
|
25
27
|
begin
|
26
28
|
method.owner.class_eval("alias #{placeholder.inspect} #{name.inspect}")
|
27
29
|
true === block.call
|
@@ -35,7 +37,7 @@ module Methodsolver
|
|
35
37
|
# Optionally return a hash with metadata:
|
36
38
|
|
37
39
|
if options[:metadata]
|
38
|
-
{
|
40
|
+
{ receiver: receiver, placeholder: placeholder, results: results }
|
39
41
|
else
|
40
42
|
results
|
41
43
|
end
|
@@ -63,11 +65,30 @@ module Methodsolver
|
|
63
65
|
.uniq
|
64
66
|
end
|
65
67
|
|
68
|
+
SAVE = [Array, Fixnum, Float, Hash, Object, Range, Regexp, String]
|
69
|
+
|
70
|
+
def self.somewhat_limited_safety_checks(receiver, block)
|
71
|
+
return if receiver.nil?
|
72
|
+
block.binding.eval('local_variables').each do |name|
|
73
|
+
if receiver.equal? block.binding.eval("#{name} rescue nil")
|
74
|
+
receiver.clone rescue break # omit warning for immutables
|
75
|
+
raise ArgumentError,
|
76
|
+
"receiver equals local variable '#{name}', " <<
|
77
|
+
"use solve(unsafe: true) or clone/dup the variable"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
unless SAVE.include? receiver.class
|
81
|
+
raise ArgumentError,
|
82
|
+
"class of receiver not marked as save: #{receiver.class}, " <<
|
83
|
+
"use solve(unsafe: true) or append to Methodsolver::SAVE"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
66
87
|
end
|
67
88
|
|
68
|
-
def solve(&block)
|
69
|
-
data = Methodsolver.call(metadata: true, &block)
|
70
|
-
object, found = data[:
|
89
|
+
def solve(options = {}, &block)
|
90
|
+
data = Methodsolver.call(options.merge(metadata: true), &block)
|
91
|
+
object, found = data[:receiver], data[:results]
|
71
92
|
if block.respond_to? :method_source
|
72
93
|
puts "Found #{found.count} methods for #{block.method_source.strip}"
|
73
94
|
else
|
data/lib/methodsolver/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: methodsolver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adrian Kuhn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|