Module: Esvg

Extended by:
Esvg
Included in:
Esvg
Defined in:
lib/esvg.rb,
lib/esvg/svg.rb,
lib/esvg/svgs.rb,
lib/esvg/utils.rb,
lib/esvg/symbol.rb,
lib/esvg/version.rb,
lib/esvg/railties.rb

Defined Under Namespace

Modules: Helpers, Utils Classes: Railtie, Svg, Svgs, Symbol

Constant Summary collapse

VERSION =
"4.2.3"

Instance Method Summary collapse

Instance Method Details

#build_paths(names = nil) ⇒ Object



48
49
50
# File 'lib/esvg.rb', line 48

def build_paths(names=nil)
  find_svgs(names).map{|s| s.build_paths(names) }.flatten
end

#embed(names = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/esvg.rb', line 38

def embed(names=nil)
  if rails? && Rails.env.production?
    html_safe build_paths(names).each do |path|
      javascript_include_tag(path, async: true)
    end.join("\n")
  else
    html_safe find_svgs(names).map{|s| s.embed_script(names) }.join
  end
end

#find_svgs(names = nil) ⇒ Object



52
53
54
# File 'lib/esvg.rb', line 52

def find_svgs(names=nil)
  @svgs.select {|s| s.buildable_svgs(names) }
end

#find_symbol(name, options = {}) ⇒ Object



56
57
58
59
60
# File 'lib/esvg.rb', line 56

def find_symbol(name, options={})
  if group = @svgs.find {|s| s.find_symbol(name, options[:fallback]) }
    group.find_symbol(name, options[:fallback])
  end
end

#html_safe(input) ⇒ Object



66
67
68
69
# File 'lib/esvg.rb', line 66

def html_safe(input)
  input = input.html_safe if rails?
  input
end

#new(options = {}) ⇒ Object



16
17
18
19
20
# File 'lib/esvg.rb', line 16

def new(options={})
  @svgs ||=[]
  @svgs << Svgs.new(options)
  @svgs.last
end

#node_module(cmd) ⇒ Object

Determine if an NPM module is installed by checking paths with ‘npm bin` Returns path to binary if installed



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/esvg.rb', line 81

def node_module(cmd)
  @modules ||={}
  return @modules[cmd] if !@modules[cmd].nil?

  local = "$(npm bin)/#{cmd}"
  global = "$(npm -g bin)/#{cmd}"

  @modules[cmd] = if Open3.capture3(local)[2].success?
    local
  elsif Open3.capture3(global)[2].success?
    global
  else
    false
  end
end

#precompile_assetsObject



71
72
73
74
75
76
77
# File 'lib/esvg.rb', line 71

def precompile_assets
  if rails? && defined?(Rake)
    ::Rake::Task['assets:precompile'].enhance do
      Svgs.new(gzip: true, print: true).build
    end
  end
end

#rails?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/esvg.rb', line 62

def rails?
  defined?(Rails)
end

#svgsObject



22
23
24
# File 'lib/esvg.rb', line 22

def svgs
  @svgs
end

#use(name, options = {}) ⇒ Object



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

def use(name, options={})
  if symbol = find_symbol(name, options)
    html_safe symbol.use options
  end
end

#use_tag(name, options = {}) ⇒ Object



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

def use_tag(name, options={})
  if symbol = find_symbol(name, options)
    html_safe symbol.use_tag options
  end
end