Class: YARD::CodeObjects::MacroObject

### Home Classes Methods

### In Files

- lib/yard/code_objects/macro_object.rb

### Parent

YARD::CodeObjects::Base

### Methods

- ::apply
- ::apply_macro
- ::create
- ::create_docstring
- ::expand
- ::find
- ::find_or_create
- #attached?
- #expand
- #path
- #sep

### Files

- LICENSE.txt
- MIT.txt

### Class/Module Index

Quicksearch

- YARD
- YARD::CLI
- YARD::CLI::Command
- YARD::CLI::CommandParser
- YARD::CLI::Config
- YARD::CLI::Diff
- YARD::CLI::Display
- YARD::CLI::Gems
- YARD::CLI::Graph
- YARD::CLI::GraphOptions
- YARD::CLI::Help
- YARD::CLI::I18n
- YARD::CLI::List
- YARD::CLI::MarkupTypes
- YARD::CLI::Server
- YARD::CLI::Stats
- YARD::CLI::YRI
- YARD::CLI::Yardoc
- YARD::CLI::YardocOptions
- YARD::CLI::YardoptsCommand
- YARD::CodeObjects
- YARD::CodeObjects::Base
- YARD::CodeObjects::ClassObject
- YARD::CodeObjects::ClassVariableObject
- YARD::CodeObjects::CodeObjectList
- YARD::CodeObjects::ConstantObject
- YARD::CodeObjects::ExtendedMethodObject
- YARD::CodeObjects::ExtraFileObject
- YARD::CodeObjects::MacroObject
- YARD::CodeObjects::MethodObject
- YARD::CodeObjects::ModuleObject
- YARD::CodeObjects::NamespaceObject
- YARD::CodeObjects::Proxy
- YARD::CodeObjects::ProxyMethodError
- YARD::CodeObjects::RootObject
- YARD::Config
- YARD::Docstring
- YARD::DocstringParser
- YARD::Handlers
- YARD::Handlers::Base
- YARD::Handlers::C
- YARD::Handlers::C::AliasHandler
- YARD::Handlers::C::AttributeHandler
- YARD::Handlers::C::Base
- YARD::Handlers::C::ClassHandler
- YARD::Handlers::C::ConstantHandler
- YARD::Handlers::C::HandlerMethods
- YARD::Handlers::C::InitHandler
- YARD::Handlers::C::MethodHandler
- YARD::Handlers::C::MixinHandler
- YARD::Handlers::C::ModuleHandler
- YARD::Handlers::C::OverrideCommentHandler
- YARD::Handlers::C::PathHandler
- YARD::Handlers::C::StructHandler
- YARD::Handlers::C::SymbolHandler
- YARD::Handlers::HandlerAborted
- YARD::Handlers::NamespaceMissingError
- YARD::Handlers::Processor
- YARD::Handlers::Ruby
- YARD::Handlers::Ruby::AliasHandler
- YARD::Handlers::Ruby::AttributeHandler
- YARD::Handlers::Ruby::Base
- YARD::Handlers::Ruby::ClassConditionHandler
- YARD::Handlers::Ruby::ClassHandler
- YARD::Handlers::Ruby::ClassVariableHandler
- YARD::Handlers::Ruby::CommentHandler
- YARD::Handlers::Ruby::ConstantHandler
- YARD::Handlers::Ruby::DSLHandler
- YARD::Handlers::Ruby::DSLHandlerMethods
- YARD::Handlers::Ruby::ExceptionHandler
- YARD::Handlers::Ruby::ExtendHandler
- YARD::Handlers::Ruby::HandlesExtension
- YARD::Handlers::Ruby::Legacy
- YARD::Handlers::Ruby::Legacy::AliasHandler
- YARD::Handlers::Ruby::Legacy::AttributeHandler
- YARD::Handlers::Ruby::Legacy::Base
- YARD::Handlers::Ruby::Legacy::ClassConditionHandler
- YARD::Handlers::Ruby::Legacy::ClassHandler
- YARD::Handlers::Ruby::Legacy::ClassVariableHandler
- YARD::Handlers::Ruby::Legacy::CommentHandler
- YARD::Handlers::Ruby::Legacy::ConstantHandler
- YARD::Handlers::Ruby::Legacy::DSLHandler
- YARD::Handlers::Ruby::Legacy::ExceptionHandler
- YARD::Handlers::Ruby::Legacy::ExtendHandler
- YARD::Handlers::Ruby::Legacy::MethodHandler
- YARD::Handlers::Ruby::Legacy::MixinHandler
- YARD::Handlers::Ruby::Legacy::ModuleFunctionHandler
- YARD::Handlers::Ruby::Legacy::ModuleHandler
- YARD::Handlers::Ruby::Legacy::PrivateClassMethodHandler
- YARD::Handlers::Ruby::Legacy::PrivateConstantHandler
- YARD::Handlers::Ruby::Legacy::VisibilityHandler
- YARD::Handlers::Ruby::Legacy::YieldHandler
- YARD::Handlers::Ruby::MethodCallWrapper
- YARD::Handlers::Ruby::MethodConditionHandler
- YARD::Handlers::Ruby::MethodHandler
- YARD::Handlers::Ruby::MixinHandler
- YARD::Handlers::Ruby::ModuleFunctionHandler
- YARD::Handlers::Ruby::ModuleHandler
- YARD::Handlers::Ruby::PrivateClassMethodHandler
- YARD::Handlers::Ruby::PrivateConstantHandler
- YARD::Handlers::Ruby::StructHandlerMethods
- YARD::Handlers::Ruby::TestNodeWrapper
- YARD::Handlers::Ruby::VisibilityHandler
- YARD::Handlers::Ruby::YieldHandler
- YARD::I18n
- YARD::I18n::Locale
- YARD::I18n::Message
- YARD::I18n::Messages
- YARD::I18n::POParser
- YARD::I18n::PotGenerator
- YARD::I18n::Text
- YARD::Logger
- YARD::Options
- YARD::Parser
- YARD::Parser::Base
- YARD::Parser::C
- YARD::Parser::C::BodyStatement
- YARD::Parser::C::CParser
- YARD::Parser::C::Comment
- YARD::Parser::C::CommentParser
- YARD::Parser::C::Statement
- YARD::Parser::C::ToplevelStatement
- YARD::Parser::OrderedParser
- YARD::Parser::ParserSyntaxError
- YARD::Parser::Ruby
- YARD::Parser::Ruby::AstNode
- YARD::Parser::Ruby::ClassNode
- YARD::Parser::Ruby::CommentNode
- YARD::Parser::Ruby::ConditionalNode
- YARD::Parser::Ruby::KeywordNode
- YARD::Parser::Ruby::Legacy
- YARD::Parser::Ruby::Legacy::RubyLex
- YARD::Parser::Ruby::Legacy::RubyLex::BufferedReader
- YARD::Parser::Ruby::Legacy::RubyParser
- YARD::Parser::Ruby::Legacy::RubyToken
- YARD::Parser::Ruby::Legacy::RubyToken::TkBlockContents
- YARD::Parser::Ruby::Legacy::RubyToken::TkError
- YARD::Parser::Ruby::Legacy::RubyToken::TkId
- YARD::Parser::Ruby::Legacy::RubyToken::TkKW
- YARD::Parser::Ruby::Legacy::RubyToken::TkNode
- YARD::Parser::Ruby::Legacy::RubyToken::TkOPASGN
- YARD::Parser::Ruby::Legacy::RubyToken::TkOp
- YARD::Parser::Ruby::Legacy::RubyToken::TkStatementEnd
- YARD::Parser::Ruby::Legacy::RubyToken::TkUnknownChar
- YARD::Parser::Ruby::Legacy::RubyToken::TkVal
- YARD::Parser::Ruby::Legacy::RubyToken::TkWhitespace
- YARD::Parser::Ruby::Legacy::RubyToken::Token
- YARD::Parser::Ruby::Legacy::Statement
- YARD::Parser::Ruby::Legacy::StatementList
- YARD::Parser::Ruby::Legacy::TokenList
- YARD::Parser::Ruby::LiteralNode
- YARD::Parser::Ruby::LoopNode
- YARD::Parser::Ruby::MethodCallNode
- YARD::Parser::Ruby::MethodDefinitionNode
- YARD::Parser::Ruby::ModuleNode
- YARD::Parser::Ruby::ParameterNode
- YARD::Parser::Ruby::ReferenceNode
- YARD::Parser::Ruby::RipperParser
- YARD::Parser::Ruby::RubyParser
- YARD::Parser::SourceParser
- YARD::Parser::UndocumentableError
- YARD::Rake
- YARD::Rake::YardocTask
- YARD::Registry
- YARD::RegistryStore
- YARD::Serializers
- YARD::Serializers::Base
- YARD::Serializers::FileSystemSerializer
- YARD::Serializers::ProcessSerializer
- YARD::Serializers::StdoutSerializer
- YARD::Serializers::YardocSerializer
- YARD::Server
- YARD::Server::Adapter
- YARD::Server::Commands
- YARD::Server::Commands::Base
- YARD::Server::Commands::DisplayFileCommand
- YARD::Server::Commands::DisplayObjectCommand
- YARD::Server::Commands::FramesCommand
- YARD::Server::Commands::LibraryCommand
- YARD::Server::Commands::LibraryIndexCommand
- YARD::Server::Commands::LibraryOptions
- YARD::Server::Commands::ListCommand
- YARD::Server::Commands::SearchCommand
- YARD::Server::Commands::StaticFileCommand
- YARD::Server::DocServerHelper
- YARD::Server::DocServerSerializer
- YARD::Server::FinishRequest
- YARD::Server::LibraryNotPreparedError
- YARD::Server::LibraryVersion
- YARD::Server::NotFoundError
- YARD::Server::RackAdapter
- YARD::Server::RackMiddleware
- YARD::Server::Router
- YARD::Server::StaticCaching
- YARD::Server::WebrickAdapter
- YARD::Server::WebrickServlet
- YARD::StubProxy
- YARD::Tags
- YARD::Tags::AttributeDirective
- YARD::Tags::DefaultFactory
- YARD::Tags::DefaultTag
- YARD::Tags::Directive
- YARD::Tags::EndGroupDirective
- YARD::Tags::GroupDirective
- YARD::Tags::Library
- YARD::Tags::MacroDirective
- YARD::Tags::MethodDirective
- YARD::Tags::OptionTag
- YARD::Tags::OverloadTag
- YARD::Tags::ParseDirective
- YARD::Tags::RefTag
- YARD::Tags::RefTagList
- YARD::Tags::ScopeDirective
- YARD::Tags::Tag
- YARD::Tags::TagFormatError
- YARD::Tags::VisibilityDirective
- YARD::Templates
- YARD::Templates::Engine
- YARD::Templates::ErbCache
- YARD::Templates::Helpers
- YARD::Templates::Helpers::BaseHelper
- YARD::Templates::Helpers::FilterHelper
- YARD::Templates::Helpers::HtmlHelper
- YARD::Templates::Helpers::HtmlSyntaxHighlightHelper
- YARD::Templates::Helpers::Markup
- YARD::Templates::Helpers::Markup::RDocMarkdown
- YARD::Templates::Helpers::Markup::RDocMarkup
- YARD::Templates::Helpers::Markup::RDocMarkup::RDocMarkup
- YARD::Templates::Helpers::Markup::RDocMarkup::RDocMarkup::RDocMarkupToHtml
- YARD::Templates::Helpers::Markup::RDocMarkup::RDocMarkupToHtml
- YARD::Templates::Helpers::MarkupHelper
- YARD::Templates::Helpers::MethodHelper
- YARD::Templates::Helpers::ModuleHelper
- YARD::Templates::Helpers::TextHelper
- YARD::Templates::Helpers::UMLHelper
- YARD::Templates::Section
- YARD::Templates::Template
- YARD::Templates::Template::ClassMethods
- YARD::Templates::TemplateOptions
- YARD::Verifier
- Gem
- Gem::DocManager
- Gem::SourceIndex
- Gem::Specification
- Rack
- Rack::Request
- WEBrick
- WEBrick::HTTPRequest
- Array
- File
- Hash
- Insertion
- Module
- Object
- String
- SymbolHash

