Class: Esvg::Svg

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/esvg/svg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#attributes, #compress, #dasherize, #sort, #sub_path

Constructor Details

#initialize(name, symbols, config = {}) ⇒ Svg



10
11
12
13
14
15
16
17
# File 'lib/esvg/svg.rb', line 10

def initialize(name, symbols, config={})
  @name = name
  @config = config
  @symbols = symbols
  @symbol_defs = []
  @asset = File.basename(name).start_with?('_')
  @version = @config[:version] || Digest::MD5.hexdigest(symbols.map(&:mtime).join)
end

Instance Attribute Details

#assetObject (readonly)

Returns the value of attribute asset.



8
9
10
# File 'lib/esvg/svg.rb', line 8

def asset
  @asset
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/esvg/svg.rb', line 8

def name
  @name
end

#vesionObject (readonly)

Returns the value of attribute vesion.



8
9
10
# File 'lib/esvg/svg.rb', line 8

def vesion
  @vesion
end

Instance Method Details

#embedObject



19
20
21
22
23
# File 'lib/esvg/svg.rb', line 19

def embed
  %Q{if (!document.querySelector('#esvg-#{id}')) {
  document.querySelector('body').insertAdjacentHTML('afterbegin', '#{svg}')
}}
end

#idObject



31
32
33
34
35
36
37
# File 'lib/esvg/svg.rb', line 31

def id
  if @name == '.'
    'symbols'
  else
    dasherize "#{@config[:prefix]}-#{name_key}"
  end
end

#named?(names = []) ⇒ Boolean



25
26
27
28
29
# File 'lib/esvg/svg.rb', line 25

def named?(names=[])
  [names].flatten.map { 
    |n| dasherize(n) 
  }.include? dasherize(@name)
end

#pathObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/esvg/svg.rb', line 39

def path
  @path ||= begin
    name = name_key

    if name.start_with?('_') # Is it an asset?
      File.join @config[:assets], "#{name}.js"
    else # or a build file?

      # User doesn't want a fingerprinted build file and hasn't set a version
      if !@config[:fingerprint] && !@config[:version]
        File.join @config[:build], "#{name}.js"
      else
        File.join @config[:build], "#{name}-#{@version}.js"
      end
    end
  end
end