Class: Arrow::Template::AttributeDirective

Inherits:
Directive show all
Defined in:
lib/arrow/template/nodes.rb

Overview

The attribute directive superclass. Attribute directives are those that present an exterior interface to the controlling system for message-passing and content-injection (e.g., , , , etc.)

Direct Known Subclasses

AttrDirective, BracketingDirective, CallDirective, CommentDirective, ElsifDirective, RenderDirective

Constant Summary

SVNRev =

SVN Revision

%q$Rev$
SVNId =

SVN Id

%q$Id$

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

Methods inherited from Node

#add_to_template, #css_class, #to_a, #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

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

Initialize a new AttributeDirective with the given tag name, template parser, and parser state.



369
370
371
372
373
374
# File 'lib/arrow/template/nodes.rb', line 369

def initialize( type, parser, state ) # :notnew:
  @name = nil
  @format = nil
  @methodchain = nil
  super
end

Instance Attribute Details

- (Object) format

The format string that was specified with the directive, if any



386
387
388
# File 'lib/arrow/template/nodes.rb', line 386

def format
  @format
end

- (Object) methodchain

The source code for the methodchain that will be used to render the attribute.



383
384
385
# File 'lib/arrow/template/nodes.rb', line 383

def methodchain
  @methodchain
end

- (Object) name (readonly)

The name of the directive, which is used to associate it with a attribute in the template the node belongs to.



390
391
392
# File 'lib/arrow/template/nodes.rb', line 390

def name
  @name
end

Class Method Details

+ (Boolean) allows_format?

Returns true for classes that support a prepended format. (e.g., ).

Returns:

  • (Boolean)


358
359
360
# File 'lib/arrow/template/nodes.rb', line 358

def self::allows_format?
  true
end

Instance Method Details

- (Object) before_rendering(template)

Try to pre-render any attributes which correspond to this node.



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/arrow/template/nodes.rb', line 402

def before_rendering( template )
  if attrib = template[ self.name ]
    # self.log.debug "  got %s attribute in #before_rendering for %p" %
      # [ attrib.class.name, self.name ]

    if attrib.respond_to?( :before_rendering )
      # self.log.debug "  pre-rendering attribute %p" % [attrib]
      attrib.before_rendering( template )
    elsif attrib.respond_to?( :each )
      # self.log.debug "  iterating over attribute %p" % [attrib]
      attrib.each do |obj|
        obj.before_rendering if obj.respond_to?( :before_rendering )
      end
    end
  else
    # No-op
    # self.log.debug "  no value for node %p in #before_rendering" %
      # self.name
  end
end

- (Object) build_rendering_proc(template, scope) (protected)

Build a Proc object that encapsulates the execution necessary to render the directive.



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/arrow/template/nodes.rb', line 510

def build_rendering_proc( template, scope )
  return nil if self.format.nil? && self.methodchain.nil?

  if self.format
    code = %(Proc.new {|%s| "%s" %% %s%s}) % 
      [ self.name, self.format, self.name, self.methodchain ]
  else
    code = "Proc.new {|%s| %s%s}" %
      [ self.name, self.name, self.methodchain ]
  end
  code.untaint

  #self.log.debug "Rendering proc code is: %p" % code
  desc = "[%s (%s): %s]" %
    [ self.class.name, __FILE__, code ]

  return eval( code, scope.get_binding, desc, __LINE__ )
end

- (Object) call_methodchain(template, scope, *args) (protected)

Call the node’s methodchain, if any, passing the associated attribute as the first argument and any additional args as second and succeeding arguments. Returns the results of the call.



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/arrow/template/nodes.rb', line 533

def call_methodchain( template, scope, *args )
  chain = self.build_rendering_proc( template, scope )
  # self.log.debug "Rendering proc is: %p" % chain

  # self.log.debug "Fetching attribute %p of template %p" %
    # [ self.name, template ]
  attribute = template.send( self.name )
  # self.log.debug "Attribute to be rendered (%s) is: %p" %
    # [ self.name, attribute ]

  if chain
    return chain.call( attribute, *args )
  else
    return attribute
  end
end

- (Object) inspect

Return a human-readable version of the object suitable for debugging messages.



436
437
438
439
440
441
442
443
# File 'lib/arrow/template/nodes.rb', line 436

def inspect
  %Q{<%s %s%s (Format: %p)>} % [
    @type.capitalize,
    @name,
    @methodchain.strip.empty? ? "" : @methodchain,
    @format,
  ]
end

- (Boolean) is_rendering_node?

Returns true for nodes which generate output themselves (as opposed to ones which generate output through subnodes). This is used for eliding blank lines from the node tree.

Returns:

  • (Boolean)


396
397
398
# File 'lib/arrow/template/nodes.rb', line 396

def is_rendering_node?
  true
end

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

Parse the contents of the directive, looking for an optional format for tags like , then a required identifier, then an optional methodchain attached to the identifier.



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/arrow/template/nodes.rb', line 473

def parse_directive_contents( parser, state )
  super

  # Look for a format
  if self.class.allows_format?
    if fmt = parser.scan_for_quoted_string( state )
      state.scanner.skip( /\s*%\s*/ ) or
        raise Arrow::ParseError, "Format missing modulus operator?"
      @format = fmt[1..-2]
      #self.log.debug "Found format %p" % @format
    else
      #self.log.debug "No format string"
      @format = nil
    end
  end

  # Look for the identifier
  @name = parser.scan_for_identifier( state ) or
    raise Arrow::ParseError, "missing or malformed indentifier"
  #self.log.debug "Set name of %s to %p" %
  #  [ self.class.name, @name ]

  # Now pick up the methodchain if there is one
  @methodchain = parser.scan_for_methodchain( state )

  return true
end

- (Object) render(template, scope)

Render the directive node’s contents as a String and return it.



425
426
427
428
429
430
431
# File 'lib/arrow/template/nodes.rb', line 425

def render( template, scope )
  # self.log.debug "Rendering %p" % self
  rary = super

  rary.push( *(self.render_contents( template, scope )) )
  return rary
end

- (Object) render_contents(template, scope) (protected)

Render the contents of the node



503
504
505
# File 'lib/arrow/template/nodes.rb', line 503

def render_contents( template, scope )
  return self.call_methodchain( template, scope )
end

- (Object) to_html

Return an HTML fragment that can be used to represent the node symbolically in a web-based introspection interface.



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/arrow/template/nodes.rb', line 448

def to_html
  html = ''
  if @format
    html << %q{"%s" %% } % self.escape_html( @format )
  end
  html << %q{<strong>#%s</strong>} % @name
  if @methodchain
    html << self.escape_html( @methodchain )
  end

  if block_given?
    html << " " << yield
  end

  super { html }
end