No matching classes.

# YARD::CodeObjects::MacroObject

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 {file:docs/Tags.md#macro Tags Overview} document.

@example Creating a basic named macro

    # @!macro prop
    #   @!method $1(${3-})
    #   @return [$2] the value of the $0
    property :foo, String, :a, :b

    # @!macro prop
    property :bar, Numeric, :value

@example Creating a macro that is attached to the method call

    # @!macro [attach] prop2
    #   @!method $1(value)
    property :foo

    # Extra data added to docstring
    property :bar

### Constants

MACRO_MATCH

### Attributes

macro_data[RW]

@return [String] the macro data stored on the object

method_object[RW]

@return [CodeObjects::Base] the method object that this macro is

    attached to.

### Public Class Methods

apply(docstring, call_params = [], full_source = '', block_source = '', method_object = nil) click to toggle source

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.

@param [Docstring] docstring the docstring to create a macro out of @!macro macro.expand @see find_or_create

    # File lib/yard/code_objects/macro_object.rb, line 120
    def apply(docstring, call_params = [], full_source = '', block_source = '', method_object = nil)
      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 = '') click to toggle source

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.

@param [MacroObject] macro the macro object @!macro macro.expand

    # File lib/yard/code_objects/macro_object.rb, line 136
    def apply_macro(macro, docstring, call_params = [], full_source = '', block_source = '')
      apply(docstring, call_params, full_source, block_source)
    end

