class YARD::CodeObjects::MacroObject
property :bar
# Extra data added to docstring
property :foo
# @!method $1(value)
# @!macro [attach] prop2
@example Creating a macro that is attached to the method call
property :bar, Numeric, :value
# @!macro prop
property :foo, String, :a, :b
# @return [$2] the value of the $0
# @!method $1(${3-})
# @!macro prop
@example Creating a basic named macro
document.
Macros are fully described in the {file:docs/Tags.md#macro Tags Overview}
specific DSL method so it will be implicitly reused.attached type flag to the macro definition to have it attached to the
reused by specifying the tag +@!macro NAME+. You can also provide the
A MacroObject represents a docstring defined through +@!macro NAME+ and can be
def apply(docstring, call_params = [], full_source = '', block_source = '', _method_object = nil) # rubocop:disable Lint/UnusedMethodArgument
- See: find_or_create -
Parameters:
-
docstring(Docstring) -- the docstring to create a macro out of
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
def apply_macro(macro, docstring, call_params = [], full_source = '', block_source = '') # rubocop:disable Lint/UnusedMethodArgument
-
macro(MacroObject) -- the macro object
def apply_macro(macro, docstring, call_params = [], full_source = '', block_source = '') # rubocop:disable Lint/UnusedMethodArgument apply(docstring, call_params, full_source, block_source) end
def attached?; method_object ? true : false end
-
(Boolean)- whether this macro is attached to a method
def attached?; method_object ? true : false end
def create(macro_name, data, method_object = nil)
-
(MacroObject)- the newly created object
Parameters:
-
method_object(CodeObjects::Base) -- an object to attach this -
data(String) -- the data the macro should expand when re-used -
macro_name(String) -- the name of the macro, must be unique.
def create(macro_name, data, method_object = nil) obj = new(:root, macro_name) obj.macro_data = data obj.method_object = method_object obj end
def expand(macro_data, call_params = [], full_source = '', block_source = '') # rubocop:disable Lint/UnusedMethodArgument
-
macro_data(String) -- the macro data to expand (taken from {#macro_data}) -
block_source(String) -- Currently unused. Will support -
full_source(String) -- the full source line (excluding block) -
call_params(Array) -- the method name and parameters
Returns:
-
(String)- the expanded macro data
def expand(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
def expand(call_params = [], full_source = '', block_source = '')
- See: expand -
Other tags:
- Example: Expanding a Macro -
Parameters:
-
block_source(String) -- the source passed in the block of the method -
full_source(String) -- the full method call (not including the block) -
call_params(Array) -- a list of tokens that are passed
def expand(call_params = [], full_source = '', block_source = '') self.class.expand(macro_data, call_params, full_source, block_source) end
def find(macro_name)
-
(nil)- if there is no registered macro by that name -
(MacroObject)- if a macro is found
Parameters:
-
macro_name(#to_s) -- the name of the macro
def find(macro_name) Registry.at('.macro.' + macro_name.to_s) end
def find_or_create(macro_name, data, method_object = nil)
-
(nil)- if the +data+ has no macro tag or if the macro is -
(MacroObject)- the newly created or existing macro, depending
Parameters:
-
method_object(CodeObjects::Base) -- an optional method to attach -
macro_name(#to_s) -- the name of the macro
def find_or_create(macro_name, data, method_object = nil) find(name) || create(macro_name, data, method_object) end
def path; '.macro.' + name.to_s end
def path; '.macro.' + name.to_s end
def sep; '.' end
def sep; '.' end