Class: RexleBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rexle-builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj = nil, root: 'root', debug: false) ⇒ RexleBuilder

Returns a new instance of RexleBuilder.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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


  # reserved keywords are masked with ._ e.g. ._method_missing
  a ||= [sym.to_s.sub(/^(?:\._|_)/,'').sub(/^cdata!$/,'![')\
       .sub(/^comment!$/, '!-'), attributes || {}, value || '']
  if @debug then
    puts 'sym: ' + sym.inspect
    puts 'args: ' + args.inspect
    puts 'obj: ' + obj.inspect
    puts 'a: ' + a.inspect
  end

  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
  puts '@current_a, before block_given : ' + @current_a.inspect if @debug

  if block_given? then

    prev_a = @current_a
    @current_a = a

    r = yield(@current_a)
    @current_a = prev_a

    if @debug then
      puts 'r: ' + r.inspect
      puts 'r.class: ' + r.class.inspect
    end

    return r if r == @a.first

    if r.is_a? Array then
      
      # only concat if r contains raw Rexle elements.
      if r.all? {|field, attributes, value| attributes.is_a?(Hash) and \
                 value} then
        puts 'concatenating: ' + r.inspect if @debug
        @current_a.concat r
      end

    end
    puts '@current_a ' + @current_a.inspect if @debug
    #@current_a = prev_a
    return @a.first
    #return @current_a

  end

  #@a.first
  nil
end

Class Method Details

.build(debug: false) {|self.new(debug: debug)| ... } ⇒ Object

Yields:

  • (self.new(debug: debug))


17
18
19
20
21
# File 'lib/rexle-builder.rb', line 17

def self.build(debug: false)

  yield(self.new(debug: debug))

end

Instance Method Details

#[](s) ⇒ Object



43
44
45
46
# File 'lib/rexle-builder.rb', line 43

def [](s)
  @namespace = s
  self
end

#to_aObject



48
49
50
# File 'lib/rexle-builder.rb', line 48

def to_a
  @a.first
end