Class: Esvg::Svgs

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

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

Constructor Details

#initialize(options = {}) ⇒ Svgs

Returns a new instance of Svgs.



14
15
16
17
18
19
20
21
22
# File 'lib/esvg/svgs.rb', line 14

def initialize(options={})
  @config = options
  @symbols = []
  @svgs = []
  @last_read = 0
  read_cache

  load_files
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/esvg/svgs.rb', line 12

def config
  @config
end

#symbolsObject (readonly)

Returns the value of attribute symbols.



11
12
13
# File 'lib/esvg/svgs.rb', line 11

def symbols
  @symbols
end

Instance Method Details

#buildObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/esvg/svgs.rb', line 55

def build

  paths = []

  if config[:core]
    path = File.join config[:assets], "_esvg.js"
    write_file path, js_core
    paths << path
  end

  @svgs.each do |file|
    write_file file.path, js(file.embed)

    puts "Writing #{file.path}" if config[:print]
    paths << file.path

    if !file.asset && config[:gzip] && gz = compress(file.path)
      puts "Writing #{gz}" if config[:print]
      paths << gz
    end
  end

  write_cache
  paths
end

#build_paths(names = nil) ⇒ Object



112
113
114
# File 'lib/esvg/svgs.rb', line 112

def build_paths(names=nil)
  buildable_svgs(names).map{ |f| File.basename(f.path) }
end

#buildable_svgs(names = nil) ⇒ Object



135
136
137
# File 'lib/esvg/svgs.rb', line 135

def buildable_svgs(names=nil)
  find_svgs(names).reject(&:asset)
end

#cache_stale?Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
# File 'lib/esvg/svgs.rb', line 105

def cache_stale?
  path = File.join(config[:temp], config[:cache_file])

  # No cache file exists or cache file is older than a new symbol
  !File.exist?(path) || File.mtime(path).to_i < @symbols.map(&:mtime).sort.last
end

#embed_script(names = nil) ⇒ Object

Embed only build scripts



94
95
96
97
98
99
100
101
102
103
# File 'lib/esvg/svgs.rb', line 94

def embed_script(names=nil)
  embeds = buildable_svgs(names).map(&:embed)

  write_cache if cache_stale?

  if !embeds.empty?
    "<script>#{js(embeds.join("\n"))}</script>"
  end

end

#find_svgs(names = nil) ⇒ Object



129
130
131
132
133
# File 'lib/esvg/svgs.rb', line 129

def find_svgs(names=nil)
  return @svgs if names.nil? || names.empty?

  @svgs.select { |svg| svg.named?(names) }
end

#find_symbol(name, fallback = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/esvg/svgs.rb', line 116

def find_symbol(name, fallback=nil)
  # Ensure that file changes are picked up in development
  load_files unless Esvg.rails? && Rails.env.production?

  name = get_alias dasherize(name)

  if svg = @symbols.find { |s| s.name == name }
    svg
  elsif fallback
    find_symbol(fallback)
  end
end

#load_filesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/esvg/svgs.rb', line 24

def load_files
  return if (Time.now.to_i - @last_read) < config[:throttle_read]

  files = Dir[File.join(config[:source], '**/*.svg')].uniq.sort

  if files.empty? && config[:print]
    puts "No svgs found at #{config[:source]}"
    return
  end

  # Remove deleted files
  @symbols.reject(&:read).each { |s| @symbols.delete(s) }

  files.each do |path|
    unless @symbols.find { |s| s.path == path }
      @symbols << Symbol.new(path, config)
    end
  end

  @svgs.clear

  sort(@symbols.group_by(&:group)).each do |name, symbols|
    @svgs << Svg.new(name, symbols, config)
  end

  @last_read = Time.now.to_i

  puts "Read #{@symbols.size} files from #{config[:source]}" if config[:print]

end

#read_cacheObject



86
87
88
89
90
91
# File 'lib/esvg/svgs.rb', line 86

def read_cache
  (YAML.load(read_tmp config[:cache_file]) || []).each do |c|
    config[:cache] = c
    @symbols << Symbol.new(c[:path], config)
  end
end

#write_cacheObject



81
82
83
84
# File 'lib/esvg/svgs.rb', line 81

def write_cache
  puts "Writing cache" if config[:print]
  write_tmp config[:cache_file], @symbols.map(&:data).to_yaml
end