Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/builder/xmlbase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ module Builder
# Generic error for builder
class IllegalBlockError < RuntimeError; end

# XmlBase is a base class for building XML builders. See
# XmlBase is a base class for building XML builders. See
# Builder::XmlMarkup and Builder::XmlEvents for examples.
#
# Please note, that the XmlBase class inherits from +BasicObject+,
# not from the more common +Object+. The advantage is that there are less
# instance methods which can conflict with xml element names which
# might be used. The disadvantage is that these +Object+ instance
# methods might be expected to be present by testing tools
# or IRB or other consoles for inspection.
# Prominent missing methods are: +class+, +display+, +hash+, +inspect+,
# +methods+, +object_id+, +send+ (you can use +__send__+ though).
class XmlBase < BasicObject

class << self
Expand All @@ -16,8 +25,6 @@ class << self

# Create an XML markup builder.
#
# out :: Object receiving the markup. +out+ must respond to
# <tt><<</tt>.
# indent :: Number of spaces used for indentation (0 implies no
# indentation and no line breaks).
# initial :: Level of initial indentation.
Expand Down Expand Up @@ -117,12 +124,9 @@ def <<(text)
_text(text)
end

# 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. http://fishbowl.pastiche.org/2004/10/13/cargo_cult_programming).
# +nil?+ is an instance method not implemented on the +BasicObject+,
# but since +nil?+ won't ever be an XML tag, it is pretty safe to
# define it here.
Comment on lines +127 to +129
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest. We don't even need this nil? implementation anymore. method_missing does not require it anymore.

def nil?
false
end
Expand Down
8 changes: 4 additions & 4 deletions lib/builder/xmlmarkup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

module Builder

# Create XML markup easily. All (well, almost all) methods sent to
# an XmlMarkup object will be translated to the equivalent XML
# markup. Any method with a block will be treated as an XML markup
# Create XML markup easily. All methods (except BasicObject instance methods)
# sent to an XmlMarkup object will be translated to the equivalent XML
# markup. Any method with a block will be treated as an XML markup
# tag with nested markup in the block.
#
# Examples will demonstrate this easier than words. In the
Expand Down Expand Up @@ -57,7 +57,7 @@ module Builder
#
# xm.div { # <div>
# xm.text! "line"; xm.br # line<br/>
# xm.text! "another line"; xmbr # another line<br/>
# xm.text! "another line"; xm.br # another line<br/>
# } # </div>
#
# * The special XML characters <, >, and & are converted to &lt;,
Expand Down
8 changes: 8 additions & 0 deletions test/test_markupbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ def test_complex
assert_equal %{<body bg="#ffffff"><title style="red">T</title></body>}, @xml.target!
end

def test_object_method_names
@xml.class do
@xml.methods('x')
@xml.object_id('1')
end
assert_equal %{<class><methods>x</methods><object_id>1</object_id></class>}, @xml.target!
end

def test_funky_symbol
@xml.tag!("non-ruby-token", :id=>1) { |x| x.ok }
assert_equal %{<non-ruby-token id="1"><ok/></non-ruby-token>}, @xml.target!
Expand Down