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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6f2c0e313ebc463f001136e17e06df659e86933
4
- data.tar.gz: 92bc53f31083ecdcbe0c8ae18e13e4820195c092
3
+ metadata.gz: 955fbe18661fe270738fb516eec66989a8b1c2b4
4
+ data.tar.gz: 10d0c0e22b2fe44acd4c8c6d523756922de03336
5
5
  SHA512:
6
- metadata.gz: 6ca8fbed39d19eb7dc3256000674b408d01f9b7a45c1083b3e43c1a3d44bc0ecf5876e3a62a4c8fb6d5825033197f0ebc3b36dde241aac08faa160ef1e38904e
7
- data.tar.gz: b8952babf725dec3a443d54c1da155c72fbcb261362fe10f11e58717c1143d86f8757fe8564371a9961f0c047cef0086bd073fb5b2d9f6c46e96397e43bdbf89
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 { 'lettuce'.foo == 7 }
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 `#length` and `#size`
15
+ Will find `#delete`
12
16
 
13
- Use with caution and beware of side effects!
17
+ Use with caution!
14
18
 
15
- The solver attempts to executes the block with arbitrary methods found on the reciever. Append the symbol of dangerous methods to `Methodsolver::BLACKLIST` in order to blacklist them.
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
- ## Installation
21
+ ## Usage
18
22
 
19
- Clone this repo and run pry:
23
+ Please refer to `examples/solve.rb` (and the rspec tests) for more examples.
20
24
 
21
- git clone https://github.com/akuhn/methodsolver.git
22
- cd methodsolver
23
- bundle
24
- bundle exec pry
25
+ ## Installation
25
26
 
26
- And then execute:
27
+ Get this gem:
27
28
 
28
- ```ruby
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
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
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.dup.foo == 'the'
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.dup.foo(%w(fox dog)) == %w(the quick brown jumps over the lazy)
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
- Numeric === words.dup.foo
14
+ words = %w(the quick brown fox jumps over the lazy dog)
15
+ Numeric === words.foo
15
16
  }
16
17
 
17
18
  solve {
18
- Hash === words.dup.foo(&:itself)
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
- reciever, placeholder = catch :undefined_method do
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(reciever).select do |name|
24
- method = reciever.method(name) rescue next
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
- { reciever: reciever, placeholder: placeholder, results: results }
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[:reciever], data[:results]
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
@@ -1,3 +1,3 @@
1
1
  module Methodsolver
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
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.3
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-21 00:00:00.000000000 Z
11
+ date: 2015-07-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: