class Victor::SVGBase

Attributes

content[R]
svg_attributes[R]
template[RW]

Public Class Methods

new(attributes = nil, &block) click to toggle source
# File lib/victor/svg_base.rb, line 7
def initialize(attributes = nil, &block)
  setup attributes
  @content = []
  build &block if block_given?
end

Public Instance Methods

<<(additional_content) click to toggle source
# File lib/victor/svg_base.rb, line 13
def <<(additional_content)
  content.push additional_content.to_s
end
Also aliased as: append
append(additional_content)
Alias for: <<
build(&block) click to toggle source
# File lib/victor/svg_base.rb, line 32
def build(&block)
  self.instance_eval(&block)
end
css(defs = nil) click to toggle source
# File lib/victor/svg_base.rb, line 65
def css(defs = nil)
  @css ||= {}
  @css = defs if defs
  @css
end
css=(defs) click to toggle source
# File lib/victor/svg_base.rb, line 71
def css=(defs)
  @css = defs
end
element(name, value=nil, attributes={}) { || ... } click to toggle source
# File lib/victor/svg_base.rb, line 36
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
render(template: nil) click to toggle source
# File lib/victor/svg_base.rb, line 75
def render(template: nil)
  @template = template if template
  css_handler = CSS.new css

  svg_template % {
    css: css_handler,
    style: css_handler.render,
    attributes: svg_attributes,
    content: content.join("\n")
  }
end
save(filename, template: nil) click to toggle source
# File lib/victor/svg_base.rb, line 91
def save(filename, template: nil)
  filename = "#{filename}.svg" unless filename =~ /\..{2,4}$/
  File.write filename, render(template: template)
end
setup(attributes = nil) click to toggle source
# File lib/victor/svg_base.rb, line 18
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_s() click to toggle source
# File lib/victor/svg_base.rb, line 87
def to_s
  content.join "\n"
end

Protected Instance Methods

svg_template() click to toggle source
# File lib/victor/svg_base.rb, line 98
def svg_template
  File.read template_path
end
template_path() click to toggle source
# File lib/victor/svg_base.rb, line 102
def template_path
  if template.is_a? Symbol
    File.join File.dirname(__FILE__), 'templates', "#{template}.svg"
  else
    template
  end
end