Class: YARD::CodeObjects::MacroObject
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Base
|
|
Instance Chain:
self,
Base
|
|
Inherits: |
YARD::CodeObjects::Base
|
Defined in: | lib/yard/code_objects/macro_object.rb |
Overview
A MacroObject represents a docstring defined through +@!macro NAME+ and can be
reused by specifying the tag +@!macro NAME+. You can also provide the
attached
type flag to the macro definition to have it attached to the
specific DSL method so it will be implicitly reused.
Macros are fully described in the {YARD::Tags Overview} document.
Constant Summary
-
MACRO_MATCH =
# File 'lib/yard/code_objects/macro_object.rb', line 30/(\\)?\$(?:\{(-?\d+|\*)(-)?(-?\d+)?\}|(-?\d+|\*))/
Class Method Summary
-
.apply(docstring, call_params = [], full_source = '', block_source = '', _method_object = nil) ⇒ String
Applies a macro on a docstring by creating any macro data inside of the docstring first.
-
.apply_macro(macro, docstring, call_params = [], full_source = '', block_source = '') ⇒ String
Applies a macro to a docstring, interpolating the macro's data on the docstring and appending any extra local docstring data that was in the original
docstring
object. -
.create(macro_name, data, method_object = nil) ⇒ MacroObject
Creates a new macro and fills in the relevant properties.
-
.create_docstring(macro_name, data, method_object = nil)
Alias for .find_or_create.
-
.expand(macro_data, call_params = [], full_source = '', block_source = '')
Expands #macro_data using the interpolation parameters.
-
.find(macro_name) ⇒ MacroObject?
Finds a macro using
macro_name
-
.find_or_create(macro_name, data, method_object = nil) ⇒ MacroObject?
(also: .create_docstring)
Parses a given docstring and determines if the macro is "new" or not.
Base
- Inherited
Instance Attribute Summary
Base
- Inherited
#base_docstring | The non-localized documentation string associated with the object. |
#dynamic | Marks whether or not the method is conditionally defined at runtime. |
#dynamic? | Is the object defined conditionally at runtime? |
#files | The files the object was defined in. |
#group, | |
#namespace | The namespace the object is defined in. |
#namespace= | Sets the namespace the object is defined in. |
#parent | Alias for Base#namespace. |
#root?, | |
#signature | The one line signature representing an object. |
#source | The source code associated with the object. |
#source= | Attaches source code to a code object with an optional file location. |
#source_type | Language of the source code associated with the object. |
#visibility, #visibility= |
Instance Method Summary
-
#expand(call_params = [], full_source = '', block_source = '')
Expands the macro using.
-
#path
Overrides Base#path so the macro path is ".macro.MACRONAME".
-
#sep
Overrides the separator to be '.'.
Base
- Inherited
#== | Alias for Base#equal?. |
#[] | Accesses a custom attribute on the object. |
#[]= | Sets a custom attribute on the object. |
#add_file | Associates a file with a code object, optionally adding the line where it was defined. |
#add_tag | Add tags to the |
#copy_to | Copies all data in this object to another code object, except for uniquely identifying information (path, namespace, name, scope). |
#docstring | The documentation string associated with the object. |
#docstring= | Attaches a docstring to a code object by parsing the comments attached to the statement and filling the |
#eql? | Alias for Base#equal?. |
#equal? | Tests if another object is equal to this, including a proxy. |
#file | Returns the filename the object was first parsed at, taking definitions with docstrings first. |
#format | Renders the object using the templating system. |
#has_tag? | Tests if the |
#hash, | |
#initialize | Creates a new code object. |
#inspect | Inspects the object, returning the type and path. |
#line | Returns the line the object was first parsed at (or nil). |
#method_missing, | |
#name | The name of the object. |
#path | Represents the unique path of the object. |
#relative_path, | |
#sep | Override this method with a custom component separator. |
#tag | Gets a tag from the |
#tags | Gets a list of tags from the |
#title, #to_ary, | |
#to_s | Alias for Base#path. |
#type | Default type is the lowercase class name without the "Object" suffix. |
#format_source | Formats source code by removing leading indentation. |
#translate_docstring |
Constructor Details
This class inherits a constructor from YARD::CodeObjects::Base
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class YARD::CodeObjects::Base
Class Method Details
.apply(docstring, call_params = [], full_source = '', block_source = '', _method_object = nil) ⇒ String
Applies a macro on a docstring by creating any macro data inside of the docstring first. Equivalent to calling .find_or_create and .apply_macro on the new macro object.
# File 'lib/yard/code_objects/macro_object.rb', line 119
def apply(docstring, call_params = [], full_source = '', block_source = '', _method_object = nil) # rubocop:disable Lint/UnusedMethodArgument docstring = docstring.all if Docstring === docstring parser = Docstring.parser handler = OpenStruct.new handler.call_params = call_params[1..-1] handler.caller_method = call_params.first handler.statement = OpenStruct.new(:source => full_source) parser.parse(docstring, nil, handler).to_docstring.to_raw end
.apply_macro(macro, docstring, call_params = [], full_source = '', block_source = '') ⇒ String
Applies a macro to a docstring, interpolating the macro's data on the
docstring and appending any extra local docstring data that was in
the original docstring
object.
# File 'lib/yard/code_objects/macro_object.rb', line 135
def apply_macro(macro, docstring, call_params = [], full_source = '', block_source = '') # rubocop:disable Lint/UnusedMethodArgument apply(docstring, call_params, full_source, block_source) end
.create(macro_name, data, method_object = nil) ⇒ MacroObject
Creates a new macro and fills in the relevant properties.
# File 'lib/yard/code_objects/macro_object.rb', line 39
def create(macro_name, data, method_object = nil) obj = new(:root, macro_name) obj.macro_data = data obj.method_object = method_object obj end
.create_docstring(macro_name, data, method_object = nil)
Alias for .find_or_create.
# File 'lib/yard/code_objects/macro_object.rb', line 73
alias create_docstring find_or_create
.expand(macro_data, call_params = [], full_source = '', block_source = '')
Expands #macro_data using the interpolation parameters.
Interpolation rules:
- $0, $1, $2, ... = the Nth parameter in
call_params
- $* = the full statement source (excluding block)
- Also supports ${N-M} ranges, as well as negative indexes on N or M
- Use \$ to escape the variable name in a macro.
# File 'lib/yard/code_objects/macro_object.rb', line 92
def (macro_data, call_params = [], full_source = '', block_source = '') # rubocop:disable Lint/UnusedMethodArgument macro_data = macro_data.all if macro_data.is_a?(Docstring) macro_data.gsub(MACRO_MATCH) do escape = $1 first = $2 || $5 last = $4 rng = $3 ? true : false next $&[1..-1] if escape if first == '*' last ? $& : full_source else first_i = first.to_i last_i = (last ? last.to_i : call_params.size) last_i = first_i unless rng params = call_params[first_i..last_i] params ? params.join(", ") : '' end end end
.find(macro_name) ⇒ MacroObject
?
Finds a macro using macro_name
.find_or_create(macro_name, data, method_object = nil) ⇒ MacroObject
?
Also known as: .create_docstring
Parses a given docstring and determines if the macro is "new" or not. If the macro has $variable names or if it has a @!macro tag with the [new] or [attached] flag, it is considered new.
If a new macro is found, the macro is created and registered. Otherwise the macro name is searched and returned. If a macro is not found, nil is returned.
# File 'lib/yard/code_objects/macro_object.rb', line 70
def find_or_create(macro_name, data, method_object = nil) find(name) || create(macro_name, data, method_object) end
Instance Attribute Details
#attached? ⇒ Boolean
(readonly)
# File 'lib/yard/code_objects/macro_object.rb', line 148
def attached?; method_object ? true : false end
#macro_data ⇒ String (rw)
# File 'lib/yard/code_objects/macro_object.rb', line 141
attr_accessor :macro_data
#method_object ⇒ CodeObjects::Base (rw)
# File 'lib/yard/code_objects/macro_object.rb', line 145
attr_accessor :method_object
Instance Method Details
#expand(call_params = [], full_source = '', block_source = '')
Expands the macro using
# File 'lib/yard/code_objects/macro_object.rb', line 166
def (call_params = [], full_source = '', block_source = '') self.class. (macro_data, call_params, full_source, block_source) end
#path
Overrides Base#path so the macro path is ".macro.MACRONAME"
# File 'lib/yard/code_objects/macro_object.rb', line 151
def path; '.macro.' + name.to_s end
#sep
Overrides the separator to be '.'
# File 'lib/yard/code_objects/macro_object.rb', line 154
def sep; '.' end