Module: Locomotive::XML::ClassMethods

Defined in:
lib/locomotive/utils/xml.rb

Instance Method Summary collapse

Instance Method Details

#def_node(*mtds) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/locomotive/utils/xml.rb', line 11

def def_node(*mtds)
  mtds.each do |mtd|
    define_method(mtd) do |attrs={},&block|
      attr_list = []
      attrs.keys.each do |attr|
        attr_list << "#{attr}=#{quote(attrs[attr])}"
      end
      mtd_name = mtd.to_s
      if /^_?(?<name>[a-z]*)_?$/i =~ mtd_name.to_s
        mtd_name = name
      end
      xml = "<#{mtd_name}"
      xml += " " unless attr_list.empty?
      if block.nil?
        xml += "#{attr_list.join(" ")}/>\n"
      else
        content = block.call.to_s
        xml += "#{attr_list.join(" ")}>#{content.length < 60 ? "" : "\n"}" \
        "#{content}#{content.length < 60 ? "" : "\n"}" \
        "</#{mtd_name}>\n"
      end
    end
  end
end