create(macro_name, data, method_object = nil) click to toggle source

Creates a new macro and fills in the relevant properties. @param [String] macro_name the name of the macro, must be unique. @param [String] data the data the macro should expand when re-used @param [CodeObjects::Base] method_object an object to attach this

    macro to. If supplied, {#attached?} will be true

@return [MacroObject] the newly created object

    # 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) click to toggle source
Alias for: find_or_create
expand(macro_data, call_params = [], full_source = '', block_source = '') click to toggle 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.

@!macro [new] macro.expand

    @param [Array<String>] call_params the method name and parameters
      to the method call. These arguments will fill \$0-N
    @param [String] full_source the full source line (excluding block)
      interpolated as \$*
    @param [String] block_source Currently unused. Will support
      interpolating the block data as a variable.
    @return [String] the expanded macro data

@param [String] macro_data the macro data to expand (taken from {macro_data})

    # File lib/yard/code_objects/macro_object.rb, line 96
    def expand(macro_data, call_params = [], full_source = '', block_source = '')
      macro_data = macro_data.all if macro_data.is_a?(Docstring)
      macro_data.gsub(MACRO_MATCH) do
        escape, first, last, rng = $1, $2 || $5, $4, $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) click to toggle source

Finds a macro using `macro_name` @param [to_s] macro_name the name of the macro @return [MacroObject] if a macro is found @return [nil] if there is no registered macro by that name

    # File lib/yard/code_objects/macro_object.rb, line 50
    def find(macro_name)
      Registry.at('.macro.' + macro_name.to_s)
    end

