suggest_rb 0.2.0 → 0.3.0

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
  SHA256:
3
- metadata.gz: e61c1dd1cb882f09e1c11a1f7adfe9d123aa770a28e2578a2785025bc6ee1cbc
4
- data.tar.gz: 5f3e5ed7bbaa495ba9844412c39fb47d6b686d8dec109a0aeedb4713c0f64f9f
3
+ metadata.gz: e4b15e6cb0b18984749d2abbdd32835774de3e480374dfc3d4a9dd7393f1b208
4
+ data.tar.gz: 71444761f35ef08b561a0e3f5cfc8c84061ab9ddc974f494f99bd0b2b42b163b
5
5
  SHA512:
6
- metadata.gz: 1037bab415285b2009496fb5b37e2c8b56593e5a62e5142b262fefa86978c94fedaef70b4f48ebc4fb9707a4dde5c5711843bd110a866d1c5a5e8ff523362568
7
- data.tar.gz: 544907353e5c561b26edc69b20eecca87bc4070af9284cde736f37d3fa193068748b4a260dfbd0191c538cf4783c7be84134c2a6f01f8a3a3e80368ad965b633
6
+ metadata.gz: 42fd7d3197b65a1c708533545f16c8f395574a26275fa48a32bcf26589e98b5c98e8aedbbc79201a98ec582da2b1253d6122b1907722d6588480aa039d88ee30
7
+ data.tar.gz: daa3d40c2ca895d59552d6ae4dc9c6dd37619e8647493e74d1bdc8ac0202dbadddecb80f723f4c7494e9b6a4eb26ef1bbbb0ccd7ddaecfaad056a00e08ffbf08
data/README.md CHANGED
@@ -56,3 +56,11 @@ require 'suggest'
56
56
  [1,2,3,4].what_mutates? [2,4] { |n| n % 2 == 0 }
57
57
  => [:select!, :keep_if]
58
58
  ```
59
+
60
+ ## Note to Self
61
+
62
+ Snippet to use in `bin/console` for finding methods for blacklisting:
63
+
64
+ ```
65
+ Suggest::SUGGEST_MODS.flat_map { |k| [k].product(k.instance_methods) }.select { |k, v| v == :rand }.map { |k, v| k.instance_method(v).owner }.uniq
66
+ ```
data/lib/suggest.rb CHANGED
@@ -4,12 +4,26 @@ require "set"
4
4
  module Suggest
5
5
  SUGGEST_MODS = Set.new([
6
6
  Array,
7
+ BasicObject,
8
+ Comparable,
9
+ Complex,
7
10
  Enumerable,
8
- String,
11
+ FalseClass,
12
+ Float,
9
13
  Hash,
10
- Regexp,
11
14
  Integer,
12
- Set
15
+ Math,
16
+ NilClass,
17
+ Numeric,
18
+ Range,
19
+ Regexp,
20
+ Regexp,
21
+ Set,
22
+ String,
23
+ Struct,
24
+ Symbol,
25
+ Time,
26
+ TrueClass,
13
27
  ])
14
28
 
15
29
  UNSAFE_WITH_BLOCK = Set.new([
@@ -23,12 +37,24 @@ module Suggest
23
37
  [Array, :shuffle!]
24
38
  ])
25
39
 
40
+ TOO_COMPLICATED = Set.new([
41
+ [String, :freeze],
42
+ [Set, :freeze],
43
+ [Set, :taint],
44
+ [Set, :untaint],
45
+ [Numeric, :singleton_method_added],
46
+ [Numeric, :clone],
47
+ [Numeric, :dup],
48
+ ])
49
+
26
50
  module Mixin
27
51
  def what_returns?(expected, args: [], allow_mutation: false)
28
52
  block = Proc.new if block_given?
29
53
 
30
54
  applicable_methods = self.methods.map(&method(:method)).select do |m|
31
- SUGGEST_MODS.include?(m.owner) && !INCONSISTENT.include?([m.owner, m.name])
55
+ SUGGEST_MODS.include?(m.owner) &&
56
+ !INCONSISTENT.include?([m.owner, m.name]) &&
57
+ !TOO_COMPLICATED.include?([m.owner, m.name])
32
58
  end
33
59
 
34
60
  applicable_methods.select do |m|
@@ -54,7 +80,9 @@ module Suggest
54
80
  block = Proc.new if block_given?
55
81
 
56
82
  applicable_methods = self.methods.map(&method(:method)).select do |m|
57
- SUGGEST_MODS.include?(m.owner) && !INCONSISTENT.include?([m.owner, m.name])
83
+ SUGGEST_MODS.include?(m.owner) &&
84
+ !INCONSISTENT.include?([m.owner, m.name]) &&
85
+ !TOO_COMPLICATED.include?([m.owner, m.name])
58
86
  end
59
87
 
60
88
  applicable_methods.select do |m|
@@ -81,6 +109,19 @@ module Suggest
81
109
  def self.eq?(result, expected)
82
110
  result.is_a?(expected.class) && result == expected
83
111
  end
112
+
113
+ def self.suggestable_methods
114
+ candidates = []
115
+ SUGGEST_MODS.each do |mod|
116
+ owned_methods = mod.instance_methods.select { |m| mod.instance_method(m).owner == mod }
117
+ next if owned_methods.none?
118
+ candidates += [mod].product(owned_methods)
119
+ end
120
+
121
+ candidates.reject do |m|
122
+ INCONSISTENT.include?(m) || TOO_COMPLICATED.include?(m)
123
+ end
124
+ end
84
125
  end
85
126
 
86
127
  Object.include(Suggest::Mixin)
@@ -1,3 +1,3 @@
1
1
  module Suggest
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: suggest_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Bodah