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.



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

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

  load_files
end

Instance Attribute Details

#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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/esvg/svgs.rb', line 89

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 #{print_path( file.path )}" if @config[:print]
    paths << file.path

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

  write_cache
  paths
end

#build_paths(names = nil) ⇒ Object



158
159
160
# File 'lib/esvg/svgs.rb', line 158

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

#buildable_svgs(names = nil) ⇒ Object



181
182
183
# File 'lib/esvg/svgs.rb', line 181

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

#cache_stale?Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
# File 'lib/esvg/svgs.rb', line 151

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) || @reset_cache || @symbols.size > 0 && File.mtime(path).to_i < @symbols.map(&:mtime).sort.last
end

#configObject



26
27
28
29
30
31
32
33
34
# File 'lib/esvg/svgs.rb', line 26

def config
  # use cached configuration
  if !production? && config_expired? && config_changed?
    puts "Reloading ESVG config: #{print_path( @config[:config_file] )}" if @config[:print]
    @config = Esvg.update_config( @config )
  else
    @config
  end
end

#config_changed?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/esvg/svgs.rb', line 44

def config_changed?
  @config[:config_file] && @config[:read] < File.mtime( @config[:config_file] ).to_i
end

#config_expired?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/esvg/svgs.rb', line 40

def config_expired?
  @config[:throttle_read] < (Time.now.to_i - @config[:read])
end

#embed_script(names = nil) ⇒ Object

Embed svg symbols



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/esvg/svgs.rb', line 136

def embed_script(names=nil)
  if production?
    embeds = buildable_svgs(names).map(&:embed)
  else
    embeds = find_svgs(names).map(&:embed)
  end

  write_cache if !production? && cache_stale?

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

end

#find_svgs(names = nil) ⇒ Object



175
176
177
178
179
# File 'lib/esvg/svgs.rb', line 175

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



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/esvg/svgs.rb', line 162

def find_symbol(name, fallback=nil)
  # Ensure that file changes are picked up in development
  load_files unless production?

  name = get_alias dasherize(name)

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

#load_filesObject



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
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/esvg/svgs.rb', line 52

def load_files
  return if loaded_recently?

  files        = Dir[File.join(@config[:source], '**/*.svg')].uniq.sort
  size_before  = @symbols.size
  added        = []
  removed      = []

  # Remove all files which no longer exist
  @symbols.reject(&:exist?).each { |s| 
    removed.push @symbols.delete(s) 
  }

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

  if @config[:debug]
    puts %Q{Read #{files.size} SVGs from #{print_path( @config[:source] )}}
    puts %Q{  Added: #{added.size} SVG#{'s' if added.size != 1} } if added.size > 0 
    puts %Q{  Removed: #{removed.size} SVG#{'s' if removed.size != 1} } if removed.size > 0 
  end

  @svgs.clear

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

  @last_load = Time.now.to_i
  write_cache if cache_stale?

end

#loaded_recently?Boolean

Returns:

  • (Boolean)


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

def loaded_recently?
  @last_load && (Time.now.to_i - @last_load) < @config[:throttle_read]
end


36
37
38
# File 'lib/esvg/svgs.rb', line 36

def print_path( path )
  sub_path( @config[:root], path )
end

#production?Boolean

Returns:

  • (Boolean)


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

def production?
  @config[:env] == 'production'
end

#read_cacheObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/esvg/svgs.rb', line 119

def read_cache
  return unless cache = read_tmp(@config[:cache_file])

  YAML.load(cache).each do |c|

    # Only read cache data which matches this source
    # Example: Cache data from previous gem versions shouldn't be loaded
    next unless c[:path].include?(@config[:source])

    @config[:cache] = c
    @symbols << Symbol.new(c[:path], self)
  end

  @reset_cache = cache.size != @symbols.size
end

#write_cacheObject



114
115
116
117
# File 'lib/esvg/svgs.rb', line 114

def write_cache
  puts "Writing cache: #{ print_path( File.join( @config[:temp], @config[:cache_file]) )}" if @config[:debug]
  write_tmp @config[:cache_file], @symbols.map(&:data).to_yaml
end