Class: Redwood::SourceManager
- Includes:
- Singleton
- Defined in:
- lib/sup/source.rb
Instance Method Summary collapse
- #[](id) ⇒ Object
- #add_source(source) ⇒ Object
-
#initialize ⇒ SourceManager
constructor
A new instance of SourceManager.
- #load_sources(fn = Redwood::SOURCE_FN) ⇒ Object
- #save_sources(fn = Redwood::SOURCE_FN, force = false) ⇒ Object
- #source_for(uri) ⇒ Object
- #sources ⇒ Object
- #unusual_sources ⇒ Object
- #usual_sources ⇒ Object
Methods included from Singleton
Constructor Details
#initialize ⇒ SourceManager
Returns a new instance of SourceManager.
192 193 194 195 196 |
# File 'lib/sup/source.rb', line 192 def initialize @sources = {} @sources_dirty = false @source_mutex = Monitor.new end |
Instance Method Details
#[](id) ⇒ Object
198 199 200 |
# File 'lib/sup/source.rb', line 198 def [](id) @source_mutex.synchronize { @sources[id] } end |
#add_source(source) ⇒ Object
202 203 204 205 206 207 208 209 210 211 |
# File 'lib/sup/source.rb', line 202 def add_source source @source_mutex.synchronize do raise "duplicate source!" if @sources.include? source @sources_dirty = true max = @sources.max_of { |id, s| s.is_a?(DraftLoader) || s.is_a?(SentLoader) ? 0 : id } source.id ||= (max || 0) + 1 ##source.id += 1 while @sources.member? source.id @sources[source.id] = source end end |
#load_sources(fn = Redwood::SOURCE_FN) ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/sup/source.rb', line 226 def load_sources fn=Redwood::SOURCE_FN source_array = Redwood::load_yaml_obj(fn) || [] @source_mutex.synchronize do @sources = Hash[*(source_array).map { |s| [s.id, s] }.flatten] @sources_dirty = false end end |
#save_sources(fn = Redwood::SOURCE_FN, force = false) ⇒ Object
234 235 236 237 238 239 240 241 |
# File 'lib/sup/source.rb', line 234 def save_sources fn=Redwood::SOURCE_FN, force=false @source_mutex.synchronize do if @sources_dirty || force Redwood::save_yaml_obj sources, fn, false, true end @sources_dirty = false end end |
#source_for(uri) ⇒ Object
218 219 220 221 |
# File 'lib/sup/source.rb', line 218 def source_for uri = Source.(uri) sources.find { |s| s.is_source_for? } end |
#sources ⇒ Object
213 214 215 216 |
# File 'lib/sup/source.rb', line 213 def sources ## favour the inbox by listing non-archived sources first @source_mutex.synchronize { @sources.values }.sort_by { |s| s.id }.partition { |s| !s.archived? }.flatten end |
#unusual_sources ⇒ Object
224 |
# File 'lib/sup/source.rb', line 224 def unusual_sources; sources.find_all { |s| !s.usual? }; end |
#usual_sources ⇒ Object
223 |
# File 'lib/sup/source.rb', line 223 def usual_sources; sources.find_all { |s| s.usual? }; end |