lib/esvg/symbol.rb in esvg-4.6.1 vs lib/esvg/symbol.rb in esvg-4.6.2

- old
+ new

@@ -4,18 +4,22 @@ class Symbol attr_reader :name, :id, :path, :content, :optimized, :size, :group, :mtime, :defs include Esvg::Utils - def initialize(path, config={}) - @config = config + def initialize(path, parent) + @parent = parent @path = path @last_checked = 0 load_data read end + def config + @parent.config + end + 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) @@ -100,16 +104,16 @@ end def use(options={}) # If preset key is set, merge presets from configuration - if options[:preset] && preset = @config[:presets][ options.delete(:preset).to_sym ] + if options[:preset] && preset = config[:presets][ options.delete(:preset).to_sym ] options = options.merge( preset ) end # If size key is set, merge size class from configuration - if options[:size] && size_class = @config[:sizes][ options.delete(:size).to_sym ] + if options[:size] && size_class = config[:sizes][ options.delete(:size).to_sym ] options = options.merge( size_class ) end options.delete(:fallback) content = options.delete(:content) || '' @@ -122,11 +126,11 @@ end use_attr = options.delete(:use) || {} svg_attr = { - class: [@config[:class], @config[:prefix]+"-"+@name, options.delete(:class)].compact.join(' '), + class: [config[:class], config[:prefix]+"-"+@name, options.delete(:class)].compact.join(' '), viewBox: @size[:viewBox], role: 'img' }.merge(options) if svg_attr[:scale] @@ -141,11 +145,11 @@ end def use_tag(options={}) options["xlink:href"] = "##{@id}" - if options[:scale] && @config[:scale] + if options[:scale] && config[:scale] # User doesn't want dimensions to be set options.delete(:scale) else # Scale dimensions based on attributes options = scale( options ) @@ -155,11 +159,11 @@ %Q{<use #{attributes(options)}></use>} end def svgo? - @config[:optimize] && !!Esvg.node_module('svgo') + config[:optimize] && !!Esvg.node_module('svgo') end def optimize read if changed? @@ -190,32 +194,32 @@ end private def load_data - if @config[:cache] - @config.delete(:cache).each do |name, value| + if config[:cache] + config.delete(:cache).each do |name, value| instance_variable_set("@#{name}", value) end end end def last_modified - if Time.now.to_i - @last_checked < @config[:throttle_read] + if Time.now.to_i - @last_checked < config[:throttle_read] @last_modified else @last_checked = Time.now.to_i @last_modified = File.mtime(@path).to_i end end def file_id(name) - dasherize "#{@config[:prefix]}-#{name}" + dasherize "#{config[:prefix]}-#{name}" end def local_path - @local_path ||= sub_path(@config[:source], @path) + @local_path ||= sub_path(config[:source], @path) end def file_name dasherize flatten_path.sub('.svg','') end @@ -228,25 +232,25 @@ dir = File.dirname(flatten_path) # Flattened paths which should be treated as assets will use '_' as their dir key # - flatten: _foo - _foo/icon.svg will have a dirkey of _ # - filename: _icons - treats all root or flattened files as assets - if dir == '.' && ( local_path.start_with?('_') || @config[:filename].start_with?('_') ) + if dir == '.' && ( local_path.start_with?('_') || config[:filename].start_with?('_') ) '_' else dir end end def flatten_path - @flattened_path ||= local_path.sub(Regexp.new(@config[:flatten]), '') + @flattened_path ||= local_path.sub(Regexp.new(config[:flatten_dir]), '') end def name_key(key) if key == '_' # Root level asset file - "_#{@config[:filename]}".sub(/_+/, '_') + "_#{config[:filename]}".sub(/_+/, '_') elsif key == '.' # Root level build file - @config[:filename] + config[:filename] else "#{key}" end end