Class: Aquarium::Finders::MethodFinder

Inherits:
Object
  • Object
show all
Includes:
Utils::ArrayUtils, Utils::OptionsUtils
Defined in:
lib/aquarium/finders/method_finder.rb

Overview

MethodFinder

Locate methods in specified types or objects.

Constant Summary collapse

NIL_OBJECT =
MethodFinder.new
METHOD_FINDER_CANONICAL_OPTIONS =
{
  "objects" => %w[object for_object for_objects on_object on_objects in_object in_objects within_object within_objects],
  "methods" => %w[method within_method within_methods calling invoking invocations_of calls_to sending_message_to sending_messages_to],
  "method_options" => %w[method_option restricting_methods_to] 
}
CANONICAL_OPTIONS =
METHOD_FINDER_CANONICAL_OPTIONS.merge(Aquarium::Finders::TypeFinder::TYPE_FINDER_CANONICAL_OPTIONS)
RECOGNIZED_METHOD_OPTIONS =
{
  "all"       => %w[all_methods],
  "public"    => %w[public_methods],
  "private"   => %w[private_methods],
  "protected" => %w[protected_methods],
  "instance"  => %w[instance_methods],
  "class"     => %w[class_methods],
  "singleton" => %w[singleton_methods],
  "exclude_ancestor_methods" => %w[exclude_ancestors exclude_ancestors_methods suppress_ancestors suppress_ancestor_methods suppress_ancestors_methods],
  "include_system_methods" => %w[include_all_system_methods]
}
IGNORED_SYSTEM_METHODS =

Methods we ignore by default, because users rarely want to advice them and so it makes finding what you want easier.

[/^__/]

Instance Attribute Summary

Attributes included from Utils::OptionsUtils

#specification

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::OptionsUtils

append_features, #hashify, #init_specification, universal_options, universal_prepositions, #validate_options

Methods included from Utils::ArrayUtils

#make_array, make_array, #strip_array_nils, strip_array_nils

Methods included from Utils::SetUtils

#make_set, #strip_set_nils, strip_set_nils

Class Method Details

.all_recognized_method_option_symbolsObject



165
166
167
168
169
170
171
# File 'lib/aquarium/finders/method_finder.rb', line 165

def self.all_recognized_method_option_symbols
  all = RECOGNIZED_METHOD_OPTIONS.keys.map {|key| key.intern}
  RECOGNIZED_METHOD_OPTIONS.keys.inject(all) do |all, key|
    all += RECOGNIZED_METHOD_OPTIONS[key].map {|value| value.intern}
    all
  end
end

.init_method_options(scope_options_set) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/aquarium/finders/method_finder.rb', line 149

def self.init_method_options scope_options_set
  return Set.new([]) if scope_options_set.nil?
  options = []
  scope_options_set.each do |opt|
    if RECOGNIZED_METHOD_OPTIONS.keys.include?(opt.to_s)
      options << opt
    else
      RECOGNIZED_METHOD_OPTIONS.keys.each do |key|
        options << key.intern if RECOGNIZED_METHOD_OPTIONS[key].include?(opt.to_s)
      end
    end
  end
  options << :instance unless (options.include?(:class) or options.include?(:singleton))
  Set.new(options.sort{|x,y| x.to_s <=> y.to_s}.uniq)
end

.is_recognized_method_option(string_or_symbol) ⇒ Object



173
174
175
176
# File 'lib/aquarium/finders/method_finder.rb', line 173

def self.is_recognized_method_option string_or_symbol
  sym = string_or_symbol.respond_to?(:intern) ? string_or_symbol.intern : string_or_symbol
  all_recognized_method_option_symbols.include? sym
end

Instance Method Details

#find(options = {}) ⇒ Object

Returns a Aquarium::Finders::FinderResult, where the “matched” keys are the input types, type names, and/or regular expressions, and objects for which matches were found and the corresponding values are the method name symbols that were found. Method names, not method objects, are always returned, because we can only get method objects for instance methods if we have an instance! The keys in the “not_matched” part of the FinderResult are the specified types and objects for which no matches were found.

The options are as follows:

Types

All the options supported by TypeFinder#find.

Objects

One or more objects to match. Specify one or an array of values. Note: String or symbol objects will be treated as type names!

  • :objects => objects

  • :object => objects

  • :for_objects => objects

  • :for_object => objects

  • :on_objects => objects

  • :on_object => objects

  • :in_objects => objects

  • :in_object => objects

  • :within_objects => objects

  • :within_object => objects

Methods

One or more method names and/or regular expressions to match. Specify one or an array of values. If :all or :all_methods is specified, all methods in the types or objects will be matched, subject to the search options described below. That is, :all is equivalent to the regular expression /.+/.

  • :methods => method_names_and_regexps

  • :method => method_names_and_regexps

  • :within_methods => method_names_and_regexps

  • :within_method => method_names_and_regexps

  • :calling => method_names_and_regexps

  • :invoking => method_names_and_regexps

  • :calls_to => method_names_and_regexps

  • :sending_message_to => method_names_and_regexps

  • :sending_messages_to => method_names_and_regexps

Methods Options

By default, the searches are restricted to public instance methods.

  • :method_options => options

  • :method_option => options

  • :restricting_methods_to => options

Note, the older, deprecated :options synonym has been removed.

Here are the allowed “options”. Specify one or more of them. Any combination is allowed.

:public or :public_methods

Search for public methods (default).

:private or :private_methods

Search for private methods.

:protected or :protected_methods

Search for protected methods.

:instance or :instance_methods

Search for instance methods.

:class or :class_methods

Search for class methods.

:singleton or :singleton_methods

Search for singleton methods.

:include_system_methods

Also search for “system” methods like __id__ that are normally ignored (See IGNORED_SYSTEM_MEHTODS).

Note: specifying :class when objects are specified won’t work. Also, :class, :public, :protected, and :private are ignored when looking for singleton methods.

Excluded Types, Objects, or Methods

Exclude one or more types, objects, or methods from the match. Specify one or an array of values.

  • :exclude_methods => method_names_and_regexps

  • :exclude_method => method_names_and_regexps

  • :exclude_ancestor_methods - Suppress “ancestor” methods.

The exclude_ prefix can be used with any of the synonyms for the other options. WARNING: the :exclude_ancestor_methods option means that if you search for a override method foo in a derived class and foo is defined in the base class, you won’t find it!



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/aquarium/finders/method_finder.rb', line 93

def find options = {}
  init_specification options, CANONICAL_OPTIONS do
    finish_specification_initialization 
  end
  return Aquarium::Finders::FinderResult.new if nothing_to_find?
  types_and_objects = input_types + input_objects
  method_names_or_regexps = input_methods
  if method_names_or_regexps.empty?
    not_matched = {}
    types_and_objects.each {|t| not_matched[t] = []}
    return Aquarium::Finders::FinderResult.new(:not_matched => not_matched)
  end
  result = do_find_all_by types_and_objects, method_names_or_regexps
  unless (input_exclude_methods.nil? or input_exclude_methods.empty?)
    result -= do_find_all_by types_and_objects, input_exclude_methods
  end
  result
end