Class: YARD::CodeObjects::Proxy
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/yard/code_objects/proxy.rb |
Overview
The Proxy class is a way to lazily resolve code objects in
cases where the object may not yet exist. A proxy simply stores
an unresolved path until a method is called on the object, at which
point it does a lookup using Registry.resolve. If the object is
not found, a warning is raised and ProxyMethodError
might be raised.
Class Method Summary
- .===(other)
-
.new(namespace, name, type = nil) ⇒ Proxy
constructor
Creates a new
Proxy
.
Instance Attribute Summary
- #namespace (also: #parent) readonly
-
#parent
readonly
Alias for #namespace.
-
#root? ⇒ Boolean
readonly
This class is never a root object.
-
#type ⇒ Symbol
rw
Returns the type of the proxy.
-
#type=(type) ⇒ void
rw
Allows a parser to infer the type of the proxy by its path.
Instance Method Summary
- #<=>(other) ⇒ Boolean
-
#==(other)
Alias for #equal?.
- #===(other) ⇒ Boolean
-
#class ⇒ Class
Returns the class name of the object the proxy is mimicking, if resolved.
- #equal?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Integer
-
#inspect ⇒ String
Returns a text representation of the
Proxy
. - #instance_of?(klass) ⇒ Boolean
- #is_a?(klass) ⇒ Boolean
- #kind_of?(klass) ⇒ Boolean
-
#method_missing(meth, *args, &block)
Dispatches the method to the resolved object.
-
#name(prefix = false) ⇒ Symbol, String
The name of the object.
-
#path ⇒ String
(also: #to_s, #to_str, #title)
If the proxy resolves to an object, returns its path, otherwise guesses at the correct path using the original namespace and name.
- #respond_to?(meth, include_private = false) ⇒ Boolean
-
#title
Alias for #path.
-
#to_s
Alias for #path.
-
#to_str
Alias for #path.
- #proxy_path private
- #to_ary private
-
#to_obj ⇒ Base?
private
Attempts to find the object that this unresolved object references by checking if any objects by this name are registered all the way up the namespace tree.
Constructor Details
.new(namespace, name, type = nil) ⇒ Proxy
Creates a new Proxy
# File 'lib/yard/code_objects/proxy.rb', line 34
def initialize(namespace, name, type = nil) namespace = Registry.root if !namespace || namespace == :root if name =~ /^#{NSEPQ}/ namespace = Registry.root name = name[2..-1] end if name =~ PROXY_MATCH @orignamespace = namespace @origname = name @imethod = true if name.include? ISEP namespace = Proxy.new(namespace, $`) unless $`.empty? name = $1 else @orignamespace = nil @origname = nil @imethod = nil end @name = name.to_sym @namespace = namespace @obj = nil @imethod ||= nil self.type = type if @namespace.is_a?(ConstantObject) unless @namespace.value =~ /\A#{NAMESPACEMATCH}\Z/ raise Parser::UndocumentableError, "constant mapping for " + "#{@origname} (type=#{type.inspect})" end @origname = nil # forget these for a constant @orignamespace = nil @namespace = Proxy.new(@namespace.namespace, @namespace.value) end unless @namespace.is_a?(NamespaceObject) || @namespace.is_a?(Proxy) raise ArgumentError, "Invalid namespace object: #{namespace}" end # If the name begins with "::" (like "::String") # this is definitely a root level object, so # remove the namespace and attach it to the root if @name =~ /^#{NSEPQ}/ @name.gsub!(/^#{NSEPQ}/, '') @namespace = Registry.root end end
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block)
Dispatches the method to the resolved object.
# File 'lib/yard/code_objects/proxy.rb', line 178
def method_missing(meth, *args, &block) if to_obj to_obj.__send__(meth, *args, &block) else log.warn "Load Order / Name Resolution Problem on #{path}:\n" \ "-\n" \ "Something is trying to call #{meth} on object #{path} before it has been recognized.\n" \ "This error usually means that you need to modify the order in which you parse files\n" \ "so that #{path} is parsed before methods or other objects attempt to access it.\n" \ "-\n" \ "YARD will recover from this error and continue to parse but you *may* have problems\n" \ "with your generated documentation. You should probably fix this.\n" \ "-\n" begin super rescue NoMethodError raise ProxyMethodError, "Proxy cannot call method ##{meth} on object '#{path}'" end end end
Class Method Details
.===(other)
[ GitHub ]# File 'lib/yard/code_objects/proxy.rb', line 25
def self.===(other) other.is_a?(self) end
Instance Attribute Details
#namespace (readonly) Also known as: #parent
[ GitHub ]# File 'lib/yard/code_objects/proxy.rb', line 27
attr_reader :namespace
#parent (readonly)
Alias for #namespace.
# File 'lib/yard/code_objects/proxy.rb', line 28
alias parent namespace
#root? ⇒ Boolean
(readonly)
This class is never a root object
# File 'lib/yard/code_objects/proxy.rb', line 200
def root?; false end
#type ⇒ Symbol
(rw)
Returns the type of the proxy. If it cannot be resolved at the
time of the call, it will either return the inferred proxy type
(see #type=) or :proxy
#type=(type) ⇒ void
(rw)
This method returns an undefined value.
Allows a parser to infer the type of the proxy by its path.
Instance Method Details
#<=>(other) ⇒ Boolean
# File 'lib/yard/code_objects/proxy.rb', line 118
def <=>(other) if other.respond_to? :path path <=> other.path else false end end
#==(other)
Alias for #equal?.
# File 'lib/yard/code_objects/proxy.rb', line 134
alias == equal?
#===(other) ⇒ Boolean
#class ⇒ Class
Returns the class name of the object the proxy is mimicking, if
resolved. Otherwise returns Proxy
.
#equal?(other) ⇒ Boolean
Also known as: #==
# File 'lib/yard/code_objects/proxy.rb', line 127
def equal?(other) if other.respond_to? :path path == other.path else false end end
#hash ⇒ Integer
# File 'lib/yard/code_objects/proxy.rb', line 137
def hash; path.hash end
#inspect ⇒ String
Returns a text representation of the Proxy
#instance_of?(klass) ⇒ Boolean
# File 'lib/yard/code_objects/proxy.rb', line 161
def instance_of?(klass) self.class == klass end
#is_a?(klass) ⇒ Boolean
#kind_of?(klass) ⇒ Boolean
# File 'lib/yard/code_objects/proxy.rb', line 166
def kind_of?(klass) self.class <= klass end
#name(prefix = false) ⇒ Symbol
, String
The name of the object
# File 'lib/yard/code_objects/proxy.rb', line 85
def name(prefix = false) prefix ? "#{@imethod && ISEP}#{@name}" : @name end
#path ⇒ String Also known as: #to_s, #to_str, #title
If the proxy resolves to an object, returns its path, otherwise guesses at the correct path using the original namespace and name.
# File 'lib/yard/code_objects/proxy.rb', line 100
def path to_obj ? to_obj.path : proxy_path end
#proxy_path (private)
[ GitHub ]# File 'lib/yard/code_objects/proxy.rb', line 228
def proxy_path if @namespace.root? (@imethod ? ISEP : "") + name.to_s elsif @origname if @origname =~ CONSTANTSTART @origname else [namespace.path, @origname].join end elsif name.to_s =~ CONSTANTSTART name.to_s else # class meth? [namespace.path, name.to_s].join(CSEP) end end
#respond_to?(meth, include_private = false) ⇒ Boolean
#title
Alias for #path.
# File 'lib/yard/code_objects/proxy.rb', line 105
alias title path
#to_ary (private)
this method fixes a bug in 1.9.2: http://gist.github.com/437136
# File 'lib/yard/code_objects/proxy.rb', line 205
def to_ary; nil end
#to_obj ⇒ Base? (private)
Attempts to find the object that this unresolved object references by checking if any objects by this name are registered all the way up the namespace tree.
# File 'lib/yard/code_objects/proxy.rb', line 212
def to_obj return @obj if @obj @obj = Registry.resolve(@namespace, (@imethod ? ISEP : '') + @name.to_s, false, false, @type) if @obj if @origname && @origname.include?("::") && !@obj.path.include?(@origname) # the object's path should include the original proxy namespace, # otherwise it's (probably) not the right object. @obj = nil else @namespace = @obj.namespace @name = @obj.name end end @obj end
#to_s
Alias for #path.
# File 'lib/yard/code_objects/proxy.rb', line 103
alias to_s path
#to_str
Alias for #path.
# File 'lib/yard/code_objects/proxy.rb', line 104
alias to_str path