Class: XML::DSL

Inherits:
Object
  • Object
show all
Includes:
Views
Defined in:
lib/capcode/render/xml.rb,
lib/capcode/render/xml.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(helper, &block) ⇒ DSL

Returns a new instance of DSL.



6
7
8
9
10
11
12
13
14
# File 'lib/capcode/render/xml.rb', line 6

def initialize( helper, &block )
  @__x_d_level = 0
  @__x_d_helper = helper
  @__x_d_helper.instance_variables.each do |ivar|
    self.instance_variable_set(ivar, @__x_d_helper.instance_variable_get(ivar))
  end
  @__x_d_builder = ""
  instance_eval(&block) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/capcode/render/xml.rb', line 92

def method_missing(sym, *args, &block)
  if @__x_d_helper.respond_to?(sym, true)
    @__x_d_helper.send(sym, *args, &block)
  elsif instance_variables.include?(ivar = "@__x_d_#{sym}")
    instance_variable_get(ivar)
  elsif !@__x_d_helper.nil? && @__x_d_helper.instance_variables.include?(ivar)
    @__x_d_helper.instance_variable_get(ivar)
  else
    tag!(sym, *args, &block)
  end
end

Instance Method Details

#_(x) ⇒ Object



20
21
22
# File 'lib/capcode/render/xml.rb', line 20

def _(x)
  @__x_d_builder << __ << x << "\n"
end

#__Object



16
17
18
# File 'lib/capcode/render/xml.rb', line 16

def __
  " "*@__x_d_level
end

#cdata(x = "", &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/capcode/render/xml.rb', line 74

def cdata( x = "", &block )
  @__x_d_builder << __ << "<![CDATA["
  if x.match( /\n/ ) or block
    @__x_d_level += 2
    @__x_d_builder << "\n" << __ << x << "\n" if x.size > 0
    instance_eval(&block) if block
    @__x_d_level -= 2
    @__x_d_builder << __ 
  else
    @__x_d_builder << x if x.size > 0
  end
  @__x_d_builder << "]]>\n"
end

#tag!(sym, *args, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
71
72
# File 'lib/capcode/render/xml.rb', line 24

def tag!(sym, *args, &block)
  tag = {
    :bra => "<",
    :ket => " />",
    :name => sym.id2name,
    :close => block_given?(),
    :attrs => "",
    :value => ""
  }
          
  args.each do |a|
    if a.class == Hash
      a.each do |k, v|
        tag[:attrs] << " #{k.to_s}='#{v}'"
      end
    elsif a.class == String
      tag[:close] = true
      tag[:value] << a << "\n"
    end
  end
  
  if tag[:name].match( /\?$/ )
    tag[:name].gsub!( /\?$/, "" )
    tag[:bra] = "<?"
    tag[:ket] = "?>"
    
    if tag[:close] == true
      raise XML::TagError, "Malformated traitment tag!"
    end
  end

  @__x_d_builder << __ << tag[:bra] << "#{tag[:name]}#{tag[:attrs]}"
  if tag[:close]
    @__x_d_builder << ">\n"
  else
    @__x_d_builder << tag[:ket] << "\n"
  end
  
  @__x_d_level += 2
  
  @__x_d_builder << __ << tag[:value] if tag[:value].size > 0
  instance_eval(&block) if block
  
  @__x_d_level -= 2
  
  if tag[:close]
    @__x_d_builder << __ << "</#{tag[:name]}>\n"
  end
end

#to_sObject



88
89
90
# File 'lib/capcode/render/xml.rb', line 88

def to_s
  @__x_d_builder
end