Class: Lutaml::Xsd::Spa::Svg::Utils::SvgBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/xsd/spa/svg/utils/svg_builder.rb

Overview

Helper methods for building SVG elements

Class Method Summary collapse

Class Method Details

.circle(cx, cy, r, attributes = {}) ⇒ Object



36
37
38
# File 'lib/lutaml/xsd/spa/svg/utils/svg_builder.rb', line 36

def self.circle(cx, cy, r, attributes = {})
  element("circle", attributes.merge(cx: cx, cy: cy, r: r))
end

.element(name, attributes = {}, content = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lutaml/xsd/spa/svg/utils/svg_builder.rb', line 16

def self.element(name, attributes = {}, content = nil)
  attr_str = attributes.map do |k, v|
    "#{k}=\"#{escape_xml(v)}\""
  end.join(" ")

  if content
    "<#{name} #{attr_str}>#{content}</#{name}>"
  elsif block_given?
    "<#{name} #{attr_str}>#{yield}</#{name}>"
  else
    "<#{name} #{attr_str}/>"
  end
end

.escape_xml(text) ⇒ Object



12
13
14
# File 'lib/lutaml/xsd/spa/svg/utils/svg_builder.rb', line 12

def self.escape_xml(text)
  CGI.escape_html(text.to_s)
end

.group(attributes = {}) ⇒ Object



50
51
52
# File 'lib/lutaml/xsd/spa/svg/utils/svg_builder.rb', line 50

def self.group(attributes = {}, &)
  element("g", attributes, &)
end

.line(x1, y1, x2, y2, attributes = {}) ⇒ Object



40
41
42
43
44
# File 'lib/lutaml/xsd/spa/svg/utils/svg_builder.rb', line 40

def self.line(x1, y1, x2, y2, attributes = {})
  element("line", attributes.merge(
                    x1: x1, y1: y1, x2: x2, y2: y2,
                  ))
end

.path(d, attributes = {}) ⇒ Object



59
60
61
# File 'lib/lutaml/xsd/spa/svg/utils/svg_builder.rb', line 59

def self.path(d, attributes = {})
  element("path", attributes.merge(d: d))
end

.polygon(points, attributes = {}) ⇒ Object



54
55
56
57
# File 'lib/lutaml/xsd/spa/svg/utils/svg_builder.rb', line 54

def self.polygon(points, attributes = {})
  points_str = points.map { |p| "#{p.x},#{p.y}" }.join(" ")
  element("polygon", attributes.merge(points: points_str))
end

.rect(x, y, width, height, attributes = {}) ⇒ Object



30
31
32
33
34
# File 'lib/lutaml/xsd/spa/svg/utils/svg_builder.rb', line 30

def self.rect(x, y, width, height, attributes = {})
  element("rect", attributes.merge(
                    x: x, y: y, width: width, height: height,
                  ))
end

.text(x, y, content, attributes = {}) ⇒ Object



46
47
48
# File 'lib/lutaml/xsd/spa/svg/utils/svg_builder.rb', line 46

def self.text(x, y, content, attributes = {})
  element("text", attributes.merge(x: x, y: y), escape_xml(content))
end