Class: Esvg::Symbol
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#defs ⇒ Object
readonly
Returns the value of attribute defs.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#mtime ⇒ Object
readonly
Returns the value of attribute mtime.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#optimized ⇒ Object
readonly
Returns the value of attribute optimized.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #attr ⇒ Object
- #changed? ⇒ Boolean
- #data ⇒ Object
- #height ⇒ Object
-
#initialize(path, config = {}) ⇒ Symbol
constructor
A new instance of Symbol.
- #optimize ⇒ Object
- #read ⇒ Object
- #svgo? ⇒ Boolean
- #use(options = {}) ⇒ Object
- #use_tag(options = {}) ⇒ Object
- #width ⇒ Object
Methods included from Utils
#attributes, #compress, #dasherize, #sort, #sub_path
Constructor Details
#initialize(path, config = {}) ⇒ Symbol
Returns a new instance of Symbol.
9 10 11 12 13 14 15 |
# File 'lib/esvg/symbol.rb', line 9 def initialize(path, config={}) @config = config @path = path @last_checked = 0 load_data read end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
5 6 7 |
# File 'lib/esvg/symbol.rb', line 5 def content @content end |
#defs ⇒ Object (readonly)
Returns the value of attribute defs.
5 6 7 |
# File 'lib/esvg/symbol.rb', line 5 def defs @defs end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
5 6 7 |
# File 'lib/esvg/symbol.rb', line 5 def group @group end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/esvg/symbol.rb', line 5 def id @id end |
#mtime ⇒ Object (readonly)
Returns the value of attribute mtime.
5 6 7 |
# File 'lib/esvg/symbol.rb', line 5 def mtime @mtime end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/esvg/symbol.rb', line 5 def name @name end |
#optimized ⇒ Object (readonly)
Returns the value of attribute optimized.
5 6 7 |
# File 'lib/esvg/symbol.rb', line 5 def optimized @optimized end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/esvg/symbol.rb', line 5 def path @path end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
5 6 7 |
# File 'lib/esvg/symbol.rb', line 5 def size @size end |
Instance Method Details
#attr ⇒ Object
64 65 66 |
# File 'lib/esvg/symbol.rb', line 64 def attr { id: @id, 'data-name' => @name }.merge @size end |
#changed? ⇒ Boolean
138 139 140 |
# File 'lib/esvg/symbol.rb', line 138 def changed? last_modified != mtime end |
#data ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/esvg/symbol.rb', line 48 def data { path: @path, id: @id, name: @name, group: @group, mtime: @mtime, size: @size, content: @content, defs: @defs, optimized: @optimized, optimized_at: @optimized_at, svgo_optimized: svgo? && @svgo_optimized } end |
#height ⇒ Object
44 45 46 |
# File 'lib/esvg/symbol.rb', line 44 def height @size[:height] end |
#optimize ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/esvg/symbol.rb', line 116 def optimize read if changed? # Only optimize again if the file has changed return @optimized if @optimized && @optimized_at && @optimized_at > @mtime @optimized = @content if svgo? response = Open3.capture3(%Q{#{Esvg.node_module('svgo')} --disable=removeUselessDefs -s '#{@optimized}' -o -}) if !response[0].empty? && response[2].success? @optimized = response[0] @svgo_optimized = true end end post_optimize @optimized_at = Time.now.to_i @optimized end |
#read ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/esvg/symbol.rb', line 17 def read return if !File.exist?(@path) # Ensure that cache optimization matches current optimization settings # If config has changed name, reset optimized build (name gets baked in) if changed? || @svgo_optimized != svgo? || name != file_name @optimized = nil @optimized_at = nil end @group = dir_key @name = file_name @id = file_id file_key if changed? @content = prep_defs pre_optimize File.read(@path) @mtime = last_modified @size = dimensions end self end |
#svgo? ⇒ Boolean
112 113 114 |
# File 'lib/esvg/symbol.rb', line 112 def svgo? @config[:optimize] && !!Esvg.node_module('svgo') end |
#use(options = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/esvg/symbol.rb', line 68 def use(={}) .delete(:fallback) content = .delete(:content) || '' if desc = .delete(:desc) content = "<desc>#{desc}</desc>#{content}" end if title = .delete(:title) content = "<title>#{title}</title>#{content}" end use_attr = .delete(:use) || {} svg_attr = { class: [@config[:class], @config[:prefix]+"-"+@name, .delete(:class)].compact.join(' '), viewBox: @size[:viewBox], role: 'img' }.merge() # If user doesn't pass a size or set scale: true if svg_attr[:width].nil? && svg_attr[:height].nil? && !svg_attr[:scale] svg_attr[:width] = width svg_attr[:height] = height end svg_attr.delete(:scale) %Q{<svg #{attributes(svg_attr)}>#{use_tag(use_attr)}#{content}</svg>} end |
#use_tag(options = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/esvg/symbol.rb', line 98 def use_tag(={}) ["xlink:href"] = "##{@id}" # If user doesn't pass a size or set scale: true if [:width].nil? && [:height].nil? && ![:scale] && !@config[:scale] [:width] ||= width [:height] ||= height end .delete(:scale) %Q{<use #{attributes(options)}/>} end |
#width ⇒ Object
40 41 42 |
# File 'lib/esvg/symbol.rb', line 40 def width @size[:width] end |