Class: Victor::SVGBase
- Inherits:
-
Object
- Object
- Victor::SVGBase
- Defined in:
- lib/victor/svg_base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#svg_attributes ⇒ Object
readonly
Returns the value of attribute svg_attributes.
-
#template ⇒ Object
Returns the value of attribute template.
Instance Method Summary collapse
- #<<(additional_content) ⇒ Object (also: #append)
- #build(&block) ⇒ Object
- #css(defs = nil) ⇒ Object
- #css=(defs) ⇒ Object
- #element(name, value = nil, attributes = {}, &_block) ⇒ Object
-
#initialize(attributes = nil, &block) ⇒ SVGBase
constructor
A new instance of SVGBase.
- #render(template: nil) ⇒ Object
- #save(filename, template: nil) ⇒ Object
- #setup(attributes = nil) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(attributes = nil, &block) ⇒ SVGBase
Returns a new instance of SVGBase.
7 8 9 10 11 |
# File 'lib/victor/svg_base.rb', line 7 def initialize(attributes = nil, &block) setup attributes @content = [] build &block if block_given? end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
5 6 7 |
# File 'lib/victor/svg_base.rb', line 5 def content @content end |
#svg_attributes ⇒ Object (readonly)
Returns the value of attribute svg_attributes.
5 6 7 |
# File 'lib/victor/svg_base.rb', line 5 def svg_attributes @svg_attributes end |
#template ⇒ Object
Returns the value of attribute template.
4 5 6 |
# File 'lib/victor/svg_base.rb', line 4 def template @template end |
Instance Method Details
#<<(additional_content) ⇒ Object Also known as: append
13 14 15 |
# File 'lib/victor/svg_base.rb', line 13 def <<(additional_content) content.push additional_content.to_s end |
#build(&block) ⇒ Object
32 33 34 |
# File 'lib/victor/svg_base.rb', line 32 def build(&block) self.instance_eval(&block) end |
#css(defs = nil) ⇒ Object
65 66 67 68 69 |
# File 'lib/victor/svg_base.rb', line 65 def css(defs = nil) @css ||= {} @css = defs if defs @css end |
#css=(defs) ⇒ Object
71 72 73 |
# File 'lib/victor/svg_base.rb', line 71 def css=(defs) @css = defs end |
#element(name, value = nil, attributes = {}, &_block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/victor/svg_base.rb', line 36 def element(name, value=nil, attributes={}, &_block) if value.is_a? Hash attributes = value value = nil end escape = true if name.to_s.end_with? '!' escape = false name = name[0..-2] end attributes = Attributes.new attributes empty_tag = name.to_s == '_' if block_given? || value content.push "<#{name} #{attributes}".strip + ">" unless empty_tag if value content.push(escape ? value.to_s.encode(xml: :text) : value) else yield end content.push "</#{name}>" unless empty_tag else content.push "<#{name} #{attributes}/>" end end |
#render(template: nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/victor/svg_base.rb', line 75 def render(template: nil) @template = template if template css_handler = CSS.new css svg_template % { css: css_handler, style: css_handler.render, attributes: svg_attributes, content: content.join("\n") } end |
#save(filename, template: nil) ⇒ Object
91 92 93 94 |
# File 'lib/victor/svg_base.rb', line 91 def save(filename, template: nil) filename = "#{filename}.svg" unless filename =~ /\..{2,4}$/ File.write filename, render(template: template) end |
#setup(attributes = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/victor/svg_base.rb', line 18 def setup(attributes = nil) attributes ||= {} attributes[:width] ||= "100%" attributes[:height] ||= "100%" if attributes[:template] @template = attributes.delete :template elsif !@template @template = :default end @svg_attributes = Attributes.new attributes end |
#to_s ⇒ Object
87 88 89 |
# File 'lib/victor/svg_base.rb', line 87 def to_s content.join "\n" end |