Class: Middleman::Sources
- Inherits:
-
Object
- Object
- Middleman::Sources
show all
- Extended by:
- Forwardable
- Includes:
- Contracts
- Defined in:
- lib/middleman-core/sources.rb
Overview
Sources handle multiple on-disk collections of files which make up
a Middleman project. They are separated by type
which can then be
queried. For example, the source
type represents all content that
the sitemap uses to build a project. The data
type represents YAML
data. The locales
type represents localization YAML, and so on.
Defined Under Namespace
Classes: CallbackDescriptor
Constant Summary
collapse
- OUTPUT_TYPES =
Types which could cause output to change.
[:source, :locales, :data].freeze
- CODE_TYPES =
Types which require a reload to eval ruby
[:reload].freeze
- Matcher =
- HANDLER =
Duck-typed definition of a valid source watcher
Constants included
from Contracts
Contracts::PATH_MATCHER
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#by_type(type) ⇒ Object
-
#changed(matcher = nil, &_block) ⇒ Object
-
#deleted(matcher = nil, &_block) ⇒ Object
-
#exists?(types, path) ⇒ Boolean
-
#files ⇒ Object
-
#find(types, path, glob = false) ⇒ Object
-
#find_new_files! ⇒ Object
-
#globally_ignored?(file) ⇒ Boolean
-
#ignore(name, type, regex = nil, &block) ⇒ Object
-
#ignored?(path) ⇒ Boolean
-
#initialize(app, options = {}, watchers = []) ⇒ Sources
constructor
A new instance of Sources.
-
#on_change(types, &block)
Disconnect a specific watcher.
-
#poll_once! ⇒ Object
-
#start! ⇒ Object
-
#stop! ⇒ Object
-
#Symbol
Add a proc to ignore paths with either a regex or block.
-
#unwatch(watcher) ⇒ Object
-
#watch(type_or_handler, options = {}) ⇒ Object
-
#watcher_for_path(types, path) ⇒ Object
-
#watchers ⇒ Object
Methods included from Contracts
#Contract
Constructor Details
#initialize(app, options = {}, watchers = []) ⇒ Sources
Returns a new instance of Sources.
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/middleman-core/sources.rb', line 55
def initialize(app, options={}, watchers=[])
@app = app
@watchers = watchers
@sorted_watchers = @watchers.dup.freeze
::Middleman::Sources.file_cache = {}
@options = options
@on_change_callbacks = ::Hamster::Vector.empty
@ignores = ::Hamster::Hash.empty
@running = false
@update_count = 0
@last_update_count = -1
@app.before_shutdown(&method(:stop!))
end
|
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
32
33
34
|
# File 'lib/middleman-core/sources.rb', line 32
def app
@app
end
|
#options ⇒ Object
Returns the value of attribute options.
39
40
41
|
# File 'lib/middleman-core/sources.rb', line 39
def options
@options
end
|
Instance Method Details
#by_type(type) ⇒ Object
170
171
172
|
# File 'lib/middleman-core/sources.rb', line 170
def by_type(type)
self.class.new @app, @options, watchers.select { |d| d.type == type }
end
|
#changed(matcher = nil, &_block) ⇒ Object
278
279
280
281
282
283
284
|
# File 'lib/middleman-core/sources.rb', line 278
def changed(matcher=nil, &_block)
on_change OUTPUT_TYPES do |updated, _removed|
updated
.select { |f| matcher.nil? ? true : matches?(matcher, f) }
.each { |f| yield f[:relative_path] }
end
end
|
#deleted(matcher = nil, &_block) ⇒ Object
290
291
292
293
294
295
296
|
# File 'lib/middleman-core/sources.rb', line 290
def deleted(matcher=nil, &_block)
on_change OUTPUT_TYPES do |_updated, removed|
removed
.select { |f| matcher.nil? ? true : matches?(matcher, f) }
.each { |f| yield f[:relative_path] }
end
end
|
#exists?(types, path) ⇒ Boolean
206
207
208
|
# File 'lib/middleman-core/sources.rb', line 206
def exists?(types, path)
watchers.any? { |d| Array(types).include?(d.type) && d.exists?(path) }
end
|
#files ⇒ Object
178
179
180
|
# File 'lib/middleman-core/sources.rb', line 178
def files
watchers.flat_map(&:files).uniq { |f| f[:relative_path] }
end
|
#find(types, path, glob = false) ⇒ Object
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/middleman-core/sources.rb', line 189
def find(types, path, glob=false)
array_of_types = Array(types)
watchers
.lazy
.select { |d| array_of_types.include?(d.type) }
.map { |d| d.find(path, glob) }
.reject(&:nil?)
.first
end
|
#find_new_files! ⇒ Object
224
225
226
227
228
229
|
# File 'lib/middleman-core/sources.rb', line 224
def find_new_files!
return [] unless @update_count != @last_update_count
@last_update_count = @update_count
watchers.reduce([]) { |sum, w| sum + w.find_new_files! }
end
|
#globally_ignored?(file) ⇒ Boolean
102
103
104
105
106
107
|
# File 'lib/middleman-core/sources.rb', line 102
def globally_ignored?(file)
@ignores.values.any? do |descriptor|
((descriptor[:type] == :all) || file[:types].include?(descriptor[:type])) &&
matches?(descriptor[:validator], file)
end
end
|
#ignore(name, type, regex = nil, &block) ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/middleman-core/sources.rb', line 89
def ignore(name, type, regex=nil, &block)
@ignores = @ignores.put(name, type: type,
validator: (block_given? ? block : regex))
bump_count
poll_once! if @running
end
|
#ignored?(path) ⇒ Boolean
302
303
304
305
|
# File 'lib/middleman-core/sources.rb', line 302
def ignored?(path)
descriptor = find(OUTPUT_TYPES, path)
!descriptor || globally_ignored?(descriptor)
end
|
#on_change(types, &block)
This method returns an undefined value.
Disconnect a specific watcher.
156
|
# File 'lib/middleman-core/sources.rb', line 156
Contract RespondTo[:on_change] => Any
|
#poll_once! ⇒ Object
235
236
237
238
239
240
|
# File 'lib/middleman-core/sources.rb', line 235
def poll_once!
return [] unless @update_count != @last_update_count
@last_update_count = @update_count
watchers.reduce([]) { |sum, w| sum + w.poll_once! }
end
|
#start! ⇒ Object
246
247
248
249
|
# File 'lib/middleman-core/sources.rb', line 246
def start!
watchers.each(&:listen!)
@running = true
end
|
#stop! ⇒ Object
255
256
257
258
|
# File 'lib/middleman-core/sources.rb', line 255
def stop!
watchers.each(&:stop_listener!)
@running = false
end
|
#Symbol
This method returns an undefined value.
Add a proc to ignore paths with either a regex or block.
88
|
# File 'lib/middleman-core/sources.rb', line 88
Contract Symbol, Symbol, Or[Regexp, Proc] => Any
|
#unwatch(watcher) ⇒ Object
157
158
159
160
161
162
163
|
# File 'lib/middleman-core/sources.rb', line 157
def unwatch(watcher)
@watchers.delete(watcher)
watcher.unwatch
bump_count
end
|
#watch(type_or_handler, options = {}) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/middleman-core/sources.rb', line 117
def watch(type_or_handler, options={})
handler = if type_or_handler.is_a? Symbol
path = File.expand_path(options.delete(:path), app.root)
SourceWatcher.new(self, type_or_handler, path, options)
else
type_or_handler
end
@watchers << handler
n = 0
@sorted_watchers = @watchers.sort_by do |w|
priority = w.options.fetch(:priority, 50)
n += 1
[priority, n]
end.reverse.freeze
handler.on_change(&method(:did_change))
if @running
handler.poll_once!
handler.listen!
end
handler
end
|
#watcher_for_path(types, path) ⇒ Object
216
217
218
|
# File 'lib/middleman-core/sources.rb', line 216
def watcher_for_path(types, path)
watchers.detect { |d| Array(types).include?(d.type) && d.exists?(path) }
end
|
#watchers ⇒ Object
148
149
150
|
# File 'lib/middleman-core/sources.rb', line 148
def watchers
@sorted_watchers
end
|