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
- #config ⇒ Object
- #data ⇒ Object
- #dir ⇒ Object
- #exist? ⇒ Boolean
- #height ⇒ Object
-
#initialize(path, parent) ⇒ Symbol
constructor
A new instance of Symbol.
- #optimize ⇒ Object
-
#optimize? ⇒ Boolean
Only optimize if - Configuration asks for it - SVGO is present - If Rails is present.
- #read ⇒ Object
- #scale(a) ⇒ Object
-
#scale_height(w) ⇒ Object
Scale height based on propotion to width.
-
#scale_width(h) ⇒ Object
Scale width based on propotion to height.
-
#split_unit(size) ⇒ Object
Separate size and unit for easier math.
- #symbol ⇒ Object
- #use(options = {}) ⇒ Object
- #use_tag(options = {}) ⇒ Object
- #width ⇒ Object
Methods included from Utils
#attributes, #compress, #dasherize, #sort, #sub_path
Constructor Details
#initialize(path, parent) ⇒ Symbol
Returns a new instance of Symbol.
9 10 11 12 13 14 15 |
# File 'lib/esvg/symbol.rb', line 9 def initialize(path, parent) @parent = parent @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
110 111 112 |
# File 'lib/esvg/symbol.rb', line 110 def attr { id: @id, 'data-name' => @name }.merge @size end |
#changed? ⇒ Boolean
209 210 211 |
# File 'lib/esvg/symbol.rb', line 209 def changed? last_modified != mtime end |
#config ⇒ Object
17 18 19 |
# File 'lib/esvg/symbol.rb', line 17 def config @parent.config end |
#data ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/esvg/symbol.rb', line 95 def data { path: @path, name: @name, group: @group, mtime: @mtime, size: @size, content: @content, defs: @defs, optimized: @optimized, optimized_at: @optimized_at, svgo_optimized: optimize? && @svgo_optimized } end |
#dir ⇒ Object
25 26 27 |
# File 'lib/esvg/symbol.rb', line 25 def dir @group end |
#exist? ⇒ Boolean
21 22 23 |
# File 'lib/esvg/symbol.rb', line 21 def exist? File.exist?(@path) end |
#height ⇒ Object
56 57 58 |
# File 'lib/esvg/symbol.rb', line 56 def height @size[:height] end |
#optimize ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/esvg/symbol.rb', line 179 def optimize read if changed? # Only optimize again if the file has changed if @optimized && @optimized_at && @optimized_at > @mtime return @optimized end # Only optimize if SVGO is installed if optimize? puts "Optimizing #{name}.svg" if config[:print] response = Open3.capture3(%Q{#{Esvg.node_module('svgo')} --disable=removeUselessDefs -s '#{@content}' -o -}) if !response[0].empty? && response[2].success? @optimized = response[0] @svgo_optimized = true end post_optimize @optimized_at = Time.now.to_i @optimized end end |
#optimize? ⇒ Boolean
Only optimize if
-
Configuration asks for it
-
SVGO is present
-
If Rails is present
175 176 177 |
# File 'lib/esvg/symbol.rb', line 175 def optimize? config[:optimize] && !!Esvg.node_module('svgo') && config[:env] == 'production' end |
#read ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/esvg/symbol.rb', line 29 def read return if !exist? # Ensure that cache optimization matches current optimization settings # If config has changed name, reset optimized build (name gets baked in) if changed? || @svgo_optimized != optimize? || 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 |
#scale(a) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/esvg/symbol.rb', line 79 def scale( a ) # Width was set, determine scaled height if a[:width] a[:height] ||= scale_height( a[:width] ) # Height was set, determine scaled width elsif a[:height] a[:width] ||= scale_width( a[:height] ) # Nothing was set, default to dimensions else a[:width] = width a[:height] = height end a end |
#scale_height(w) ⇒ Object
Scale height based on propotion to width
67 68 69 70 |
# File 'lib/esvg/symbol.rb', line 67 def scale_height( w ) s = split_unit( w ) "#{( s[:size] / width * height ).round(2)}#{s[:unit]}" end |
#scale_width(h) ⇒ Object
Scale width based on propotion to height
61 62 63 64 |
# File 'lib/esvg/symbol.rb', line 61 def scale_width( h ) s = split_unit( h ) "#{( s[:size] / height * width ).round(2)}#{s[:unit]}" end |
#split_unit(size) ⇒ Object
Separate size and unit for easier math. Returns: { size: 10, unit: ‘px’ }
74 75 76 77 |
# File 'lib/esvg/symbol.rb', line 74 def split_unit( size ) m = size.to_s.match(/(\d+)\s*(\D*)/) { size: m[1].to_f, unit: m[2] } end |
#symbol ⇒ Object
205 206 207 |
# File 'lib/esvg/symbol.rb', line 205 def symbol symbolize( optimize || @content ) end |
#use(options = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/esvg/symbol.rb', line 114 def use(={}) # If preset key is set, merge presets from configuration if [:preset] && preset = config[:presets][ .delete(:preset).to_sym ] = .merge( preset ) end # If size key is set, merge size class from configuration if [:size] && size_class = config[:sizes][ .delete(:size).to_sym ] = .merge( size_class ) end .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 svg_attr[:scale] # User doesn't want dimensions to be set svg_attr.delete(:scale) else # Scale dimensions based on attributes svg_attr = scale( svg_attr ) end %Q{<svg #{attributes(svg_attr)}>#{use_tag(use_attr)}#{content}</svg>} end |
#use_tag(options = {}) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/esvg/symbol.rb', line 155 def use_tag(={}) ["xlink:href"] = "##{@id}" if [:scale] && config[:scale] # User doesn't want dimensions to be set .delete(:scale) else # Scale dimensions based on attributes = scale( ) end .delete(:scale) %Q{<use #{attributes()}></use>} end |
#width ⇒ Object
52 53 54 |
# File 'lib/esvg/symbol.rb', line 52 def width @size[:width] end |