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, parent) ⇒ Svg

Returns a new instance of Svg.



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

def initialize(name, symbols, parent)
  @parent = parent
  @name = name
  @symbols = symbols
  @symbol_defs = []
  @asset = File.basename(name).start_with?('_')
  @version = parent.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

#configObject



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

def config
  @parent.config
end

#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



35
36
37
38
39
40
41
# File 'lib/esvg/svg.rb', line 35

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

#named?(names = []) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/esvg/svg.rb', line 29

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

#pathObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/esvg/svg.rb', line 43

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