class RexleBuilder

Public Class Methods

build(debug: false) { |self(debug: debug)| ... } click to toggle source
# File lib/rexle-builder.rb, line 17
def self.build(debug: false)

  yield(self.new(debug: debug))

end
new(obj=nil, root: 'root', debug: false) click to toggle source
# File lib/rexle-builder.rb, line 23
def initialize(obj=nil, root: 'root', debug: false)
  
  @debug = debug
  @a = []
  @current_a = @a
  @namespace = nil
  
  if obj.is_a? Hash then
    
    key = obj.keys.first      
    
    if obj.length > 1 or obj[key].is_a?(Array) or obj[key].is_a?(String) then
      self.send(root.to_sym) { buildx obj} 
    else
      self.send(key.to_sym) {buildx obj[key]}
    end

  end
end

Public Instance Methods

[](s) click to toggle source
# File lib/rexle-builder.rb, line 43
def [](s)
  @namespace = s
  self
end
method_missing(sym, *args) { || ... } click to toggle source
# File lib/rexle-builder.rb, line 52
def method_missing(sym, *args)
  
  puts 'args: ' + args.inspect if @debug
  
  value = args.find {|x| x.is_a? String} || ''
  attributes, obj = args.select {|x| x.is_a? Hash}
  
  # The obj is an optional Hash object used to build nested XML
  # after the current element
  
  if value =~ /^<.*>$/ then
    
    a = [
      sym.to_s, 
      attributes, 
      *Rexle.new("<root>%s</root>" % value, debug: @debug).to_a[2..-1]
    ]
    
  end
  
  if @debug then
    puts 'sym: ' + sym.inspect
    puts 'args: ' + args.inspect
  end
  
  # reserved keywords are masked with ._ e.g. ._method_missing
  a ||= [sym.to_s.sub(/^(?:\._|_)/,'').sub(/^cdata!$/,'![')\
       .sub(/^comment!$/, '!-'), attributes || {}, value || '']
  
  a.concat RexleBuilder.new(obj, debug: false).to_a[3..-1] if obj

  if @namespace then 
    a.first.prepend(@namespace + ':')
    @namespace = nil 
  end

  @current_a << a
  
  if block_given? then
    
    prev_a = @current_a
    @current_a = a
    
    r = yield()     
    
    @current_a.concat r if r.class == RexleArray
    @current_a = prev_a      
    
  end
  
  @a.first
end
to_a() click to toggle source
# File lib/rexle-builder.rb, line 48
def to_a
  @a.first
end

Private Instance Methods

buildx( h) click to toggle source

build from a Hash object

# File lib/rexle-builder.rb, line 109
def buildx( h)
  
  # the following statement prevents duplicate elements where 1 key is
  # represented by a String and the other by a symbol.
  #
  h2 = h.map {|x| [x[0].to_sym, x[1]]}.to_h
  
  h2.each_pair do |key, value|

    if value.is_a? Hash then
      
      self.send(key.to_sym) do 
        buildx value
      end
      
    elsif value.is_a? Array  
      
      self.send(key.to_sym) do
        
        if value.first.is_a? Hash then
          value.map {|x| buildx x} 
        else
          RexleArray.new value
        end
        
      end
      
    else
      
      self.send(key.to_sym,  value.to_s)
      
    end
  end

end