find_or_create(macro_name, data, method_object = nil) click to toggle source

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.

@param [to_s] macro_name the name of the macro @param [CodeObjects::Base] method_object an optional method to attach

    the macro to. Only used if the macro is being created, otherwise
    this argument is ignored.

@return [MacroObject] the newly created or existing macro, depending

    on whether the @!macro tag was a new tag or not.

@return [nil] if the `data` has no macro tag or if the macro is

    not new and no macro by the macro name is found.

    # File lib/yard/code_objects/macro_object.rb, line 70
    def find_or_create(macro_name, data, method_object = nil)
      if macro = find(name)
        macro
      else
        create(macro_name, data, method_object)
      end
    end

Also aliased as: create_docstring

### Public Instance Methods

attached?() click to toggle source

@return [Boolean] whether this macro is attached to a method

    # File lib/yard/code_objects/macro_object.rb, line 149
    def attached?; method_object ? true : false end

expand(call_params = [], full_source = '', block_source = '') click to toggle source

Expands the macro using @param [Array<String>] call_params a list of tokens that are passed

    to the method call

@param [String] full_source the full method call (not including the block) @param [String] block_source the source passed in the block of the method

    call, if there is a block.

@example Expanding a Macro

    macro.expand(%w(property foo bar), 'property :foo, :bar', '') #=>
      "...macro data interpolating this line of code..."

@see expand

    # File lib/yard/code_objects/macro_object.rb, line 167
    def expand(call_params = [], full_source = '', block_source = '')
      self.class.expand(macro_data, call_params, full_source, block_source)
    end

path() click to toggle source

Overrides {Base#path} so the macro path is ".macro.MACRONAME"

    # File lib/yard/code_objects/macro_object.rb, line 152
    def path; '.macro.' + name.to_s end

sep() click to toggle source

Overrides the separator to be '.'

    # File lib/yard/code_objects/macro_object.rb, line 155
    def sep; '.' end

[Validate]

Generated with the Darkfish Rdoc Generator 2.