Class: Redwood::SourceManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sup/source.rb

Instance Method Summary collapse

Methods included from Singleton

included

Constructor Details

#initializeSourceManager

Returns a new instance of SourceManager.



198
199
200
201
202
# File 'lib/sup/source.rb', line 198

def initialize
  @sources = {}
  @sources_dirty = false
  @source_mutex = Monitor.new
end

Instance Method Details

#[](id) ⇒ Object



204
205
206
# File 'lib/sup/source.rb', line 204

def [](id)
  @source_mutex.synchronize { @sources[id] }
end

#add_source(source) ⇒ Object



208
209
210
211
212
213
214
215
216
217
# File 'lib/sup/source.rb', line 208

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



232
233
234
235
236
237
238
# File 'lib/sup/source.rb', line 232

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



240
241
242
243
244
245
246
247
# File 'lib/sup/source.rb', line 240

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



224
225
226
227
# File 'lib/sup/source.rb', line 224

def source_for uri
  expanded_uri = Source.expand_filesystem_uri(uri)
  sources.find { |s| s.is_source_for? expanded_uri }
end

#sourcesObject



219
220
221
222
# File 'lib/sup/source.rb', line 219

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_sourcesObject



230
# File 'lib/sup/source.rb', line 230

def unusual_sources; sources.find_all { |s| !s.usual? }; end

#usual_sourcesObject



229
# File 'lib/sup/source.rb', line 229

def usual_sources; sources.find_all { |s| s.usual? }; end