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.



170
171
172
173
174
# File 'lib/sup/source.rb', line 170

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

Instance Method Details

#[](id) ⇒ Object



176
177
178
# File 'lib/sup/source.rb', line 176

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

#add_source(source) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/sup/source.rb', line 180

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



204
205
206
207
208
209
210
# File 'lib/sup/source.rb', line 204

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



212
213
214
215
216
217
218
219
# File 'lib/sup/source.rb', line 212

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



196
197
198
199
# File 'lib/sup/source.rb', line 196

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

#sourcesObject



191
192
193
194
# File 'lib/sup/source.rb', line 191

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



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

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

#usual_sourcesObject



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

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