Class: Inversion::Template::RescueTag

Inherits:
Tag
  • Object
show all
Defined in:
lib/inversion/template/rescuetag.rb

Overview

Inversion ‘rescue’ tag.

This tag adds a logical switch to a BeginTag. If rendering any of the BeginTag’s nodes raises an exception of the type specified by the RescueTag, the nodes following the RescueTag are rendered instead.

Syntax

<?begin ?>
    <?for employee in employees.all ?>
        <?attr employee.name ?> --> <?attr employee.title ?>
    <?end for?>
<?rescue DatabaseError => err ?>
    Oh no!! I can't talk to the database for some reason.  The
    error was as follows:

        <?attr err.message ?>

<?end?>

Constant Summary

Constants inherited from Tag

Tag::TAG_PLUGIN_PATTERN

Instance Attribute Summary collapse

Attributes inherited from Tag

#body

Attributes inherited from Node

#colnum, #linenum

Instance Method Summary collapse

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 included from AbstractClass

included

Methods included from AbstractClass::ClassMethods

#inherited, #pure_virtual

Methods inherited from Node

#after_appending, #after_rendering, #as_comment_body, #before_rendering, #is_container?, #location, #render

Constructor Details

#initialize(body = '', linenum = nil, colnum = nil) ⇒ RescueTag

Overridden to default body to nothing, and raise an error if it has one.



31
32
33
34
# File 'lib/inversion/template/rescuetag.rb', line 31

def initialize( body='', linenum=nil, colnum=nil ) # :notnew:
	super
	@exception_types = parse_exception_types( self.body )
end

Instance Attribute Details

#exception_typesObject (readonly)

The exception classes the rescue will handle (an Array of Class objects)



42
43
44
# File 'lib/inversion/template/rescuetag.rb', line 42

def exception_types
  @exception_types
end

Instance Method Details

#before_appending(parsestate) ⇒ Object

Parsing callback – check to be sure the node tree can have the ‘rescue’ tag appended to it.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/inversion/template/rescuetag.rb', line 47

def before_appending( parsestate )
	condtag = parsestate.node_stack.reverse.find do |node|
		case node

		# If there was a previous 'begin', the 'rescue' belongs to it. Also
		# allow it to be appended to a 'comment' section so you can comment out a
		# rescue clause without commenting out the begin
		when Inversion::Template::BeginTag,
		     Inversion::Template::CommentTag
			break node

		# If it's some other kind of container, it's an error
		when Inversion::Template::ContainerTag
			raise Inversion::ParseError, "'%s' tags can't have '%s' clauses" %
				[ node.tagname.downcase, self.tagname.downcase ]
		end
	end

	# If there wasn't a valid container, it's an error too
	raise Inversion::ParseError, "orphaned '%s' tag" % [ self.tagname.downcase ] unless condtag
end