class Esvg::Svgs

Attributes

symbols[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/esvg/svgs.rb, line 14
def initialize(options={})
  @config = options
  @symbols = []
  @svgs = []
  read_cache

  load_files
end

Public Instance Methods

build() click to toggle source
# File lib/esvg/svgs.rb, line 90
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) click to toggle source
# File lib/esvg/svgs.rb, line 159
def build_paths(names=nil)
  buildable_svgs(names).map{ |f| File.basename(f.path) }
end
buildable_svgs(names=nil) click to toggle source
# File lib/esvg/svgs.rb, line 182
def buildable_svgs(names=nil)
  find_svgs(names).reject(&:asset)
end
cache_stale?() click to toggle source
# File lib/esvg/svgs.rb, line 152
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
config() click to toggle source
# File lib/esvg/svgs.rb, line 27
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?() click to toggle source
# File lib/esvg/svgs.rb, line 45
def config_changed?
  @config[:config_file] && @config[:read] < File.mtime( @config[:config_file] ).to_i
end
config_expired?() click to toggle source
# File lib/esvg/svgs.rb, line 41
def config_expired?
  @config[:throttle_read] < (Time.now.to_i - @config[:read])
end
embed_script(names=nil) click to toggle source

Embed svg symbols

# File lib/esvg/svgs.rb, line 137
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) click to toggle source
# File lib/esvg/svgs.rb, line 176
def find_svgs(names=nil)
  return @svgs if names.nil? || names.empty?

  @svgs.select { |svg| svg.named?(names) }
end
find_symbol(name, fallback=nil) click to toggle source
# File lib/esvg/svgs.rb, line 163
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_files() click to toggle source
# File lib/esvg/svgs.rb, line 53
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?() click to toggle source
# File lib/esvg/svgs.rb, line 49
def loaded_recently?
  @last_load && (Time.now.to_i - @last_load) < @config[:throttle_read]
end
print_path( path ) click to toggle source
production?() click to toggle source
# File lib/esvg/svgs.rb, line 23
def production?
  @config[:env] == 'production'
end
read_cache() click to toggle source
# File lib/esvg/svgs.rb, line 120
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_cache() click to toggle source
# File lib/esvg/svgs.rb, line 115
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

Private Instance Methods

get_alias(name) click to toggle source
# File lib/esvg/svgs.rb, line 207
def get_alias(name)
  @config[:aliases][dasherize(name).to_sym] || name
end
js( embed ) click to toggle source
# File lib/esvg/svgs.rb, line 198
def js( embed )
  @embed = embed
  read_js( 'embed' )
end
js_core() click to toggle source
# File lib/esvg/svgs.rb, line 203
def js_core
  read_js( 'core' )
end
read_js(path) click to toggle source
# File lib/esvg/svgs.rb, line 188
def read_js(path)
  contents = ''
  path = File.expand_path("js/#{path}.js.erb", File.dirname(__FILE__))

  File.open path do |f|
    contents = ERB.new(f.read).result(binding)
  end
  contents
end
read_tmp(name) click to toggle source
# File lib/esvg/svgs.rb, line 218
def read_tmp(name)
  path = File.join(@config[:temp], name)
  File.read path if File.exist? path
end
write_file(path, contents) click to toggle source
# File lib/esvg/svgs.rb, line 223
def write_file(path, contents)
  FileUtils.mkdir_p(File.expand_path(File.dirname(path)))
  File.open(path, 'w') do |io|
    io.write(contents)
  end
end
write_tmp(name, content) click to toggle source
# File lib/esvg/svgs.rb, line 211
def write_tmp(name, content)
  path = File.join(@config[:temp], name)
  FileUtils.mkdir_p(File.dirname(path))
  write_file path, content
  path
end