Class: Victor::SVGBase

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

Direct Known Subclasses

SVG

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Marshaling

#encode_with, #init_with, #marshal_dump, #marshal_load

Constructor Details

#initialize(attributes = nil, &block) ⇒ SVGBase

Returns a new instance of SVGBase.



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

def initialize(attributes = nil, &block)
  setup attributes
  @content = []
  build(&block) if block
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#css(defs = nil) ⇒ Object



73
74
75
76
77
# File 'lib/victor/svg_base.rb', line 73

def css(defs = nil)
  @css ||= {}
  @css = defs if defs
  @css
end

#glueObject

Returns the value of attribute glue.



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

def glue
  @glue
end

#svg_attributesObject (readonly)

Returns the value of attribute svg_attributes.



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

def svg_attributes
  @svg_attributes
end

#templateObject

Returns the value of attribute template.



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

def template
  @template
end

Instance Method Details

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



19
20
21
# File 'lib/victor/svg_base.rb', line 19

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

#build(&block) ⇒ Object



39
40
41
# File 'lib/victor/svg_base.rb', line 39

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

#marshalingObject



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

def marshaling
  %i[template glue svg_attributes css content]
end

#render(template: nil, glue: nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/victor/svg_base.rb', line 79

def render(template: nil, glue: nil)
  @template = template if template
  @glue = glue if glue
  css_handler = CSS.new css

  svg_template % {
    css:        css_handler,
    style:      css_handler.render,
    attributes: svg_attributes,
    content:    to_s,
  }
end

#save(filename, template: nil, glue: nil) ⇒ Object



96
97
98
99
# File 'lib/victor/svg_base.rb', line 96

def save(filename, template: nil, glue: nil)
  filename = "#{filename}.svg" unless /\..{2,4}$/.match?(filename)
  File.write filename, render(template: template, glue: glue)
end

#setup(attributes = nil) ⇒ Object



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

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

  @template = attributes[:template] || @template || :default
  @glue = attributes[:glue] || @glue || "\n"

  attributes.delete :template
  attributes.delete :glue

  @svg_attributes = Attributes.new attributes
end

#tag(name, value = nil, attributes = {}) ⇒ Object Also known as: element



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/victor/svg_base.rb', line 43

def tag(name, value = nil, attributes = {})
  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

#to_sObject



92
93
94
# File 'lib/victor/svg_base.rb', line 92

def to_s
  content.join glue
end