Class: SVGen::SVG

Inherits:
Object
  • Object
show all
Includes:
Nestable
Defined in:
lib/svgen/svg.rb

Instance Method Summary collapse

Methods included from Nestable

#circle, #group, #line, #path, #rect, #text

Constructor Details

#initialize(attrs = {}, &block) ⇒ SVG

Returns a new instance of SVG.



7
8
9
10
11
# File 'lib/svgen/svg.rb', line 7

def initialize(attrs = {}, &block)
  @children = []
  @attrs = attrs
  block.call(self) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/svgen/svg.rb', line 20

def method_missing(name, *args)
  case name.to_s
  when /^(.+)=$/
    super unless value = args.first
    @attrs.has_key?($1) ? @attrs[$1] = value : @attrs[$1.to_sym] = value
  else
    @attrs[name] || @attrs[name.to_sym]
  end
end

Instance Method Details

#generateObject



13
14
15
16
17
18
# File 'lib/svgen/svg.rb', line 13

def generate
  builder = Builder::XmlMarkup.new(indent: 2)
  builder.svg(@attrs.merge({ xmlns: "http://www.w3.org/2000/svg" })) do |svg|
    @children.each { |child| child.generate(svg) }
  end
end