In: |
DescribeMethods.rb
|
This module provides a describe_method method on the class into which it is included. It provides a way to formally describe the public instance methods of the class, and later exlore those descriptions through the method_description and method_descriptions methods.
Methods can be described before or after they are created. (In truth, no checking is done to ensure that the method name exists in the class.)
require 'DescribeMethods' class MyClass include DescribeMethods def echo_out( msg, repeats, say_done=false ) repeats.times{ puts msg } puts "all done" if say_done end describe_method( :echo_out, "Echo String", "Print a string a number of times", nil, Method::Arg.new( :msg, :string, "The string to output" ), Method::Arg.new( :repeats, :integer, "The number of times to print the string" ), Method::Arg.new( :say_done, :boolean, "Output 'all done' when done?", false, false ) ) end puts MyClass.method_description[ :echo_out ] #=> Method Name: MyClass#echo_out #=> Display Name: Echo String #=> Description: Print a string a number of times #=> Returns: (unknown) #=> Arguments: #=> msg string The string to output #=> repeats integer The number of times to print the string #=> say_done boolean (optional) Output 'all done' when done?
See the Method::Description class for accessor information on the items stored in method_descriptions.
The Method::Arg class uses describe_method to describe its own constructor:
puts Method::Arg.method_description[ :new ] #=> Method Name: Method::Arg#new #=> Display Name: New #=> Description: Creates a new method argument #=> Returns: (unknown) #=> Arguments: #=> name symbol A name for the argument #=> type symbol The type of the argument (or nil if it may vary) #=> description string (optional) An extended description of the argument #=> required boolean (optional) Is this argument required? #=> default_value (varies) (optional) A default value to suggest for the argument #=> min (varies) (optional) A minimum value (matching argument type) #=> max (varies) (optional) A maximum value (matching argument type) #=> values array (optional) A list of legal values (for arguments of type "list")
Returns a hash of all methods described, where the method name (as symbol) is the key to its Method::Description instance. Example usage:
puts MyClass.method_description[ :some_method ]