Class: Representative::AbstractXml
- Defined in:
- lib/representative/abstract_xml.rb
Instance Method Summary collapse
-
#element(name, *args, &block) ⇒ Object
Generate an element.
-
#empty ⇒ Object
Return a magic value that, when passed to #element as a block, forces generation of an empty element.
-
#initialize(subject = nil, options = {}) ⇒ AbstractXml
constructor
A new instance of AbstractXml.
-
#list_of(name, *args, &block) ⇒ Object
Generate a list of elements from Enumerable data.
Methods inherited from Base
#current_subject, #representing
Constructor Details
#initialize(subject = nil, options = {}) ⇒ AbstractXml
Returns a new instance of AbstractXml.
8 9 10 11 12 13 |
# File 'lib/representative/abstract_xml.rb', line 8 def initialize(subject = nil, = {}) unless .has_key?(:naming_strategy) = .merge!(:naming_strategy => :dasherize) end super(subject, ) end |
Instance Method Details
#element(name, *args, &block) ⇒ Object
Generate an element.
With two arguments, it generates an element with the specified text content.
r.element :size, 42
# => <size>42</size>
More commonly, though, the second argument is omitted, in which case the element content is assumed to be the named property of the current #subject.
r.representing my_shoe do
r.element :size
end
# => <size>9</size>
If a block is attached, nested elements can be generated. The element “value” (whether explicitly provided, or derived from the current subject) becomes the subject during evaluation of the block.
r.element :book, book do
r.title
r.
end
# => <book><title>Whatever</title><author>Whoever</author></book>
Providing a final Hash argument specifies element attributes.
r.element :size, :type => "integer"
# => <size type="integer">9</size>
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 |
# File 'lib/representative/abstract_xml.rb', line 45 def element(name, *args, &block) = @inspector.(current_subject, name) attributes = args..merge() subject_of_element = if args.empty? @inspector.get_value(current_subject, name) else args.shift end raise ArgumentError, "too many arguments" unless args.empty? representing(subject_of_element) do resolved_attributes = resolve_attributes(attributes) content_string = content_block = nil unless current_subject.nil? if block unless block == Representative::EMPTY content_block = Proc.new { block.call(current_subject) } end else content_string = current_subject.to_s end end generate_element(format_name(name), resolved_attributes, content_string, &content_block) end end |
#empty ⇒ Object
Return a magic value that, when passed to #element as a block, forces generation of an empty element.
r.element(:link, :rel => "me", :href => "http://dogbiscuit.org", &r.empty)
# => <link rel="parent" href="http://dogbiscuit.org"/>
118 119 120 |
# File 'lib/representative/abstract_xml.rb', line 118 def empty Representative::EMPTY end |
#list_of(name, *args, &block) ⇒ Object
Generate a list of elements from Enumerable data.
r.list_of :books, my_books do
r.element :title
end
# => <books type="array">
# <book><title>Sailing for old dogs</title></book>
# <book><title>On the horizon</title></book>
# <book><title>The Little Blue Book of VHS Programming</title></book>
# </books>
Like #element, the value can be explicit, but is more commonly extracted by name from the current #subject.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/representative/abstract_xml.rb', line 93 def list_of(name, *args, &block) = args. list_subject = args.empty? ? name : args.shift raise ArgumentError, "too many arguments" unless args.empty? list_attributes = [:list_attributes] || {} item_name = [:item_name] || name.to_s.singularize item_attributes = [:item_attributes] || {} items = resolve_value(list_subject) element(name, items, list_attributes.merge(:type => proc{"array"})) do items.each do |item| element(item_name, item, item_attributes, &block) end end end |