Class: Jig::ALGap

Inherits:
Gap
  • Object
show all
Defined in:
lib/jig/xml.rb

Overview

Jig::XML is a subclass of Jig designed to simplify construction of

strings containing XML text.  Several class methods are defined to
construct standard XML elements.  Unknown class method calls are
interpreted as XML element constructions:

  j = Jig::XML.xml               
  j.inspect         # => #<Jig: ["<?xml", " version=\"1.0\"", " ?>\n"]>
  j.to_s            # => <?xml version="1.0" ?>\n

  j = Jig::XML.book(Jig::XMLtitle('War and Peace'))
  j.inspect         # => #<Jig: ["<book", nil, ">", "<title", nil, ">\n", "War and Peace", "</title>\n", "</book>\n"]>
  j.to_s            # => <book><title>\nWar and Peace</title>\n </book>

For most element constructors, arguments to the constructor are inserted as
the contents of the element.  An optional block argument is inserted as a
proc.  If there are no arguments and no block, the element is constructed
with the default gap, :___, as its contents. XML attributes are specified by
passing a Hash as the final argument.  See the Attributes section below for
a detailed description of attribute processing.
Examples:

   X = Jig::XHTML
   puts X.span('some text')            # <span>some text</span>

   puts X.h3('rendered at: ') do       # <h3>rendered at: Mon Apr 16 23:02:13 EDT 2007</h3>
     Time.now
   end

   j = X.p
   puts j                              # <p>\n</p>
   puts j.plug('Four score...')        # <p>Four score...\n</p>

=Attributes

A hash passed as a final argument to an XML element constructor is interpreted
as potential XML attributes for the element.  Each key/value pair is considered 
seperately.  Pairs are processed as follows:
* a nil value causes the pair to be silently discarded
* a symbol value causes an attribute gap to be inserted into the XML element
* a gap value is inserted as is into the XML element attribute area, the key is discarded in this case (XXX)
* a proc is inserted as a deferred attribute
* a jig is inserted as deferred attribute
* any other value is inserted as an attribute with the value converted via #to_s

=Attribute Gaps

If an attribute gap remains unfilled it will not appear in the rendered jig.  When
an attribute gap is filled, the result is processed as described for the key/value
pairs of a hash.

=Deferred Attributes

A deferred attribute is not finalized until the jig is rendered via #to_s. If a
deferred proc evaluates to nil, the attribute pair is silently discarded otherwise
the resulting value is converted to a string and an XML attribute is rendered.
A deferred jig is rendered and the result used as the XML attribute value.

  X = Jig::XHTML
  X.div('inner', :class => 'urgent')           # => <div class="urgent">inner</div>
  j = X.div('inner', :class => :class)         # => <div>inner</div>
  j.plug(:class, 'urgent')                     # => <div class="urgent">inner</div>
  j.plug(:class, nil)                          # => <div>inner</div>

  j = X.input(:type => :type)                  # => <input/>
  j.plug(:type, "")                            # => <input type="" />
  j.plug(:type, 'password')                    # => <input type="password" />

  css = nil
  j = X.div('inner', :class => proc { css })   # => <div>inner</div>
  css = 'urgent'
  j.to_s                                       # => <div class="header">inner</div>

  color = Jig.new('color: ', { %w{reb blue green}[rand(3)] }
  j = X.div('inner', :style=> color)           # => <div style="color: red">inner</div>
  j.to_s                                       # => <div style="color: green">inner</div>

Constant Summary

Constants inherited from Gap

Gap::ATTRS, Gap::GAP

Instance Attribute Summary

Attributes inherited from Gap

#filter, #name

Instance Method Summary collapse

Methods inherited from Gap

#==, comment, #initialize, #inspect, #rename, #terse_inspect, wrap

Constructor Details

This class inherits a constructor from Jig::Gap

Instance Method Details

#fill(h) ⇒ Object



83
84
85
# File 'lib/jig/xml.rb', line 83

def fill(h)
  h && h.map { |k,v| Jig::XML.attribute(k, v) }
end