class Victor::SVGBase

Attributes

content[R]
css[W]
glue[RW]
svg_attributes[R]
template[RW]

Public Class Methods

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

Public Instance Methods

<<(additional_content) click to toggle source
# File lib/victor/svg_base.rb, line 19
def <<(additional_content)
  content.push additional_content.to_s
end
Also aliased as: append, embed
append(additional_content)
Alias for: <<
build(&block) click to toggle source
# File lib/victor/svg_base.rb, line 39
def build(&block)
  instance_eval(&block)
end
css(defs = nil) click to toggle source
# File lib/victor/svg_base.rb, line 73
def css(defs = nil)
  @css ||= {}
  @css = defs if defs
  @css
end
element(name, value = nil, attributes = {})
Alias for: tag
embed(additional_content)
Alias for: <<
marshaling() click to toggle source
# File lib/victor/svg_base.rb, line 15
def marshaling
  %i[template glue svg_attributes css content]
end
render(template: nil, glue: nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 = {}) { || ... } click to toggle source
# 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
Also aliased as: element
to_s() click to toggle source
# File lib/victor/svg_base.rb, line 92
def to_s
  content.join glue
end

Protected Instance Methods

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