Class: YARD::CodeObjects::Proxy

Inherits:
Object
  • Object
show all
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.

Examples:

Creates a Proxy to the String class from a module

  # When the String class is parsed this method will
  # begin to act like the String ClassObject.
  Proxy.new(mymoduleobj, "String")

See Also:

Instance Attribute Summary

Class Method Summary

Instance Method Summary

Constructor Details

- (Proxy) initialize(namespace, name)

Creates a new Proxy

Raises:

  • (ArgumentError) — if namespace is not a NamespaceObject


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/yard/code_objects/proxy.rb', line 28

def initialize(namespace, name)
  namespace = Registry.root if !namespace || namespace == :root
  
  if name =~ /^#{NSEPQ}/
    namespace = Registry.root
    name = name[2..-1]
  end
  
  if name =~ /(?:#{NSEPQ}|#{ISEPQ}|#{CSEPQ})([^#{NSEPQ}#{ISEPQ}#{CSEPQ}]+)$/
    @orignamespace, @origname = namespace, name
    @imethod = true if name.include? ISEP
    namespace = Proxy.new(namespace, $`) unless $`.empty?
    name = $1
  end 
  
  @name = name.to_sym
  @namespace = namespace
  
  unless @namespace.is_a?(NamespaceObject) or @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

- (Object) method_missing(meth, *args, &block)

Dispatches the method to the resolved object.

Raises:



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/yard/code_objects/proxy.rb', line 182

def method_missing(meth, *args, &block)
  if obj = to_obj
    obj.__send__(meth, *args, &block)
  else
    log.warn "Load Order / Name Resolution Problem on #{path}:"
    log.warn "-"
    log.warn "Something is trying to access the object #{path} before it has been recognized."
    log.warn "This error usually means that you need to modify the order in which you parse files"
    log.warn "so that #{path} is parsed before methods or other objects attempt to access it."
    log.warn "-"
    log.warn "YARD will recover from this error and continue to parse but you *may* have problems"
    log.warn "with your generated documentation. You should probably fix this."
    log.warn "-"
    begin 
      super
    rescue NoMethodError
      raise ProxyMethodError, "Proxy cannot call method ##{meth} on object '#{path}'"
    end
  end
end

Instance Attribute Details

- (Object) name (readonly)

Returns the value of attribute name



21
22
23
# File 'lib/yard/code_objects/proxy.rb', line 21

def name
  @name
end

- (Object) namespace (readonly) Also known as: parent

Returns the value of attribute namespace



21
22
23
# File 'lib/yard/code_objects/proxy.rb', line 21

def namespace
  @namespace
end

Class Method Details

+ (Object) ===(other)



19
# File 'lib/yard/code_objects/proxy.rb', line 19

def self.===(other) other.is_a?(self) end

Instance Method Details

- (Boolean) <=>(other)

Returns:

  • (Boolean)


114
115
116
117
118
119
120
# File 'lib/yard/code_objects/proxy.rb', line 114

def <=>(other)
  if other.respond_to? :path
    path <=> other.path
  else
    false
  end
end

- (Boolean) ==(other)

Returns:

  • (Boolean)


123
124
125
126
127
128
129
# File 'lib/yard/code_objects/proxy.rb', line 123

def ==(other)
  if other.respond_to? :path
    path == other.path
  else
    false
  end
end

- (Boolean) ===(other)

Returns:

  • (Boolean)


105
106
107
108
109
110
111
# File 'lib/yard/code_objects/proxy.rb', line 105

def ===(other)
  if obj = to_obj
    obj === other
  else
    self.class <= other.class
  end
end

- (Class) class

Returns the class name of the object the proxy is mimicking, if resolved. Otherwise returns Proxy.

Returns:

  • (Class) — the resolved object’s class or Proxy


134
135
136
137
138
139
140
# File 'lib/yard/code_objects/proxy.rb', line 134

def class
  if obj = to_obj
    obj.class
  else
    Proxy
  end
end

- (String) inspect

Returns a text representation of the Proxy

Returns:

  • (String) — the object’s #inspect method or P(OBJECTPATH)


61
62
63
64
65
66
67
# File 'lib/yard/code_objects/proxy.rb', line 61

def inspect
  if obj = to_obj
    obj.inspect
  else
    "P(#{path})"
  end
end

- (Boolean) instance_of?(klass)

Returns:

  • (Boolean)


161
162
163
# File 'lib/yard/code_objects/proxy.rb', line 161

def instance_of?(klass)
  self.class == klass
end

- (Boolean) is_a?(klass)

Returns:

  • (Boolean)


96
97
98
99
100
101
102
# File 'lib/yard/code_objects/proxy.rb', line 96

def is_a?(klass)
  if obj = to_obj
    obj.is_a?(klass)
  else
    self.class <= klass
  end
end

- (Boolean) kind_of?(klass)

Returns:

  • (Boolean)


166
167
168
# File 'lib/yard/code_objects/proxy.rb', line 166

def kind_of?(klass)
  self.class <= klass
end

- (Object) parent



22
23
24
# File 'lib/yard/code_objects/proxy.rb', line 22

def namespace
  @namespace
end

- (String) path Also known as: to_s

If the proxy resolves to an object, returns its path, otherwise guesses at the correct path using the original namespace and name.

Returns:

  • (String) — the assumed path of the proxy (or the real path of the resolved object)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/yard/code_objects/proxy.rb', line 74

def path
  if obj = to_obj
    obj.path
  else
    if @namespace.root?
      (@imethod ? ISEP : "") + name.to_s
    elsif @origname
      if @origname =~ /^[A-Z]/
        @origname
      else
        [namespace.path, @origname].join
      end
    elsif name.to_s =~ /^[A-Z]/ # const
      name.to_s
    else # class meth?
      [namespace.path, name.to_s].join(CSEP)
    end
  end
end

- (Boolean) respond_to?(meth, include_private = false)

Returns:

  • (Boolean)


171
172
173
174
175
176
177
# File 'lib/yard/code_objects/proxy.rb', line 171

def respond_to?(meth, include_private = false)
  if obj = to_obj
    obj.respond_to?(meth, include_private)
  else
    super
  end
end

- (Boolean) root?

This class is never a root object

Returns:

  • (Boolean)


204
# File 'lib/yard/code_objects/proxy.rb', line 204

def root?; false end

- (Object) to_s



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/yard/code_objects/proxy.rb', line 93

def path
  if obj = to_obj
    obj.path
  else
    if @namespace.root?
      (@imethod ? ISEP : "") + name.to_s
    elsif @origname
      if @origname =~ /^[A-Z]/
        @origname
      else
        [namespace.path, @origname].join
      end
    elsif name.to_s =~ /^[A-Z]/ # const
      name.to_s
    else # class meth?
      [namespace.path, name.to_s].join(CSEP)
    end
  end
end

- (Symbol) type

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

Returns:

  • (Symbol) — the Proxy’s type

See Also:



147
148
149
150
151
152
153
# File 'lib/yard/code_objects/proxy.rb', line 147

def type
  if obj = to_obj
    obj.type
  else
    Registry.proxy_types[path] || :proxy
  end
end

- (void) type=(type)

This method returns an undefined value.

Allows a parser to infer the type of the proxy by its path.

Parameters:

  • (#to_sym) type — the proxy’s inferred type


158
# File 'lib/yard/code_objects/proxy.rb', line 158

def type=(type) Registry.proxy_types[path] = type.to_sym end