File: xbuilder.rb

package info (click to toggle)
ruby-ox 2.14.22-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,500 kB
  • sloc: xml: 39,683; ansic: 9,615; ruby: 6,422; sh: 47; makefile: 2
file content (32 lines) | stat: -rw-r--r-- 737 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Contributed by Notezen

require 'ox'
module Ox
  module XBuilder
    # args = attributes and/or children in any order, multiple appearance is possible
    # @overload build(name,attributes,children)
    #   @param [String] name name of the Element
    #   @param [Hash] attributes
    #   @param [String|Element|Array] children text, child element or array of elements
    def x(name, *args)
      n = Element.new(name)

      for arg in args
        case arg
        when Hash
          arg.each { |k, v| n[k.to_s] = v }
        when Array
          arg.each { |c| n << c if c}
        else
          n << arg if arg
        end
      end

      n
    end

    def x_if(condition, *args)
      x(*args) if condition
    end
  end
end