Class: SvgStack

Inherits:
Object
  • Object
show all
Defined in:
lib/svgstack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, x: 0, y: 0, width: 150, height: nil) ⇒ SvgStack

Returns a new instance of SvgStack.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/svgstack.rb', line 29

def initialize(data=nil, x: 0,  y: 0, width: 150, height: nil)
  
  
  @x, @y, @width, @height = x, y, width, height
  
  @css = "
  rect {
    fill: #f6f;
  }

  text { fill: blue}
"

  if data then
    @boxes = data       
    @doc = render()
  end


end

Instance Attribute Details

#cssObject

Returns the value of attribute css.



27
28
29
# File 'lib/svgstack.rb', line 27

def css
  @css
end

Instance Method Details

#import(s) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/svgstack.rb', line 50

def import(s)
  
  s2 = s.slice(/<\?svgstack [^>]*\?>/)

  if s2 then
    
    attributes = %w(delimiter id schema).inject({}) do |r, keyword|
      found = s2[/(?<=#{keyword}=['"])[^'"]+/]
      found ? r.merge(keyword.to_sym => found) : r
    end
    
  end
  
  @dx = Dynarex.new s.sub(/svgstack/, 'dynarex') 
  @boxes = @dx.to_h.to_a.reverse
  @doc = render()

end

#renderObject



69
70
71
72
73
74
75
76
77
# File 'lib/svgstack.rb', line 69

def render()
  
  @height ||= (@boxes.length * 50) + 50    

  @svg = Victor::SVG.new viewBox: [@x, @y, @width, @height].join(' ')
  
  build @boxes
  
end

#save(file = 'untitled') ⇒ Object



79
80
81
# File 'lib/svgstack.rb', line 79

def save(file='untitled')    
  File.write file, @doc
end

#to_svgObject



83
84
85
# File 'lib/svgstack.rb', line 83

def to_svg()
  @doc
end