summaryrefslogtreecommitdiff
path: root/lib/utils.rb
blob: 1df83056443b8bd05b29ce980fb5e4d06748cf86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Virgil
  module Utils
    def self.unfold(attrs)
      ret = ""
      attrs.each do |key, value|
        if value.nil? then
          ret += " #{key.to_s}"
        else
          ret += " #{key.to_s}=\"#{value}\""
        end
      end
      ret
    end

    class Generic
      def initialize(attrs, tag = nil, &block)
        tag = self.class.name.split('::').last.downcase if tag.nil?
        @tag = "<#{tag}" +
          Utils::unfold(attrs) +
          ">" +
          self.instance_eval(&block) +
          "</#{tag}>"
      end
      def to_s
        @tag
      end
    end
    class GenericSingle < Generic
      def initialize(attrs, tag = nil)
        tag = self.class.name.split('::').last.downcase if tag.nil?
        @tag = "<#{tag}" +
          Utils::unfold(attrs) +
          ">"
      end
    end
  end
end