Class: Victor::SVGBase

Inherits:
Object
  • Object
show all
Defined in:
lib/victor/svg_base.rb

Direct Known Subclasses

SVG

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ SVGBase

Returns a new instance of SVGBase.



7
8
9
10
11
12
13
14
# File 'lib/victor/svg_base.rb', line 7

def initialize(attributes={})
  @template = attributes.delete(:template) || :default
  @svg_attributes = Attributes.new attributes
  svg_attributes[:width] ||= "100%"
  svg_attributes[:height] ||= "100%"
  @content = []
  @css = {}
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'lib/victor/svg_base.rb', line 5

def content
  @content
end

#cssObject

Returns the value of attribute css.



4
5
6
# File 'lib/victor/svg_base.rb', line 4

def css
  @css
end

#svg_attributesObject (readonly)

Returns the value of attribute svg_attributes.



5
6
7
# File 'lib/victor/svg_base.rb', line 5

def svg_attributes
  @svg_attributes
end

#templateObject

Returns the value of attribute template.



4
5
6
# File 'lib/victor/svg_base.rb', line 4

def template
  @template
end

Instance Method Details

#<<(additional_content) ⇒ Object Also known as: append



16
17
18
# File 'lib/victor/svg_base.rb', line 16

def <<(additional_content)
  content.push additional_content.to_s
end

#build(&block) ⇒ Object



21
22
23
# File 'lib/victor/svg_base.rb', line 21

def build(&block)
  self.instance_eval(&block)
end

#element(name, value = nil, attributes = {}, &_block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/victor/svg_base.rb', line 25

def element(name, value=nil, attributes={}, &_block)
  if value.is_a? Hash
    attributes = value
    value = nil
  end

  attributes = Attributes.new attributes

  if block_given? || value
    content.push "<#{name} #{attributes}".strip + ">"
    value ? content.push(value) : yield
    content.push "</#{name}>"
  else
    content.push "<#{name} #{attributes}/>"
  end
end

#renderObject



42
43
44
45
46
47
48
49
50
# File 'lib/victor/svg_base.rb', line 42

def render
  css_string = CSS.new css
  svg_template % {
    css: css_string,
    style: css_string.render,
    attributes: svg_attributes,
    content: content.join("\n")
  }
end

#save(filename) ⇒ Object



56
57
58
59
# File 'lib/victor/svg_base.rb', line 56

def save(filename)
  filename = "#{filename}.svg" unless filename =~ /\..{2,4}$/
  File.write filename, render
end

#to_sObject



52
53
54
# File 'lib/victor/svg_base.rb', line 52

def to_s
  content.join "\n"
end