Class: Esvg::Svgs
- Inherits:
-
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
|
# File 'lib/esvg/svgs.rb', line 14
def initialize(options={})
@config = options
@symbols = []
@svgs = []
read_cache
load_files
end
|
Instance Attribute Details
#symbols ⇒ Object
Returns the value of attribute symbols.
12
13
14
|
# File 'lib/esvg/svgs.rb', line 12
def symbols
@symbols
end
|
Instance Method Details
#build ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# 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) ⇒ Object
159
160
161
|
# 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) ⇒ Object
182
183
184
|
# File 'lib/esvg/svgs.rb', line 182
def buildable_svgs(names=nil)
find_svgs(names).reject(&:asset)
end
|
#cache_stale? ⇒ Boolean
152
153
154
155
156
157
|
# File 'lib/esvg/svgs.rb', line 152
def cache_stale?
path = File.join(@config[:temp], @config[:cache_file])
!File.exist?(path) || @reset_cache || @symbols.size > 0 && File.mtime(path).to_i < @symbols.map(&:mtime).sort.last
end
|
#config ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'lib/esvg/svgs.rb', line 27
def config
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
45
46
47
|
# 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? ⇒ Boolean
41
42
43
|
# File 'lib/esvg/svgs.rb', line 41
def config_expired?
@config[:throttle_read] < (Time.now.to_i - @config[:read])
end
|
#embed_script(names = nil) ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# 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) ⇒ Object
176
177
178
179
180
|
# 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) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/esvg/svgs.rb', line 163
def find_symbol(name, fallback=nil)
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 ⇒ Object
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
88
|
# 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 = []
@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
49
50
51
|
# 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) ⇒ Object
37
38
39
|
# File 'lib/esvg/svgs.rb', line 37
def print_path( path )
sub_path( @config[:root], path )
end
|
#production? ⇒ Boolean
23
24
25
|
# File 'lib/esvg/svgs.rb', line 23
def production?
@config[:env] == 'production'
end
|
#read_cache ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/esvg/svgs.rb', line 120
def read_cache
return unless cache = read_tmp(@config[:cache_file])
YAML.load(cache).each do |c|
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 ⇒ Object
115
116
117
118
|
# 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
|