Class: Cyborg::Assets::Svgs

Inherits:
AssetType show all
Defined in:
lib/cyborg/plugin/assets/svgs.rb

Instance Attribute Summary

Attributes inherited from AssetType

#base, #plugin

Instance Method Summary collapse

Methods inherited from AssetType

#build_failure, #build_success, #change, #compress, #destination, #file_event, #filter_files, #find_files, #find_node_module, #log_error, #log_success, #npm_command, #url, #urls, #versioned, #watch

Constructor Details

#initialize(plugin, path) ⇒ Svgs

Returns a new instance of Svgs.



5
6
7
8
9
10
11
12
# File 'lib/cyborg/plugin/assets/svgs.rb', line 5

def initialize(plugin, path)

  require 'esvg'

  @plugin = plugin
  @base = path
  @svg = esvg
end

Instance Method Details

#buildObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cyborg/plugin/assets/svgs.rb', line 49

def build
  begin
    esvg.load_files

    return if esvg.symbols.empty?

    if files = esvg.build
      files.each do |file|
        puts build_success(file)
      end
    else
      log_error "FAILED TO BUILD SVGs"
    end
  rescue Exception => e
    log_error "\nFAILED TO BUILD SVGs"

    if e.backtrace && e.backtrace.is_a?(Array)
      log_error "Error in file: #{local_path(e.backtrace.shift)}"

      e.backtrace.each do |line|
        log_error local_path(line)
      end
    end

    log_error("  #{e.message}\n") if e.message

  end
end

#build_pathsObject



45
46
47
# File 'lib/cyborg/plugin/assets/svgs.rb', line 45

def build_paths
  esvg.build_paths.map { |file| file.sub("-#{plugin.version}",'') }
end

#esvgObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cyborg/plugin/assets/svgs.rb', line 14

def esvg
  @svg || Esvg.new({
    config_file: File.join(plugin.root, 'config', 'esvg.yml'),
    source: @base,
    assets: plugin.paths[:javascripts],
    version: plugin.version,
    build: plugin.destination,
    temp: Cyborg.rails_path('tmp/cache/assets'),
    filename: '_icons.js',
    compress: Cyborg.production?,
    optimize: true,
    print: false
  })
end

#extObject



29
30
31
# File 'lib/cyborg/plugin/assets/svgs.rb', line 29

def ext
  "svg"
end

#local_path(path) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/cyborg/plugin/assets/svgs.rb', line 33

def local_path(path)
  path = File.expand_path(path)

  # Strip all irrelevant sections of the path
  path.sub(plugin.paths[:javascripts]+'/', '') # written to assets dir
      .sub(plugin.root+'/','')                 # writtent to public dir
end

#use(*args) ⇒ Object



41
42
43
# File 'lib/cyborg/plugin/assets/svgs.rb', line 41

def use(*args)
  esvg.use(*args)
end