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.



164
165
166
167
168
# File 'lib/sup/source.rb', line 164

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

Instance Method Details

#[](id) ⇒ Object



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

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

#add_source(source) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/sup/source.rb', line 174

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



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

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



206
207
208
209
210
211
212
213
# File 'lib/sup/source.rb', line 206

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



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

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

#sourcesObject



185
186
187
188
# File 'lib/sup/source.rb', line 185

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



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

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

#usual_sourcesObject



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

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