Class: TrianglePattern::SvgImage

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/triangle_pattern/svg_image.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSvgImage

Returns a new instance of SvgImage.



13
14
15
16
17
# File 'lib/triangle_pattern/svg_image.rb', line 13

def initialize
  @width      = 100
  @height     = 100
  @svg_string = ''
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



11
12
13
# File 'lib/triangle_pattern/svg_image.rb', line 11

def height
  @height
end

#widthObject

Returns the value of attribute width.



11
12
13
# File 'lib/triangle_pattern/svg_image.rb', line 11

def width
  @width
end

Class Method Details

.as_comment(str) ⇒ Object



77
78
79
# File 'lib/triangle_pattern/svg_image.rb', line 77

def self.as_comment(str)
  "<!-- #{str} -->"
end

Instance Method Details

#<<(svg) ⇒ Object



47
48
49
# File 'lib/triangle_pattern/svg_image.rb', line 47

def <<(svg)
  svg_string << svg.body
end

#<=>(other) ⇒ Object



81
82
83
# File 'lib/triangle_pattern/svg_image.rb', line 81

def <=>(other)
  to_s <=> other.to_s
end

#bodyObject



43
44
45
# File 'lib/triangle_pattern/svg_image.rb', line 43

def body
  svg_string
end

#group(elements, args = {}) ⇒ Object



55
56
57
58
59
# File 'lib/triangle_pattern/svg_image.rb', line 55

def group(elements, args = {})
  svg_string << %(<g #{write_args(args)}>)
  elements.each { |e| eval e }
  svg_string << %(</g>)
end

#include?(string) ⇒ Boolean

Returns:

  • (Boolean)


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

def include?(string)
  body.include? string
end

#path(str, args = {}) ⇒ Object



51
52
53
# File 'lib/triangle_pattern/svg_image.rb', line 51

def path(str, args = {})
  svg_string << %(<path d="#{str}" #{write_args(args)} />)
end

#svg_closerObject



35
36
37
# File 'lib/triangle_pattern/svg_image.rb', line 35

def svg_closer
  '</svg>'
end

#svg_headerObject



31
32
33
# File 'lib/triangle_pattern/svg_image.rb', line 31

def svg_header
  %(<svg xmlns="http://www.w3.org/2000/svg" width="#{@width}" height="#{@height}">)
end

#to_sObject



39
40
41
# File 'lib/triangle_pattern/svg_image.rb', line 39

def to_s
  svg_header + svg_string + svg_closer
end

#write_args(args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/triangle_pattern/svg_image.rb', line 61

def write_args(args)
  str = ''
  args.each do |key, value|
    if value.is_a?(Hash)
      str << %(#{key}=")
      value.each do |k, v|
        str << %(#{k}:#{v};)
      end
      str << %(" )
    else
      str << %(#{key}="#{value}" )
    end
  end
  str
end