Class: Inversion::Template::ImportTag

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

Overview

Inversion import tag.

The import tag copies one or more attributes from an enclosing template so they only need to be set once.

Syntax

<?import txn ?>
<?import foo, bar ?>

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

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, #before_appending, #before_rendering, #is_container?, #location

Constructor Details

#initialize(body, linenum = nil, colnum = nil) ⇒ ImportTag

Create a new ImportTag with the given ‘name`, which should be a valid Ruby identifier.



21
22
23
24
# File 'lib/inversion/template/importtag.rb', line 21

def initialize( body, linenum=nil, colnum=nil )
  super
  @attributes = body.split( /\s*,\s*/ ).collect {|name| name.strip.to_sym }
end

Instance Attribute Details

#attributesObject (readonly)

the names of the attributes to import



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

def attributes
  @attributes
end

Instance Method Details

#as_comment_bodyObject

Render the tag as the body of a comment, suitable for template debugging.



67
68
69
# File 'lib/inversion/template/importtag.rb', line 67

def as_comment_body
  return "Import %s" % [ self.attributes.join(', ') ]
end

#render(renderstate) ⇒ Object

Merge the inherited renderstate into the current template’s ‘renderstate`.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/inversion/template/importtag.rb', line 36

def render( renderstate )
  if (( cstate = renderstate.containerstate ))
    # self.log.debug "Importing inherited attributes: %p from %p" %
    # [ @attributes, cstate.attributes ]

    # Pick out the attributes that are being imported
    inherited_attrs = @attributes.inject( {} ) do |attrs, key|
      attrs[ key ] = cstate.attributes[ key ]
      attrs
    end

    # Merge, but overwrite unset values with inherited ones
    renderstate.attributes.merge!( inherited_attrs ) do |key, oldval, newval|
      if oldval.nil?
        # self.log.debug "Importing attribute %p: %p" % [ key, newval ]
        newval
      else
        # self.log.debug "Not importing attribute %p: already set to %p" % [ key, oldval ]
        oldval
      end
    end

  else
    self.log.debug "No-op import: no parent attributes set."
  end

  return nil
end