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) ⇒ 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.
172 173 174 175 176 |
# File 'lib/sup/source.rb', line 172 def initialize @sources = {} @sources_dirty = false @source_mutex = Monitor.new end |
Instance Method Details
#[](id) ⇒ Object
178 179 180 |
# File 'lib/sup/source.rb', line 178 def [](id) @source_mutex.synchronize { @sources[id] } end |
#add_source(source) ⇒ Object
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/sup/source.rb', line 182 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
206 207 208 209 210 211 212 |
# File 'lib/sup/source.rb', line 206 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) ⇒ Object
214 215 216 217 218 219 220 221 |
# File 'lib/sup/source.rb', line 214 def save_sources fn=Redwood::SOURCE_FN @source_mutex.synchronize do if @sources_dirty Redwood::save_yaml_obj sources, fn, false, true end @sources_dirty = false end end |
#source_for(uri) ⇒ Object
198 199 200 201 |
# File 'lib/sup/source.rb', line 198 def source_for uri = Source.(uri) sources.find { |s| s.is_source_for? } end |
#sources ⇒ Object
193 194 195 196 |
# File 'lib/sup/source.rb', line 193 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
204 |
# File 'lib/sup/source.rb', line 204 def unusual_sources; sources.find_all { |s| !s.usual? }; end |
#usual_sources ⇒ Object
203 |
# File 'lib/sup/source.rb', line 203 def usual_sources; sources.find_all { |s| s.usual? }; end |