Class: Esvg::Svgs

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

Constant Summary collapse

CONFIG =
{
  filename: 'svgs',
  class: 'svg-symbol',
  prefix: 'svg',
  core: true,
  optimize: false,
  gzip: false,
  fingerprint: true,
  throttle_read: 4,
  flatten: [],
  alias: {}
}
CONFIG_RAILS =
{
  source: "app/assets/svgs",
  assets: "app/assets/javascripts",
  build: "public/javascripts",
  temp: "tmp"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

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

Constructor Details

#initialize(options = {}) ⇒ Svgs

Returns a new instance of Svgs.



33
34
35
36
37
38
39
40
41
# File 'lib/esvg/svgs.rb', line 33

def initialize(options={})
  config(options)
  @symbols = []
  @svgs = []
  @last_read = 0
  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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/esvg/svgs.rb', line 107

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



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

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

#buildable_svgs(names = nil) ⇒ Object



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

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

#config(options = {}) ⇒ Object



43
44
45
46
47
48
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
# File 'lib/esvg/svgs.rb', line 43

def config(options={})
  @config ||= begin
    paths = [options[:config_file], 'config/esvg.yml', 'esvg.yml'].compact

    config = CONFIG.dup

    if Esvg.rails? || options[:rails]
      config.merge!(CONFIG_RAILS)
    end

    if path = paths.select{ |p| File.exist?(p)}.first
      config.merge!(symbolize_keys(YAML.load(File.read(path) || {})))
    end

    config.merge!(options)

    config[:filename] = File.basename(config[:filename], '.*')

    config[:pwd]      = File.expand_path Dir.pwd
    config[:source]   = File.expand_path config[:source] || config[:pwd]
    config[:build]    = File.expand_path config[:build]  || config[:pwd]
    config[:assets]   = File.expand_path config[:assets] || config[:pwd]

    config[:temp]     = config[:pwd] if config[:temp].nil?
    config[:temp]     = File.expand_path File.join(config[:temp], '.esvg-cache')

    config[:aliases] = load_aliases(config[:alias])
    config[:flatten] = [config[:flatten]].flatten.map { |dir| File.join(dir, '/') }.join('|')

    config
  end
end

#embed_script(names = nil) ⇒ Object

Embed only build scripts



145
146
147
148
149
150
# File 'lib/esvg/svgs.rb', line 145

def embed_script(names=nil)
  embeds = buildable_svgs(names).map(&:embed)
  if !embeds.empty?
    "<script>#{js(embeds.join("\n"))}</script>"
  end
end

#find_svgs(names = nil) ⇒ Object



169
170
171
172
173
# File 'lib/esvg/svgs.rb', line 169

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



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/esvg/svgs.rb', line 156

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/esvg/svgs.rb', line 76

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



137
138
139
140
141
142
# File 'lib/esvg/svgs.rb', line 137

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

#write_cacheObject



133
134
135
# File 'lib/esvg/svgs.rb', line 133

def write_cache
  write_tmp '.symbols', @symbols.map(&:data).to_yaml
end