Class: SVGPlot::Tag
Overview
Tag object, represents a single SVG tag
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Expansion
#expand
Methods included from Transform
#matrix, #rotate, #scale, #skew_x, #skew_y, #translate
Constructor Details
#initialize(tag, attributes = {}, &block) ⇒ Tag
Returns a new instance of Tag.
12
13
14
15
16
17
18
|
# 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
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
78
79
80
81
82
83
84
85
86
87
|
# 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
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
9
10
11
|
# File 'lib/svgplot/tag.rb', line 9
def attributes
@attributes
end
|
#children ⇒ Object
Returns the value of attribute children.
9
10
11
|
# File 'lib/svgplot/tag.rb', line 9
def children
@children
end
|
#defaults ⇒ Object
28
29
30
|
# File 'lib/svgplot/tag.rb', line 28
def defaults
@defaults ||= {}
end
|
#tag ⇒ Object
Returns the value of attribute tag.
9
10
11
|
# File 'lib/svgplot/tag.rb', line 9
def tag
@tag
end
|
Instance Method Details
#append_child(child) ⇒ Object
38
39
40
41
42
|
# File 'lib/svgplot/tag.rb', line 38
def append_child(child)
@children.push(child)
child.defaults = defaults
child
end
|
#path(attributes = {}, &block) ⇒ Object
24
25
26
|
# File 'lib/svgplot/tag.rb', line 24
def path(attributes = {}, &block)
append_child Path.new(@img, attributes, &block)
end
|
#raw(data) ⇒ Object
20
21
22
|
# File 'lib/svgplot/tag.rb', line 20
def raw(data)
append_child Raw.new(@img, data)
end
|
#respond_to?(method) ⇒ Boolean
73
74
75
76
|
# 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) ⇒ Object
62
63
64
65
66
67
68
69
70
71
|
# 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 ⇒ Object
44
45
46
47
48
|
# File 'lib/svgplot/tag.rb', line 44
def to_s
str = ''
write(str)
str
end
|
#use(id, attributes = {}) ⇒ Object
32
33
34
35
36
|
# 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) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
|
# 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
|