Module: CoreDocs::MethodInfo
- Defined in:
- lib/core_docs.rb
Constant Summary collapse
- METHOD_INSPECT_PATTERN =
From upstream patch(@kyrylo) https://github.com/pry/pry-doc/commit/f0de941e6f5338e2c933e039f9bdef09408cd1c8
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7.0") %r{\A \#< (?:Unbound)?Method:\s (.+) # Method owner such as "BigDecimal" ([\#\.].+?) # Method signature such as ".finite?" or "#finite?" \(.*\) # Param list (?: \s/.+\.rb:\d+ # Source location )? .* # Sometimes there's gibberish like "<main>:0", we ignore that > \z}x else %r{\A \#< (?:Unbound)?Method:\s (.+) # Method owner such as "BigDecimal" ([\#\.].+?) # Method signature such as ".finite?" or "#finite?" (?: \(.*\) # Param list )? > \z}x end
Class Method Summary collapse
-
.aliases(meth) ⇒ Array
Retrives aliases of a method.
-
.c_files_found?(gem_dir) ⇒ Boolean
True if c files exist?.
-
.cache(meth) ⇒ Object
Cache the file that holds the method or return immediately if file is already cached.
-
.cached?(meth) ⇒ Boolean
Check whether the file containing the method is already cached.
-
.find_gem_dir(meth) ⇒ String
Root directory path of gem that method belongs to, nil if could not be found.
-
.gem_dir_from_method(meth) ⇒ String?
Try to recover the gem directory of a gem based on a method object.
-
.gem_root(dir) ⇒ String
FIXME: this is unnecessarily limited to ext/ and lib/ folders.
-
.guess_gem_name_from_module_name(name, guess) ⇒ String?
Try to guess what the gem name will be based on the name of the module.
-
.info_for(meth) ⇒ YARD::CodeObjects::MethodObject
Retrieve the YARD object that contains the method data.
-
.is_eval_method?(meth) ⇒ Boolean
Determine whether a method is an eval method.
-
.is_singleton?(meth) ⇒ Boolean
Checks whether method is a singleton (i.e class method).
-
.method_host(meth) ⇒ Object
The host of the method (receiver or owner).
-
.parse_and_cache_if_gem_cext(meth) ⇒ Object
Attempts to find the c source files if method belongs to a gem and use YARD to parse and cache the source files for display.
-
.receiver_notation_for(meth) ⇒ String
Convert a method object into the
Class#method
string notation. - .registry_lookup(meth) ⇒ Object
Class Method Details
.aliases(meth) ⇒ Array
Retrives aliases of a method
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/core_docs.rb', line 83 def self.aliases(meth) # host = is_singleton?(meth) ? meth.receiver : meth.owner # method_type = is_singleton?(meth) ? :method : :instance_method # methods = Pry::Method.send(:all_from_common, host, method_type, false). # map { |m| m.instance_variable_get(:@method) } # methods.select { |m| host.send(method_type,m.name) == host.send(method_type,meth.name) }. # reject { |m| m.name == meth.name }. # map { |m| host.send(method_type,m.name) } [] end |
.c_files_found?(gem_dir) ⇒ Boolean
Returns true if c files exist?.
158 159 160 |
# File 'lib/core_docs.rb', line 158 def self.c_files_found?(gem_dir) Dir.glob("#{gem_dir}/**/*.c").count > 0 end |
.cache(meth) ⇒ Object
Cache the file that holds the method or return immediately if file is already cached. Return if the method cannot be cached - i.e is a C stdlib method.
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/core_docs.rb', line 246 def self.cache(meth) file, _ = meth.source_location return if is_eval_method?(meth) return if cached?(meth) if !file parse_and_cache_if_gem_cext(meth) return end log.enter_level(Logger::FATAL) do YARD.parse(file) end end |
.cached?(meth) ⇒ Boolean
Check whether the file containing the method is already cached.
106 107 108 |
# File 'lib/core_docs.rb', line 106 def self.cached?(meth) !!registry_lookup(meth) end |
.find_gem_dir(meth) ⇒ String
Returns root directory path of gem that method belongs to, nil if could not be found.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/core_docs.rb', line 178 def self.find_gem_dir(meth) # host = method_host(meth) # begin # host_source_location, _ = WrappedModule.new(host).source_location # break if host_source_location != nil # return unless host.name # host = eval(host.namespace_name) # end while host # # we want to exclude all source_locations that aren't gems (i.e # # stdlib) # if host_source_location && host_source_location =~ %r{/gems/} # gem_root(host_source_location) # else # the WrappedModule approach failed, so try our backup approach gem_dir_from_method(meth) # end end |
.gem_dir_from_method(meth) ⇒ String?
Try to recover the gem directory of a gem based on a method object.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/core_docs.rb', line 224 def self.gem_dir_from_method(meth) guess = 0 host = method_host(meth) return unless host.name root_module_name = host.name.split("::").first while gem_name = guess_gem_name_from_module_name(root_module_name, guess) matches = $LOAD_PATH.grep %r{/gems/#{gem_name}} if !gem_name.empty? if matches && matches.any? return gem_root(matches.first) else guess += 1 end end nil end |
.gem_root(dir) ⇒ String
FIXME: this is unnecessarily limited to ext/ and lib/ folders
169 170 171 172 173 |
# File 'lib/core_docs.rb', line 169 def self.gem_root(dir) if index = dir.rindex(/\/(?:lib|ext)(?:\/|$)/) dir[0..index-1] end end |
.guess_gem_name_from_module_name(name, guess) ⇒ String?
Try to guess what the gem name will be based on the name of the module.
We try a few approaches here depending on the guess
parameter.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/core_docs.rb', line 204 def self.guess_gem_name_from_module_name(name, guess) case guess when 0 name.downcase when 1 name.scan(/[A-Z][a-z]+/).map(&:downcase).join('_') when 2 name.scan(/[A-Z][a-z]+/).map(&:downcase).join('_').sub("_", "-") when 3 name.scan(/[A-Z][a-z]+/).map(&:downcase).join('-') when 4 name else nil end end |
.info_for(meth) ⇒ YARD::CodeObjects::MethodObject
Retrieve the YARD object that contains the method data.
127 128 129 130 |
# File 'lib/core_docs.rb', line 127 def self.info_for(meth) cache(meth) registry_lookup(meth) end |
.is_eval_method?(meth) ⇒ Boolean
Determine whether a method is an eval method.
134 135 136 137 138 139 140 141 |
# File 'lib/core_docs.rb', line 134 def self.is_eval_method?(meth) file, _ = meth.source_location if file =~ /(\(.*\))|<.*>/ true else false end end |
.is_singleton?(meth) ⇒ Boolean
Checks whether method is a singleton (i.e class method)
99 100 101 |
# File 'lib/core_docs.rb', line 99 def self.is_singleton?(meth) receiver_notation_for(meth).include?('.') end |
.method_host(meth) ⇒ Object
Returns The host of the method (receiver or owner).
163 164 165 |
# File 'lib/core_docs.rb', line 163 def self.method_host(meth) is_singleton?(meth) && Module === meth.receiver ? meth.receiver : meth.owner end |
.parse_and_cache_if_gem_cext(meth) ⇒ Object
Attempts to find the c source files if method belongs to a gem and use YARD to parse and cache the source files for display
147 148 149 150 151 152 153 154 |
# File 'lib/core_docs.rb', line 147 def self.parse_and_cache_if_gem_cext(meth) if gem_dir = find_gem_dir(meth) if c_files_found?(gem_dir) warn "Scanning and caching *.c files..." YARD.parse("#{gem_dir}/**/*.c") end end end |
.receiver_notation_for(meth) ⇒ String
This mess is needed in order to support all the modern Rubies. YOU must figure out a better way to distinguish between class methods and instance methods.
Convert a method object into the Class#method
string notation.
71 72 73 74 75 76 77 |
# File 'lib/core_docs.rb', line 71 def self.receiver_notation_for(meth) match = meth.inspect.match(METHOD_INSPECT_PATTERN) owner = meth.owner.to_s.sub(/#<.+?:(.+?)>/, '\1') name = match[2] name.sub!('#', '.') if match[1] =~ /\A#<Class:/ owner + name end |
.registry_lookup(meth) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/core_docs.rb', line 110 def self.registry_lookup(meth) obj = YARD::Registry.at(receiver_notation_for(meth)) if obj.nil? if !(aliases = aliases(meth)).empty? obj = YARD::Registry.at(receiver_notation_for(aliases.first)) elsif meth.owner == Kernel # YARD thinks that some methods are on Object when # they're actually on Kernel; so try again on Object if Kernel fails. obj = YARD::Registry.at("Object##{meth.name}") end end obj end |