summaryrefslogtreecommitdiff
path: root/lib/scale/dsl.rb
blob: 99fefb46cc1dcd14b8ebe1a07dbd42b0771efe57 (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
38
39
40
module Scale
  class DSL
    def run
      DSLBuilder.new.tap do |builder|
        yield builder
      end.to_xml
    end
  end

  class DSLCommand
    def initialize(name, args, block)
      @name = name
      @args = args
      @block = block
    end

    def run(svg)
      type = Kernel.const_get("Scale::#{@name.to_s.capitalize}")
      svg.add(type.new(*@args))
    end
  end

  class DSLBuilder
    def initialize(commands = [])
      @commands = commands
    end

    def method_missing(name, *args, &block)
      @commands.push(DSLCommand.new(name, args, block))
    end

    def to_xml
      svg = SVG.new
      @commands.each do |command|
        command.run(svg)
      end
      svg.to_xml
    end
  end
end