class SVGPlot::Tag

Tag object, represents a single SVG tag

Attributes

attributes[R]
children[R]
defaults[W]
tag[R]

Public Class Methods

new(tag, attributes = {}, &block) click to toggle source
# File lib/svgplot/tag.rb, line 12
def initialize(tag, attributes = {}, &block)
  @tag = parse_tag tag
  @attributes = parse_attributes attributes
  @children = []

  instance_exec(&block) if block
end

Public Instance Methods

append_child(child) click to toggle source
# File lib/svgplot/tag.rb, line 38
def append_child(child)
  @children.push(child)
  child.defaults = defaults
  child
end
defaults() click to toggle source
# File lib/svgplot/tag.rb, line 28
def defaults
  @defaults ||= {}
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/svgplot/tag.rb, line 78
def method_missing(method, *args, &block)
  check = parse_method_name(method)
  if check
    return parse_method_op(check[:op], check[:name], args, &block)
  else
    child_name = parse_child_name(method)
    return spawn_child(child_name, *args, &block) if child_name
  end
  super
end
path(attributes = {}, &block) click to toggle source
# File lib/svgplot/tag.rb, line 24
def path(attributes = {}, &block)
  append_child Path.new(@img, attributes, &block)
end
raw(data) click to toggle source
# File lib/svgplot/tag.rb, line 20
def raw(data)
  append_child Raw.new(@img, data)
end
respond_to?(method) click to toggle source
Calls superclass method
# File lib/svgplot/tag.rb, line 73
def respond_to?(method)
  return true if parse_method_name(method) || parse_child_name(meth)
  super
end
spawn_child(tag, *args, &block) click to toggle source
# File lib/svgplot/tag.rb, line 62
def spawn_child(tag, *args, &block)
  parameters = args.first.is_a?(Hash) ? args.unshift : {}
  if parameters.empty?
    parameters = args.last.is_a?(Hash) ? args.pop : {}
    parameters.merge! expand(tag, args)
  end
  parameters = defaults.dup.merge! parameters

  append_child ChildTag.new(@img, tag, parameters, &block)
end
to_s() click to toggle source
# File lib/svgplot/tag.rb, line 44
def to_s
  str = ''
  write(str)
  str
end
use(id, attributes = {}) click to toggle source
# File lib/svgplot/tag.rb, line 32
def use(id, attributes = {})
  id = id.attributes[:id] if id.is_a? Tag
  attributes.merge!('xlink:href' => "##{id}")
  append_child ChildTag.new(@img, 'use', attributes)
end
write(output) click to toggle source
# File lib/svgplot/tag.rb, line 50
def write(output)
  output << "<#{@tag}"
  @attributes.each { |attr, value| output << %( #{attr}="#{value}") }
  if @children.empty?
    output << '/>'
  else
    output << '>'
    @children.each { |c| c.write(output) }
    output << "</#{@tag}>"
  end
end