Class: Arrow::Template::ImportDirective

Inherits:
Directive show all
Includes:
Parser::Patterns
Defined in:
lib/arrow/template/import.rb

Overview

The Arrow::Template::ImportDirective class, a derivative of Arrow::Template::Directive. This is the class which defines the behaviour of the ‘import’ template directive.

Syntax

  <?import foo?>
  <?import foo as superfoo?>

Authors

  • Michael Granger

Please see the file LICENSE in the top-level directory for licensing details.

Constant Summary

SVNRev =

SVN Revision

%q$Rev$
SVNId =

SVN Id

%q$Id$
SIMPLEIMPORT =

Various patterns

CAPTURE[ IDENTIFIER ]
ALIASIMPORT =
CAPTURE[ IDENTIFIER ] + /\s+as\s+/i + CAPTURE[ IDENTIFIER ]

Constants included from Parser::Patterns

ALTERNATION, ARGDEFAULT, ARGUMENT, CAPTURE, COMMA, DBLQSTRING, DOT, EQUALS, IDENTIFIER, INFIX, LBRACKET, NUMBER, PATHNAME, QUOTEDSTRING, RBRACKET, REBINDOP, REGEXP, SLASHQSTRING, SYMBOL, TAGCLOSE, TAGMIDDLE, TAGOPEN, TICKQSTRING, VARIABLE, WHITESPACE

Constants inherited from Directive

SVNId, SVNRev

Constants inherited from Node

SVNId, SVNRev

Constants included from Arrow::HTMLUtilities

ARRAY_HTML_CONTAINER, HASH_HTML_CONTAINER, HASH_PAIR_HTML, IMMEDIATE_OBJECT_HTML_CONTAINER, IVAR_HTML_FRAGMENT, OBJECT_HTML_CONTAINER, THREAD_DUMP_KEY

Instance Attribute Summary

Class Method Summary

Instance Method Summary

Methods inherited from Directive

create, derivativeDirs, #inspect, #to_html

Methods inherited from Node

#add_to_template, #css_class, #inspect, #is_rendering_node?, #to_a, #to_html, #to_s

Methods included from Arrow::HTMLUtilities

#escape_html, #make_html_for_object, #make_object_html_wrapper

Methods inherited from Arrow::Object

deprecate_class_method, deprecate_method, inherited

Methods included from Arrow::Loggable

#log

Constructor Details

- (ImportDirective) initialize(type, parser, state)

Create a new ImportDirective object.



50
51
52
53
# File 'lib/arrow/template/import.rb', line 50

def initialize( type, parser, state )
  @imports = {}
  super
end

Instance Attribute Details

- (Object) patterns (readonly)

An Array of Regexp objects which match the names of attributes to be imported.



62
63
64
# File 'lib/arrow/template/import.rb', line 62

def patterns
  @patterns
end

Class Method Details

+ (Boolean) allows_format?

Disallow formats

Returns:

  • (Boolean)


42
# File 'lib/arrow/template/import.rb', line 42

def self::allows_format?; false end

Instance Method Details

- (Object) parse_directive_contents(parser, state) (protected)

Parse the contents of the directive.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/arrow/template/import.rb', line 90

def parse_directive_contents( parser, state )
  super

  state.scanner.skip( WHITESPACE )
  #self.log.debug "Scanning for tag middle at: '%20s'" % state.scanner.rest

  body = state.scanner.scan( state.tag_middle ) or return nil
  #self.log.debug "Found body = %p" % body

  body.strip.split( /\s*,\s*/ ).each do |import|
    #self.log.debug "Parsing import: %p" % import
    case import
    when ALIASIMPORT
      @imports[ $1 ] = $2
      #self.log.debug "Alias import: %s => %s" % 
      # [ $1, $2 ]

    when SIMPLEIMPORT
      @imports[ $1 ] = $1
      #self.log.debug "Simple import: %s" % $1

    else
      raise Arrow::ParseError, "Failed to parse body: %p" % body
    end
  end

  return true
end

- (Object) render(template, scope)

Add the imported attributes when this node is rendered.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/arrow/template/import.rb', line 66

def render( template, scope )
  imports = []

  if (( st = template._enclosing_template ))
    @imports.each do |source,dest|
      imports << "%s as %s (%p)" %
        [ source, dest, st._attributes[source] ]
      template._attributes[dest] = st._attributes[source]
    end
  end

  if template._config[:debuggingComments]
    return template.render_comment( "Importing: " + imports.join(", ") )
  else
    return ''
  end
end