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) ⇒ RexleBuilder

Returns a new instance of RexleBuilder.



23
24
25
26
27
28
29
30
# File 'lib/rexle-builder.rb', line 23

def initialize(obj=nil)
  
  @a = []
  @current_a = @a
  @namespace = nil
  
  self.root { buildx obj} if obj.is_a? Hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



41
42
43
44
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
# File 'lib/rexle-builder.rb', line 41

def method_missing(sym, *args)
  
  args << '' if args.last.is_a? Hash 
  value, attributes = args.reverse
  
  # reserved keywords are masked with ._ e.g. ._method_missing
  a = [sym.to_s.sub(/^\._/,'').sub(/^cdata!$/,'!['), attributes || {}, \
                                                                 value || '']

  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

Class Method Details

.build {|self.new| ... } ⇒ Object

Yields:

  • (self.new)


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

def self.build()

  yield(self.new)

end

Instance Method Details

#[](s) ⇒ Object



32
33
34
35
# File 'lib/rexle-builder.rb', line 32

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

#to_aObject



37
38
39
# File 'lib/rexle-builder.rb', line 37

def to_a
  @a.first
end