Generate documentation for multiple classes with the same name? - phpdoc

I have a webapp that has multiple "Controller" files that all define a class called "Controller". There's a routing script that only includes the proper file depending on the page request, and then instantiates a new "Controller" object.
In generating the phpDoc automatic documentation, only the first "Controller" class gets documentation generated for it, and all other files that define a "Controller" object link to that one definition of "Controller" rather than their own. Is there a way to force phpDoc to create unique documentation for each of the redefinitions of the "Controller" class?

phpDocumentor can handle this kind of class name duplication, but only by putting the classes in different "packages".
/**
* MyClass #1
* #package PackageOne
*/
class MyClass {}
/**
* MyClass #2
* #package PackageTwo
*/
class MyClass {}
This will result in both classes being documented successfully in the same phpDocumentor execution.

Related

How to override method in installed package in django rest framework?

I have installed django_cognito_jwt using pip. The package has two modules backend.py and validator.py and one init.py file. I want to overridea method of a class SampleClass in validator.py. I created a file in my django rest app called authentication.py. Then I called the class
from django_cognito_jwt import SampleClass
CustomSampleClass(SampleClass):
def methodA(self):
do something
return value
I used the exact same name of the parent method with the same arguments. But the call flow does not reach the extended class. Why??
Inherit from the desired django_cognito_jwt class within your class.
In your class, override a method with the same name as the method you want to override.
And use the class you made wherever you want

How to list instance methods of an object class without listing the methods of the superclasses

The method methods returns a list of all methods of a class, for example if I call 'miguel'.methods, I will get a list of all the methods in the class String.
I would like to list the methods of a class, excluding the methods of its superclasses. For example, a list of instance methods that are defined on String, including modules included into String.
I also would like to know how to list instance methods that are exclusively defined on the object class without listing instance methods included in modules included by the class.
Try this:
class.instance_methods(false)
Here for string as example:
'miguel'.class.instance_methods(false)

Ruby inheritance and instances

I made a class User (which is an instance of Class), and made it inherit from class Person. What makes this considered not to be multiple inheritance? Doesn't it inherit instance methods like new from Class and any methods that Person offers?
In other words, how is this not multiple inheritance:
class User < class Person
some methods here...
end
when User already inherits all of Class's methods? Isn't the following what is going on under the hood?
class User < class Person and class Class
end
If you open irb you can check it yourself. Type User.superclass, you will see that User has only one superclass, which is Person. User will inherit the methods from Object because Person's superclass is Object
It is not "multiple inheritance" in usual way, but a simple chain of inheritance.
Ancestors cannot have own constructors unless you call them explicitly from initialize, and in complex hierarchies this can be very painful.
The main point is that there's only one instance object, that is shared and methods are mixed into it.
In ruby class hierarchy is always a tree. Even mixins, that may look like a multiple inheritance - internally are implemented as inheritance chain.
True multiple inheritance can be hard, for example, in C++:
class A {}
class B: public A{}
class C: public A{}
class D: public B, public C{}
How many instances of A should be inside D? (the "diamond problem")
Ruby avoids it by simply not having the cause.

What's the difference between a module function, an instance method and a class method in ruby modules?

I'd like to know the specifics about how methods defined in a module are scoped when they're defined as module_functions, class methods (i.e. 'def self.foo') and instance methods (i.e. 'def foo')
How does the behavior change when including the module in to different classes if at all?
I've been digging around on the internet and haven't been able to find a good explanation.
A class (or more properly, module) method is defined on the module, and is called with the module as a receiver. It won't be mixed in when you include YourModule.
The instance methods of a module are mixed in as instance methods of the caller when you do include YourModule.
The module_function method takes an instance method you've defined in the module, makes it private (and it will remain private when mixed in), and also turns it into a public module method.

When should I use "class Object", "class Module", "module Kernel" and nothing?

I'm new to ruby metaprogramming, and I see people metaprogramming code in different places, like class Object, class Module, module Kernel and "nothing" (ie, out of a class/module definition block).
E.g.: I'm creating a c_attr_accessor method to access class variables, and I'm not sure where I must put the code, since it works in any of those cases.
How to decide what place is more appropriate to put a new global code?
Each of these examples fall into different cases.
If you are writing methods that apply to all objects, then you open the Object class so all objects can access it. If you are writing methods that apply to all modules, then you open Module. Whenever you open a class to add methods, the methods should apply to all instances of the class and nothing else.
Extending the Kernel module is different: people do this to add methods that should be available to every scope, but not really as methods to be explicitly called on an object, by making them private.
When you are outside of any class or module statement, you are in the scope of the main object, and methods you define default to being private methods of Object. This is fine for small or simple programs, but you will eventually want to use proper modules as namespaces to organize your methods.
As a final note on the subject, you always need to be sure that you really want methods you add to built-in classes and modules to be available to everything in your application, including external inclusions because they all share the built-ins.
Now to apply this to answer your question. Because you are defining a method that creates accessors for class variables, you should put it in the class Class as it applies to all classes and nothing else. Finally, you will likely only use it in class definitions (within a class statement), so we should make it private:
class Class
private
def c_attr_accessor(name)
# ...
end
end
class User
c_attr_accessor :class_variable_name
# ...
end
If you don't really need it in every class (maybe just a few), then create a "mixin module" to extend every class that needs this feature:
module ClassVariableAccessor
private
def c_attr_accessor(name)
# ...
end
end
class User
extend ClassVariableAccessor
c_attr_accessor :class_variable_name
# ...
end
Note that you are using Object#extend to add c_attr_accessor only to the object User (remember that classes are objects; you will hear that a lot if you are new to Ruby metaprogramming).
There is another way to implement the last example, which works by explicitly extending its base class through the Module#included(base_class) "hook method" called whenever the module included, and the base class is passed to base_class:
module ClassVariableAccessor
def included(base_class)
base_class.extend ClassMethods
end
module ClassMethods
def c_attr_accessor(name)
# ...
end
end
end
class User
include ClassVariableAccessor
c_attr_accessor :class_variable_name
# ...
end
I recommend this last solution because it is the most general and uses a simple interface that should not need to be updated. I hope this is not too much!
Have you tried looking up where the normal attribute accessors are defined? I'd either define it in the same class/module, or create my own module in which all my new methods go.

Resources