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 = nil, &block) ⇒ SVGBase



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

def initialize(attributes = nil, &block)
  setup attributes
  @content = []
  @css = {}
  build &block if block_given?
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



14
15
16
# File 'lib/victor/svg_base.rb', line 14

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

#build(&block) ⇒ Object



33
34
35
# File 'lib/victor/svg_base.rb', line 33

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

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/victor/svg_base.rb', line 37

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

  escape = true

  if name.to_s.end_with? '!'
    escape = false
    name = name[0..-2]
  end

  attributes = Attributes.new attributes
  empty_tag = name.to_s == '_'

  if block_given? || value
    content.push "<#{name} #{attributes}".strip + ">" unless empty_tag
    if value
      content.push(escape ? value.to_s.encode(xml: :text) : value)
    else
      yield
    end
    content.push "</#{name}>" unless empty_tag
  else      
    content.push "<#{name} #{attributes}/>"
  end
end

#renderObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/victor/svg_base.rb', line 66

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



81
82
83
84
# File 'lib/victor/svg_base.rb', line 81

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

#setup(attributes = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/victor/svg_base.rb', line 19

def setup(attributes = nil)
  attributes ||= {}
  attributes[:width] ||= "100%"
  attributes[:height] ||= "100%"

  if attributes[:template]
    @template = attributes.delete :template
  elsif !@template
    @template = :default
  end

  @svg_attributes = Attributes.new attributes
end

#to_sObject



77
78
79
# File 'lib/victor/svg_base.rb', line 77

def to_s
  content.join "\n"
end