Class: Inversion::Template::CodeTag
- Includes:
- AbstractClass
- Defined in:
- lib/inversion/template/codetag.rb
Overview
The base class for Inversion tags that parse the body section of the tag using a Ruby parser.
It provides a tag_pattern
declarative method that is used to specify a pattern of tokens to match, and a block for handling tag instances that match the pattern.
class Inversion::Template::MyTag < Inversion::Template::CodeTag
# Match a tag that looks like: <?my "string of stuff" ?>
tag_pattern 'tstring_beg $(tstring_content) tstring_end' do |tag, match|
tag.string = match.string( 1 )
end
end
The tokens in the tag_pattern
are Ruby token names used by the parser. If you’re creating your own tag, you can dump the tokens for a particular snippet using the ‘inversion’ command-line tool that comes with the gem:
$ inversion tagtokens 'attr.dump! {|thing| thing.length }'
ident<"attr"> period<"."> ident<"dump!"> sp<" "> lbrace<"{"> op<"|"> \
ident<"thing"> op<"|"> sp<" "> ident<"thing"> period<"."> \
ident<"length"> sp<" "> rbrace<"}">
:TODO: Finish the tag_pattern docs: placeholders, regex limitations, etc.
Direct Known Subclasses
Defined Under Namespace
Classes: TokenPattern
Constant Summary
Constants inherited from Tag
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
the body of the tag.
-
#identifiers ⇒ Object
readonly
the identifiers in the code contained in the tag.
Attributes inherited from Node
Class Method Summary collapse
-
.inherit_tag_patterns ⇒ Object
Declarative that forces a tag to inherit existing patterns from its parent, rather than replacing them.
-
.tag_pattern(token_pattern, &callback) ⇒ Object
Declare a
token_pattern
for tag bodies along with acallback
that will be called when a tag matching the pattern is instantiated. -
.tag_patterns ⇒ Object
Return the tag patterns for this class, or those of its superclass if it doesn’t override them.
Instance Method Summary collapse
-
#initialize(body, linenum = nil, colnum = nil) ⇒ CodeTag
constructor
Initialize a new tag that expects Ruby code in its
body
.
Methods included from AbstractClass
Methods included from AbstractClass::ClassMethods
Methods inherited from Tag
#as_comment_body, create, #derivatives, inherited, load, load_all, #tagname, types, #types
Methods included from MethodUtilities
#singleton_attr_accessor, #singleton_attr_reader, #singleton_attr_writer
Methods inherited from Node
#after_appending, #after_rendering, #as_comment_body, #before_appending, #before_rendering, #is_container?, #location, #render
Constructor Details
#initialize(body, linenum = nil, colnum = nil) ⇒ CodeTag
Initialize a new tag that expects Ruby code in its body
. Calls the tag’s #parse_pi_body method with the specified body
.
120 121 122 123 124 125 126 |
# File 'lib/inversion/template/codetag.rb', line 120 def initialize( body, linenum=nil, colnum=nil ) # :notnew: super @body = body.strip @identifiers = [] @matched_pattern = self.match_tag_pattern( body ) end |
Instance Attribute Details
#body ⇒ Object (readonly)
the body of the tag
134 135 136 |
# File 'lib/inversion/template/codetag.rb', line 134 def body @body end |
#identifiers ⇒ Object (readonly)
the identifiers in the code contained in the tag
137 138 139 |
# File 'lib/inversion/template/codetag.rb', line 137 def identifiers @identifiers end |
Class Method Details
.inherit_tag_patterns ⇒ Object
Declarative that forces a tag to inherit existing patterns from its parent, rather than replacing them. Afterwards, you can use tag_pattern
regularly, appending to the list.
108 109 110 111 |
# File 'lib/inversion/template/codetag.rb', line 108 def self::inherit_tag_patterns raise ScriptError, "Patterns already exist for this tag." if defined?( @tag_patterns ) @tag_patterns = self.superclass.tag_patterns end |
.tag_pattern(token_pattern, &callback) ⇒ Object
Declare a token_pattern
for tag bodies along with a callback
that will be called when a tag matching the pattern is instantiated. The callback
will be called with the tag instance, and the MatchData object that resulted from matching the input, and should set up the yielded tag
object appropriately.
98 99 100 101 102 |
# File 'lib/inversion/template/codetag.rb', line 98 def self::tag_pattern( token_pattern, &callback ) #:yield: tag, match_data pattern = TokenPattern.compile( token_pattern ) @tag_patterns = [] unless defined?( @tag_patterns ) @tag_patterns << [ pattern, callback ] end |
.tag_patterns ⇒ Object
Return the tag patterns for this class, or those of its superclass if it doesn’t override them.
88 89 90 91 |
# File 'lib/inversion/template/codetag.rb', line 88 def self::tag_patterns return @tag_patterns if defined?( @tag_patterns ) return self.superclass.tag_patterns end |