Module: Suggest

Defined in:
lib/suggest.rb,
lib/suggest/version.rb

Defined Under Namespace

Modules: Mixin

Constant Summary collapse

SUGGEST_MODS =
Set.new([
  Array,
  BasicObject,
  Comparable,
  Complex,
  Enumerable,
  FalseClass,
  Float,
  Hash,
  Integer,
  Math,
  NilClass,
  Numeric,
  Range,
  Regexp,
  Regexp,
  Set,
  String,
  Struct,
  Symbol,
  Time,
  TrueClass,
])
UNSAFE_WITH_BLOCK =
Set.new([
  [Array, :cycle],
  [Enumerable, :cycle]
])
INCONSISTENT =
Set.new([
  [Array, :sample],
  [Array, :shuffle],
  [Array, :shuffle!]
])
TOO_COMPLICATED =
Set.new([
  [String, :freeze],
  [Set, :freeze],
  [Set, :taint],
  [Set, :untaint],
  [Numeric, :singleton_method_added],
  [Numeric, :clone],
  [Numeric, :dup],
  [BasicObject, :instance_eval],
  [BasicObject, :instance_exec],
  [BasicObject, :__send__],
  [BasicObject, :singleton_method_added],
  [BasicObject, :singleton_method_removed],
  [BasicObject, :singleton_method_undefined]
])
SELECTOR =
->(m) do
  SUGGEST_MODS.include?(m.owner) &&
    !INCONSISTENT.include?([m.owner, m.name]) &&
    !TOO_COMPLICATED.include?([m.owner, m.name])
end
VERSION =
"0.5.2"

Class Method Summary collapse

Class Method Details

.eq?(result, expected) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/suggest.rb', line 100

def self.eq?(result, expected)
  result.is_a?(expected.class) && result == expected
end

.suggestable!(mod, **corrections) ⇒ Object

unsafe_with_block: [], inconsistent: [], too_complicated: []

Raises:

  • (ArgumentError)


104
105
106
107
108
109
110
111
112
113
# File 'lib/suggest.rb', line 104

def self.suggestable!(mod, **corrections) # unsafe_with_block: [], inconsistent: [], too_complicated: []
  raise ArgumentError.new("Must support smart comparison (implement «#{mod}#==»)") if mod.instance_method(:==).owner == BasicObject

  SUGGEST_MODS << mod
  %w[unsafe_with_block inconsistent too_complicated].each do |correction|
    c = Suggest.const_get(correction.upcase)
    [mod].product(corrections.fetch(correction, [])).each(&c.method(:<<))
  end
  mod.include(Suggest::Mixin) unless mod.ancestors.include?(Suggest::Mixin)
end

.suggestable_methodsObject



115
116
117
118
119
120
121
122
123
# File 'lib/suggest.rb', line 115

def self.suggestable_methods
  SUGGEST_MODS.each_with_object([]) do |mod, candidates|
    owned_methods = mod.instance_methods.select { |m| mod.instance_method(m).owner == mod }
    next if owned_methods.none?
    candidates += [mod].product(owned_methods)
  end.reject do |m|
    INCONSISTENT.include?(m) || TOO_COMPLICATED.include?(m)
  end
end