Class: Builder::XmlBase

Inherits:
BasicObject
Defined in:
lib/builder/xmlbase.rb

Overview

XmlBase is a base class for building XML builders. See Builder::XmlMarkup and Builder::XmlEvents for examples.

Direct Known Subclasses

XmlMarkup

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indent = 0, initial = 0, encoding = 'utf-8') ⇒ XmlBase

Create an XML markup builder.

out

Object receiving the markup. out must respond to <<.

indent

Number of spaces used for indentation (0 implies no indentation and no line breaks).

initial

Level of initial indentation.

encoding

When encoding and $KCODE are set to ‘utf-8’ characters aren’t converted to character entities in the output stream.



27
28
29
30
31
# File 'lib/builder/xmlbase.rb', line 27

def initialize(indent=0, initial=0, encoding='utf-8')
  @indent = indent
  @level  = initial
  @encoding = encoding.downcase
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Create XML markup based on the name of the method. This method is never invoked directly, but is called for each markup method in the markup block that isn’t cached.



90
91
92
93
# File 'lib/builder/xmlbase.rb', line 90

def method_missing(sym, *args, &block)
  cache_method_call(sym) if ::Builder::XmlBase.cache_method_calls
  tag!(sym, *args, &block)
end

Class Attribute Details

.cache_method_callsObject

Returns the value of attribute cache_method_calls.



14
15
16
# File 'lib/builder/xmlbase.rb', line 14

def cache_method_calls
  @cache_method_calls
end

Instance Method Details

#<<(text) ⇒ Object

Append text to the output target without escaping any markup. May be used within the markup brackets as:

builder.p { |x| x << "<br/>HI" }   #=>  <p><br/>HI</p>

This is useful when using non-builder enabled software that generates strings. Just insert the string directly into the builder without changing the inserted markup.

It is also useful for stacking builder objects. Builders only use << to append to the target, so by supporting this method/operation builders can use other builders as their targets.



116
117
118
# File 'lib/builder/xmlbase.rb', line 116

def <<(text)
  _text(text)
end

#explicit_nil_handling?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/builder/xmlbase.rb', line 33

def explicit_nil_handling?
  @explicit_nil_handling
end

#nil?Boolean

For some reason, nil? is sent to the XmlMarkup object. If nil? is not defined and method_missing is invoked, some strange kind of recursion happens. Since nil? won’t ever be an XML tag, it is pretty safe to define it here. (Note: this is an example of cargo cult programming, cf. fishbowl.pastiche.org/2004/10/13/cargo_cult_programming).

Returns:

  • (Boolean)


126
127
128
# File 'lib/builder/xmlbase.rb', line 126

def nil?
  false
end

#tag!(sym, *args, &block) ⇒ Object

Create a tag named sym. Other than the first argument which is the tag name, the arguments are the same as the tags implemented via method_missing.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/builder/xmlbase.rb', line 40

def tag!(sym, *args, &block)
  text = nil
  attrs = nil
  sym = "#{sym}:#{args.shift}" if args.first.kind_of?(::Symbol)
  sym = sym.to_sym unless sym.class == ::Symbol
  args.each do |arg|
    case arg
    when ::Hash
      attrs ||= {}
      attrs.merge!(arg)
    when nil
      attrs ||= {}
      attrs.merge!({:nil => true}) if explicit_nil_handling?
    else
      text ||= ''.dup
      text << arg.to_s
    end
  end
  if block
    unless text.nil?
      ::Kernel::raise ::ArgumentError,
        "XmlMarkup cannot mix a text argument with a block"
    end
    _indent
    _start_tag(sym, attrs)
    _newline
    begin
      _nested_structures(block)
    ensure
      _indent
      _end_tag(sym)
      _newline
    end
  elsif text.nil?
    _indent
    _start_tag(sym, attrs, true)
    _newline
  else
    _indent
    _start_tag(sym, attrs)
    text! text
    _end_tag(sym)
    _newline
  end
  @target
end

#text!(text) ⇒ Object

Append text to the output target. Escape any markup. May be used within the markup brackets as:

builder.p { |b| b.br; b.text! "HI" }   #=>  <p><br/>HI</p>


99
100
101
# File 'lib/builder/xmlbase.rb', line 99

def text!(text)
  _text(_escape(text))
end