monetra-ruby 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +35 -0
- data/lib/monetra.rb +239 -0
- data/lib/monetra/active_support.rb +42 -0
- data/lib/monetra/active_support/binding_of_caller.rb +84 -0
- data/lib/monetra/active_support/breakpoint.rb +523 -0
- data/lib/monetra/active_support/caching_tools.rb +62 -0
- data/lib/monetra/active_support/clean_logger.rb +38 -0
- data/lib/monetra/active_support/core_ext.rb +1 -0
- data/lib/monetra/active_support/core_ext/array.rb +7 -0
- data/lib/monetra/active_support/core_ext/array/conversions.rb +72 -0
- data/lib/monetra/active_support/core_ext/array/grouping.rb +46 -0
- data/lib/monetra/active_support/core_ext/bigdecimal.rb +3 -0
- data/lib/monetra/active_support/core_ext/bigdecimal/formatting.rb +7 -0
- data/lib/monetra/active_support/core_ext/blank.rb +50 -0
- data/lib/monetra/active_support/core_ext/cgi.rb +5 -0
- data/lib/monetra/active_support/core_ext/cgi/escape_skipping_slashes.rb +14 -0
- data/lib/monetra/active_support/core_ext/class.rb +3 -0
- data/lib/monetra/active_support/core_ext/class/attribute_accessors.rb +44 -0
- data/lib/monetra/active_support/core_ext/class/inheritable_attributes.rb +115 -0
- data/lib/monetra/active_support/core_ext/class/removal.rb +24 -0
- data/lib/monetra/active_support/core_ext/date.rb +6 -0
- data/lib/monetra/active_support/core_ext/date/conversions.rb +39 -0
- data/lib/monetra/active_support/core_ext/enumerable.rb +62 -0
- data/lib/monetra/active_support/core_ext/exception.rb +33 -0
- data/lib/monetra/active_support/core_ext/hash.rb +13 -0
- data/lib/monetra/active_support/core_ext/hash/conversions.rb +148 -0
- data/lib/monetra/active_support/core_ext/hash/diff.rb +11 -0
- data/lib/monetra/active_support/core_ext/hash/indifferent_access.rb +88 -0
- data/lib/monetra/active_support/core_ext/hash/keys.rb +53 -0
- data/lib/monetra/active_support/core_ext/hash/reverse_merge.rb +25 -0
- data/lib/monetra/active_support/core_ext/integer.rb +7 -0
- data/lib/monetra/active_support/core_ext/integer/even_odd.rb +24 -0
- data/lib/monetra/active_support/core_ext/integer/inflections.rb +15 -0
- data/lib/monetra/active_support/core_ext/kernel.rb +4 -0
- data/lib/monetra/active_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/monetra/active_support/core_ext/kernel/daemonizing.rb +15 -0
- data/lib/monetra/active_support/core_ext/kernel/reporting.rb +51 -0
- data/lib/monetra/active_support/core_ext/kernel/requires.rb +24 -0
- data/lib/monetra/active_support/core_ext/load_error.rb +38 -0
- data/lib/monetra/active_support/core_ext/logger.rb +16 -0
- data/lib/monetra/active_support/core_ext/module.rb +7 -0
- data/lib/monetra/active_support/core_ext/module/aliasing.rb +57 -0
- data/lib/monetra/active_support/core_ext/module/attr_internal.rb +31 -0
- data/lib/monetra/active_support/core_ext/module/attribute_accessors.rb +44 -0
- data/lib/monetra/active_support/core_ext/module/delegation.rb +41 -0
- data/lib/monetra/active_support/core_ext/module/inclusion.rb +11 -0
- data/lib/monetra/active_support/core_ext/module/introspection.rb +21 -0
- data/lib/monetra/active_support/core_ext/module/loading.rb +13 -0
- data/lib/monetra/active_support/core_ext/name_error.rb +20 -0
- data/lib/monetra/active_support/core_ext/numeric.rb +7 -0
- data/lib/monetra/active_support/core_ext/numeric/bytes.rb +44 -0
- data/lib/monetra/active_support/core_ext/numeric/time.rb +72 -0
- data/lib/monetra/active_support/core_ext/object.rb +2 -0
- data/lib/monetra/active_support/core_ext/object/extending.rb +47 -0
- data/lib/monetra/active_support/core_ext/object/misc.rb +34 -0
- data/lib/monetra/active_support/core_ext/pathname.rb +7 -0
- data/lib/monetra/active_support/core_ext/pathname/clean_within.rb +14 -0
- data/lib/monetra/active_support/core_ext/proc.rb +12 -0
- data/lib/monetra/active_support/core_ext/range.rb +5 -0
- data/lib/monetra/active_support/core_ext/range/conversions.rb +21 -0
- data/lib/monetra/active_support/core_ext/string.rb +13 -0
- data/lib/monetra/active_support/core_ext/string/access.rb +58 -0
- data/lib/monetra/active_support/core_ext/string/conversions.rb +19 -0
- data/lib/monetra/active_support/core_ext/string/inflections.rb +153 -0
- data/lib/monetra/active_support/core_ext/string/iterators.rb +17 -0
- data/lib/monetra/active_support/core_ext/string/starts_ends_with.rb +20 -0
- data/lib/monetra/active_support/core_ext/symbol.rb +12 -0
- data/lib/monetra/active_support/core_ext/time.rb +7 -0
- data/lib/monetra/active_support/core_ext/time/calculations.rb +188 -0
- data/lib/monetra/active_support/core_ext/time/conversions.rb +36 -0
- data/lib/monetra/active_support/dependencies.rb +187 -0
- data/lib/monetra/active_support/deprecation.rb +106 -0
- data/lib/monetra/active_support/inflections.rb +53 -0
- data/lib/monetra/active_support/inflector.rb +179 -0
- data/lib/monetra/active_support/json.rb +37 -0
- data/lib/monetra/active_support/json/encoders.rb +25 -0
- data/lib/monetra/active_support/json/encoders/core.rb +65 -0
- data/lib/monetra/active_support/option_merger.rb +25 -0
- data/lib/monetra/active_support/ordered_options.rb +50 -0
- data/lib/monetra/active_support/reloadable.rb +30 -0
- data/lib/monetra/active_support/values/time_zone.rb +180 -0
- data/lib/monetra/active_support/vendor/builder.rb +13 -0
- data/lib/monetra/active_support/vendor/builder/blankslate.rb +63 -0
- data/lib/monetra/active_support/vendor/builder/xchar.rb +112 -0
- data/lib/monetra/active_support/vendor/builder/xmlbase.rb +145 -0
- data/lib/monetra/active_support/vendor/builder/xmlevents.rb +63 -0
- data/lib/monetra/active_support/vendor/builder/xmlmarkup.rb +328 -0
- data/lib/monetra/active_support/vendor/flexmock.rb +84 -0
- data/lib/monetra/active_support/vendor/xml_simple.rb +1019 -0
- data/lib/monetra/active_support/version.rb +9 -0
- data/lib/monetra/active_support/whiny_nil.rb +38 -0
- data/test/test.rb +21 -0
- metadata +167 -0
@@ -0,0 +1,145 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'builder/blankslate'
|
4
|
+
|
5
|
+
module Builder
|
6
|
+
|
7
|
+
# Generic error for builder
|
8
|
+
class IllegalBlockError < RuntimeError #:nodoc:
|
9
|
+
end
|
10
|
+
|
11
|
+
# XmlBase is a base class for building XML builders. See
|
12
|
+
# Builder::XmlMarkup and Builder::XmlEvents for examples.
|
13
|
+
class XmlBase < BlankSlate #:nodoc:
|
14
|
+
|
15
|
+
# Create an XML markup builder.
|
16
|
+
#
|
17
|
+
# out:: Object receiving the markup. +out+ must respond to
|
18
|
+
# <tt><<</tt>.
|
19
|
+
# indent:: Number of spaces used for indentation (0 implies no
|
20
|
+
# indentation and no line breaks).
|
21
|
+
# initial:: Level of initial indentation.
|
22
|
+
#
|
23
|
+
def initialize(indent=0, initial=0)
|
24
|
+
@indent = indent
|
25
|
+
@level = initial
|
26
|
+
end
|
27
|
+
|
28
|
+
# Create a tag named +sym+. Other than the first argument which
|
29
|
+
# is the tag name, the arguements are the same as the tags
|
30
|
+
# implemented via <tt>method_missing</tt>.
|
31
|
+
def tag!(sym, *args, &block)
|
32
|
+
self.__send__(sym, *args, &block)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Create XML markup based on the name of the method. This method
|
36
|
+
# is never invoked directly, but is called for each markup method
|
37
|
+
# in the markup block.
|
38
|
+
def method_missing(sym, *args, &block)
|
39
|
+
text = nil
|
40
|
+
attrs = nil
|
41
|
+
sym = "#{sym}:#{args.shift}" if args.first.kind_of?(Symbol)
|
42
|
+
args.each do |arg|
|
43
|
+
case arg
|
44
|
+
when Hash
|
45
|
+
attrs ||= {}
|
46
|
+
attrs.merge!(arg)
|
47
|
+
else
|
48
|
+
text ||= ''
|
49
|
+
text << arg.to_s
|
50
|
+
end
|
51
|
+
end
|
52
|
+
if block
|
53
|
+
unless text.nil?
|
54
|
+
raise ArgumentError, "XmlMarkup cannot mix a text argument with a block"
|
55
|
+
end
|
56
|
+
_capture_outer_self(block) if @self.nil?
|
57
|
+
_indent
|
58
|
+
_start_tag(sym, attrs)
|
59
|
+
_newline
|
60
|
+
_nested_structures(block)
|
61
|
+
_indent
|
62
|
+
_end_tag(sym)
|
63
|
+
_newline
|
64
|
+
elsif text.nil?
|
65
|
+
_indent
|
66
|
+
_start_tag(sym, attrs, true)
|
67
|
+
_newline
|
68
|
+
else
|
69
|
+
_indent
|
70
|
+
_start_tag(sym, attrs)
|
71
|
+
text! text
|
72
|
+
_end_tag(sym)
|
73
|
+
_newline
|
74
|
+
end
|
75
|
+
@target
|
76
|
+
end
|
77
|
+
|
78
|
+
# Append text to the output target. Escape any markup. May be
|
79
|
+
# used within the markup brakets as:
|
80
|
+
#
|
81
|
+
# builder.p { |b| b.br; b.text! "HI" } #=> <p><br/>HI</p>
|
82
|
+
def text!(text)
|
83
|
+
_text(_escape(text))
|
84
|
+
end
|
85
|
+
|
86
|
+
# Append text to the output target without escaping any markup.
|
87
|
+
# May be used within the markup brakets as:
|
88
|
+
#
|
89
|
+
# builder.p { |x| x << "<br/>HI" } #=> <p><br/>HI</p>
|
90
|
+
#
|
91
|
+
# This is useful when using non-builder enabled software that
|
92
|
+
# generates strings. Just insert the string directly into the
|
93
|
+
# builder without changing the inserted markup.
|
94
|
+
#
|
95
|
+
# It is also useful for stacking builder objects. Builders only
|
96
|
+
# use <tt><<</tt> to append to the target, so by supporting this
|
97
|
+
# method/operation builders can use other builders as their
|
98
|
+
# targets.
|
99
|
+
def <<(text)
|
100
|
+
_text(text)
|
101
|
+
end
|
102
|
+
|
103
|
+
# For some reason, nil? is sent to the XmlMarkup object. If nil?
|
104
|
+
# is not defined and method_missing is invoked, some strange kind
|
105
|
+
# of recursion happens. Since nil? won't ever be an XML tag, it
|
106
|
+
# is pretty safe to define it here. (Note: this is an example of
|
107
|
+
# cargo cult programming,
|
108
|
+
# cf. http://fishbowl.pastiche.org/2004/10/13/cargo_cult_programming).
|
109
|
+
def nil?
|
110
|
+
false
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
require 'builder/xchar'
|
116
|
+
def _escape(text)
|
117
|
+
text.to_xs
|
118
|
+
end
|
119
|
+
|
120
|
+
def _escape_quote(text)
|
121
|
+
_escape(text).gsub(%r{"}, '"') # " WART
|
122
|
+
end
|
123
|
+
|
124
|
+
def _capture_outer_self(block)
|
125
|
+
@self = eval("self", block)
|
126
|
+
end
|
127
|
+
|
128
|
+
def _newline
|
129
|
+
return if @indent == 0
|
130
|
+
text! "\n"
|
131
|
+
end
|
132
|
+
|
133
|
+
def _indent
|
134
|
+
return if @indent == 0 || @level == 0
|
135
|
+
text!(" " * (@level * @indent))
|
136
|
+
end
|
137
|
+
|
138
|
+
def _nested_structures(block)
|
139
|
+
@level += 1
|
140
|
+
block.call(self)
|
141
|
+
ensure
|
142
|
+
@level -= 1
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright 2004 by Jim Weirich (jim@weirichhouse.org).
|
5
|
+
# All rights reserved.
|
6
|
+
|
7
|
+
# Permission is granted for use, copying, modification, distribution,
|
8
|
+
# and distribution of modified versions of this work as long as the
|
9
|
+
# above copyright notice is included.
|
10
|
+
#++
|
11
|
+
|
12
|
+
require 'builder/xmlmarkup'
|
13
|
+
|
14
|
+
module Builder
|
15
|
+
|
16
|
+
# Create a series of SAX-like XML events (e.g. start_tag, end_tag)
|
17
|
+
# from the markup code. XmlEvent objects are used in a way similar
|
18
|
+
# to XmlMarkup objects, except that a series of events are generated
|
19
|
+
# and passed to a handler rather than generating character-based
|
20
|
+
# markup.
|
21
|
+
#
|
22
|
+
# Usage:
|
23
|
+
# xe = Builder::XmlEvents.new(hander)
|
24
|
+
# xe.title("HI") # Sends start_tag/end_tag/text messages to the handler.
|
25
|
+
#
|
26
|
+
# Indentation may also be selected by providing value for the
|
27
|
+
# indentation size and initial indentation level.
|
28
|
+
#
|
29
|
+
# xe = Builder::XmlEvents.new(handler, indent_size, initial_indent_level)
|
30
|
+
#
|
31
|
+
# == XML Event Handler
|
32
|
+
#
|
33
|
+
# The handler object must expect the following events.
|
34
|
+
#
|
35
|
+
# [<tt>start_tag(tag, attrs)</tt>]
|
36
|
+
# Announces that a new tag has been found. +tag+ is the name of
|
37
|
+
# the tag and +attrs+ is a hash of attributes for the tag.
|
38
|
+
#
|
39
|
+
# [<tt>end_tag(tag)</tt>]
|
40
|
+
# Announces that an end tag for +tag+ has been found.
|
41
|
+
#
|
42
|
+
# [<tt>text(text)</tt>]
|
43
|
+
# Announces that a string of characters (+text+) has been found.
|
44
|
+
# A series of characters may be broken up into more than one
|
45
|
+
# +text+ call, so the client cannot assume that a single
|
46
|
+
# callback contains all the text data.
|
47
|
+
#
|
48
|
+
class XmlEvents < XmlMarkup #:nodoc:
|
49
|
+
def text!(text)
|
50
|
+
@target.text(text)
|
51
|
+
end
|
52
|
+
|
53
|
+
def _start_tag(sym, attrs, end_too=false)
|
54
|
+
@target.start_tag(sym, attrs)
|
55
|
+
_end_tag(sym) if end_too
|
56
|
+
end
|
57
|
+
|
58
|
+
def _end_tag(sym)
|
59
|
+
@target.end_tag(sym)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,328 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#--
|
3
|
+
# Copyright 2004, 2005 by Jim Weirich (jim@weirichhouse.org).
|
4
|
+
# All rights reserved.
|
5
|
+
|
6
|
+
# Permission is granted for use, copying, modification, distribution,
|
7
|
+
# and distribution of modified versions of this work as long as the
|
8
|
+
# above copyright notice is included.
|
9
|
+
#++
|
10
|
+
|
11
|
+
# Provide a flexible and easy to use Builder for creating XML markup.
|
12
|
+
# See XmlBuilder for usage details.
|
13
|
+
|
14
|
+
require 'builder/xmlbase'
|
15
|
+
|
16
|
+
module Builder
|
17
|
+
|
18
|
+
# Create XML markup easily. All (well, almost all) methods sent to
|
19
|
+
# an XmlMarkup object will be translated to the equivalent XML
|
20
|
+
# markup. Any method with a block will be treated as an XML markup
|
21
|
+
# tag with nested markup in the block.
|
22
|
+
#
|
23
|
+
# Examples will demonstrate this easier than words. In the
|
24
|
+
# following, +xm+ is an +XmlMarkup+ object.
|
25
|
+
#
|
26
|
+
# xm.em("emphasized") # => <em>emphasized</em>
|
27
|
+
# xm.em { xmm.b("emp & bold") } # => <em><b>emph & bold</b></em>
|
28
|
+
# xm.a("A Link", "href"=>"http://onestepback.org")
|
29
|
+
# # => <a href="http://onestepback.org">A Link</a>
|
30
|
+
# xm.div { br } # => <div><br/></div>
|
31
|
+
# xm.target("name"=>"compile", "option"=>"fast")
|
32
|
+
# # => <target option="fast" name="compile"\>
|
33
|
+
# # NOTE: order of attributes is not specified.
|
34
|
+
#
|
35
|
+
# xm.instruct! # <?xml version="1.0" encoding="UTF-8"?>
|
36
|
+
# xm.html { # <html>
|
37
|
+
# xm.head { # <head>
|
38
|
+
# xm.title("History") # <title>History</title>
|
39
|
+
# } # </head>
|
40
|
+
# xm.body { # <body>
|
41
|
+
# xm.comment! "HI" # <!-- HI -->
|
42
|
+
# xm.h1("Header") # <h1>Header</h1>
|
43
|
+
# xm.p("paragraph") # <p>paragraph</p>
|
44
|
+
# } # </body>
|
45
|
+
# } # </html>
|
46
|
+
#
|
47
|
+
# == Notes:
|
48
|
+
#
|
49
|
+
# * The order that attributes are inserted in markup tags is
|
50
|
+
# undefined.
|
51
|
+
#
|
52
|
+
# * Sometimes you wish to insert text without enclosing tags. Use
|
53
|
+
# the <tt>text!</tt> method to accomplish this.
|
54
|
+
#
|
55
|
+
# Example:
|
56
|
+
#
|
57
|
+
# xm.div { # <div>
|
58
|
+
# xm.text! "line"; xm.br # line<br/>
|
59
|
+
# xm.text! "another line"; xmbr # another line<br/>
|
60
|
+
# } # </div>
|
61
|
+
#
|
62
|
+
# * The special XML characters <, >, and & are converted to <,
|
63
|
+
# > and & automatically. Use the <tt><<</tt> operation to
|
64
|
+
# insert text without modification.
|
65
|
+
#
|
66
|
+
# * Sometimes tags use special characters not allowed in ruby
|
67
|
+
# identifiers. Use the <tt>tag!</tt> method to handle these
|
68
|
+
# cases.
|
69
|
+
#
|
70
|
+
# Example:
|
71
|
+
#
|
72
|
+
# xml.tag!("SOAP:Envelope") { ... }
|
73
|
+
#
|
74
|
+
# will produce ...
|
75
|
+
#
|
76
|
+
# <SOAP:Envelope> ... </SOAP:Envelope>"
|
77
|
+
#
|
78
|
+
# <tt>tag!</tt> will also take text and attribute arguments (after
|
79
|
+
# the tag name) like normal markup methods. (But see the next
|
80
|
+
# bullet item for a better way to handle XML namespaces).
|
81
|
+
#
|
82
|
+
# * Direct support for XML namespaces is now available. If the
|
83
|
+
# first argument to a tag call is a symbol, it will be joined to
|
84
|
+
# the tag to produce a namespace:tag combination. It is easier to
|
85
|
+
# show this than describe it.
|
86
|
+
#
|
87
|
+
# xml.SOAP :Envelope do ... end
|
88
|
+
#
|
89
|
+
# Just put a space before the colon in a namespace to produce the
|
90
|
+
# right form for builder (e.g. "<tt>SOAP:Envelope</tt>" =>
|
91
|
+
# "<tt>xml.SOAP :Envelope</tt>")
|
92
|
+
#
|
93
|
+
# * XmlMarkup builds the markup in any object (called a _target_)
|
94
|
+
# that accepts the <tt><<</tt> method. If no target is given,
|
95
|
+
# then XmlMarkup defaults to a string target.
|
96
|
+
#
|
97
|
+
# Examples:
|
98
|
+
#
|
99
|
+
# xm = Builder::XmlMarkup.new
|
100
|
+
# result = xm.title("yada")
|
101
|
+
# # result is a string containing the markup.
|
102
|
+
#
|
103
|
+
# buffer = ""
|
104
|
+
# xm = Builder::XmlMarkup.new(buffer)
|
105
|
+
# # The markup is appended to buffer (using <<)
|
106
|
+
#
|
107
|
+
# xm = Builder::XmlMarkup.new(STDOUT)
|
108
|
+
# # The markup is written to STDOUT (using <<)
|
109
|
+
#
|
110
|
+
# xm = Builder::XmlMarkup.new
|
111
|
+
# x2 = Builder::XmlMarkup.new(:target=>xm)
|
112
|
+
# # Markup written to +x2+ will be send to +xm+.
|
113
|
+
#
|
114
|
+
# * Indentation is enabled by providing the number of spaces to
|
115
|
+
# indent for each level as a second argument to XmlBuilder.new.
|
116
|
+
# Initial indentation may be specified using a third parameter.
|
117
|
+
#
|
118
|
+
# Example:
|
119
|
+
#
|
120
|
+
# xm = Builder.new(:ident=>2)
|
121
|
+
# # xm will produce nicely formatted and indented XML.
|
122
|
+
#
|
123
|
+
# xm = Builder.new(:indent=>2, :margin=>4)
|
124
|
+
# # xm will produce nicely formatted and indented XML with 2
|
125
|
+
# # spaces per indent and an over all indentation level of 4.
|
126
|
+
#
|
127
|
+
# builder = Builder::XmlMarkup.new(:target=>$stdout, :indent=>2)
|
128
|
+
# builder.name { |b| b.first("Jim"); b.last("Weirich) }
|
129
|
+
# # prints:
|
130
|
+
# # <name>
|
131
|
+
# # <first>Jim</first>
|
132
|
+
# # <last>Weirich</last>
|
133
|
+
# # </name>
|
134
|
+
#
|
135
|
+
# * The instance_eval implementation which forces self to refer to
|
136
|
+
# the message receiver as self is now obsolete. We now use normal
|
137
|
+
# block calls to execute the markup block. This means that all
|
138
|
+
# markup methods must now be explicitly send to the xml builder.
|
139
|
+
# For instance, instead of
|
140
|
+
#
|
141
|
+
# xml.div { strong("text") }
|
142
|
+
#
|
143
|
+
# you need to write:
|
144
|
+
#
|
145
|
+
# xml.div { xml.strong("text") }
|
146
|
+
#
|
147
|
+
# Although more verbose, the subtle change in semantics within the
|
148
|
+
# block was found to be prone to error. To make this change a
|
149
|
+
# little less cumbersome, the markup block now gets the markup
|
150
|
+
# object sent as an argument, allowing you to use a shorter alias
|
151
|
+
# within the block.
|
152
|
+
#
|
153
|
+
# For example:
|
154
|
+
#
|
155
|
+
# xml_builder = Builder::XmlMarkup.new
|
156
|
+
# xml_builder.div { |xml|
|
157
|
+
# xml.stong("text")
|
158
|
+
# }
|
159
|
+
#
|
160
|
+
class XmlMarkup < XmlBase
|
161
|
+
|
162
|
+
# Create an XML markup builder. Parameters are specified by an
|
163
|
+
# option hash.
|
164
|
+
#
|
165
|
+
# :target=><em>target_object</em>::
|
166
|
+
# Object receiving the markup. +out+ must respond to the
|
167
|
+
# <tt><<</tt> operator. The default is a plain string target.
|
168
|
+
#
|
169
|
+
# :indent=><em>indentation</em>::
|
170
|
+
# Number of spaces used for indentation. The default is no
|
171
|
+
# indentation and no line breaks.
|
172
|
+
#
|
173
|
+
# :margin=><em>initial_indentation_level</em>::
|
174
|
+
# Amount of initial indentation (specified in levels, not
|
175
|
+
# spaces).
|
176
|
+
#
|
177
|
+
# :escape_attrs=><b>OBSOLETE</em>::
|
178
|
+
# The :escape_attrs option is no longer supported by builder
|
179
|
+
# (and will be quietly ignored). String attribute values are
|
180
|
+
# now automatically escaped. If you need unescaped attribute
|
181
|
+
# values (perhaps you are using entities in the attribute
|
182
|
+
# values), then give the value as a Symbol. This allows much
|
183
|
+
# finer control over escaping attribute values.
|
184
|
+
#
|
185
|
+
def initialize(options={})
|
186
|
+
indent = options[:indent] || 0
|
187
|
+
margin = options[:margin] || 0
|
188
|
+
super(indent, margin)
|
189
|
+
@target = options[:target] || ""
|
190
|
+
end
|
191
|
+
|
192
|
+
# Return the target of the builder.
|
193
|
+
def target!
|
194
|
+
@target
|
195
|
+
end
|
196
|
+
|
197
|
+
def comment!(comment_text)
|
198
|
+
_ensure_no_block block_given?
|
199
|
+
_special("<!-- ", " -->", comment_text, nil)
|
200
|
+
end
|
201
|
+
|
202
|
+
# Insert an XML declaration into the XML markup.
|
203
|
+
#
|
204
|
+
# For example:
|
205
|
+
#
|
206
|
+
# xml.declare! :ELEMENT, :blah, "yada"
|
207
|
+
# # => <!ELEMENT blah "yada">
|
208
|
+
def declare!(inst, *args, &block)
|
209
|
+
_indent
|
210
|
+
@target << "<!#{inst}"
|
211
|
+
args.each do |arg|
|
212
|
+
case arg
|
213
|
+
when String
|
214
|
+
@target << %{ "#{arg}"}
|
215
|
+
when Symbol
|
216
|
+
@target << " #{arg}"
|
217
|
+
end
|
218
|
+
end
|
219
|
+
if block_given?
|
220
|
+
@target << " ["
|
221
|
+
_newline
|
222
|
+
_nested_structures(block)
|
223
|
+
@target << "]"
|
224
|
+
end
|
225
|
+
@target << ">"
|
226
|
+
_newline
|
227
|
+
end
|
228
|
+
|
229
|
+
# Insert a processing instruction into the XML markup. E.g.
|
230
|
+
#
|
231
|
+
# For example:
|
232
|
+
#
|
233
|
+
# xml.instruct!
|
234
|
+
# #=> <?xml version="1.0" encoding="UTF-8"?>
|
235
|
+
# xml.instruct! :aaa, :bbb=>"ccc"
|
236
|
+
# #=> <?aaa bbb="ccc"?>
|
237
|
+
#
|
238
|
+
def instruct!(directive_tag=:xml, attrs={})
|
239
|
+
_ensure_no_block block_given?
|
240
|
+
if directive_tag == :xml
|
241
|
+
a = { :version=>"1.0", :encoding=>"UTF-8" }
|
242
|
+
attrs = a.merge attrs
|
243
|
+
end
|
244
|
+
_special(
|
245
|
+
"<?#{directive_tag}",
|
246
|
+
"?>",
|
247
|
+
nil,
|
248
|
+
attrs,
|
249
|
+
[:version, :encoding, :standalone])
|
250
|
+
end
|
251
|
+
|
252
|
+
# Insert a CDATA section into the XML markup.
|
253
|
+
#
|
254
|
+
# For example:
|
255
|
+
#
|
256
|
+
# xml.cdata!("text to be included in cdata")
|
257
|
+
# #=> <![CDATA[text to be included in cdata]]>
|
258
|
+
#
|
259
|
+
def cdata!(text)
|
260
|
+
_ensure_no_block block_given?
|
261
|
+
_special("<![CDATA[", "]]>", text, nil)
|
262
|
+
end
|
263
|
+
|
264
|
+
private
|
265
|
+
|
266
|
+
# NOTE: All private methods of a builder object are prefixed when
|
267
|
+
# a "_" character to avoid possible conflict with XML tag names.
|
268
|
+
|
269
|
+
# Insert text directly in to the builder's target.
|
270
|
+
def _text(text)
|
271
|
+
@target << text
|
272
|
+
end
|
273
|
+
|
274
|
+
# Insert special instruction.
|
275
|
+
def _special(open, close, data=nil, attrs=nil, order=[])
|
276
|
+
_indent
|
277
|
+
@target << open
|
278
|
+
@target << data if data
|
279
|
+
_insert_attributes(attrs, order) if attrs
|
280
|
+
@target << close
|
281
|
+
_newline
|
282
|
+
end
|
283
|
+
|
284
|
+
# Start an XML tag. If <tt>end_too</tt> is true, then the start
|
285
|
+
# tag is also the end tag (e.g. <br/>
|
286
|
+
def _start_tag(sym, attrs, end_too=false)
|
287
|
+
@target << "<#{sym}"
|
288
|
+
_insert_attributes(attrs)
|
289
|
+
@target << "/" if end_too
|
290
|
+
@target << ">"
|
291
|
+
end
|
292
|
+
|
293
|
+
# Insert an ending tag.
|
294
|
+
def _end_tag(sym)
|
295
|
+
@target << "</#{sym}>"
|
296
|
+
end
|
297
|
+
|
298
|
+
# Insert the attributes (given in the hash).
|
299
|
+
def _insert_attributes(attrs, order=[])
|
300
|
+
return if attrs.nil?
|
301
|
+
order.each do |k|
|
302
|
+
v = attrs[k]
|
303
|
+
@target << %{ #{k}="#{_attr_value(v)}"} if v
|
304
|
+
end
|
305
|
+
attrs.each do |k, v|
|
306
|
+
@target << %{ #{k}="#{_attr_value(v)}"} unless order.member?(k)
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
def _attr_value(value)
|
311
|
+
case value
|
312
|
+
when Symbol
|
313
|
+
value.to_s
|
314
|
+
else
|
315
|
+
_escape_quote(value.to_s)
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
def _ensure_no_block(got_block)
|
320
|
+
if got_block
|
321
|
+
fail IllegalBlockError,
|
322
|
+
"Blocks are not allowed on XML instructions"
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
end
|
327
|
+
|
328
|
+
end
|