Class: Repositories

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/commands/plugin.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_file = File.join(find_home, ".rails-plugin-sources")) ⇒ Repositories

Returns a new instance of Repositories.



316
317
318
319
# File 'lib/commands/plugin.rb', line 316

def initialize(cache_file = File.join(find_home, ".rails-plugin-sources"))
  @cache_file = File.expand_path(cache_file)
  load!
end

Class Method Details

.each(&block) ⇒ Object



397
398
399
# File 'lib/commands/plugin.rb', line 397

def self.each(&block)
  self.instance.each(&block)
end

.instanceObject



393
394
395
# File 'lib/commands/plugin.rb', line 393

def self.instance
  @instance ||= Repositories.new
end

Instance Method Details

#add(uri) ⇒ Object



325
326
327
328
329
# File 'lib/commands/plugin.rb', line 325

def add(uri)
  unless find{|repo| repo.uri == uri }
    @repositories.push(Repository.new(uri)).last
  end
end

#allObject



339
340
341
# File 'lib/commands/plugin.rb', line 339

def all
  @repositories
end

#defaultsObject



369
370
371
372
373
# File 'lib/commands/plugin.rb', line 369

def defaults
  <<-DEFAULTS
  http://dev.rubyonrails.com/svn/rails/plugins/
  DEFAULTS
end

#each(&block) ⇒ Object



321
322
323
# File 'lib/commands/plugin.rb', line 321

def each(&block)
  @repositories.each(&block)
end

#exist?(uri) ⇒ Boolean

Returns:

  • (Boolean)


335
336
337
# File 'lib/commands/plugin.rb', line 335

def exist?(uri)
  @repositories.detect{|repo| repo.uri == uri }
end

#find_homeObject



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/commands/plugin.rb', line 375

def find_home
  ['HOME', 'USERPROFILE'].each do |homekey|
    return ENV[homekey] if ENV[homekey]
  end
  if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
    return "#{ENV['HOMEDRIVE']}:#{ENV['HOMEPATH']}"
  end
  begin
    File.expand_path("~")
  rescue StandardError => ex
    if File::ALT_SEPARATOR
      "C:/"
    else
      "/"
    end
  end
end

#find_plugin(name) ⇒ Object



343
344
345
346
347
348
349
350
# File 'lib/commands/plugin.rb', line 343

def find_plugin(name)
  @repositories.each do |repo|
    repo.each do |plugin|
      return plugin if plugin.name == name
    end
  end
  return nil
end

#load!Object



352
353
354
355
356
357
358
# File 'lib/commands/plugin.rb', line 352

def load!
  contents = File.exist?(@cache_file) ? File.read(@cache_file) : defaults
  contents = defaults if contents.empty?
  @repositories = contents.split(/\n/).reject do |line|
    line =~ /^\s*#/ or line =~ /^\s*$/
  end.map { |source| Repository.new(source.strip) }
end

#remove(uri) ⇒ Object



331
332
333
# File 'lib/commands/plugin.rb', line 331

def remove(uri)
  @repositories.reject!{|repo| repo.uri == uri}
end

#saveObject



360
361
362
363
364
365
366
367
# File 'lib/commands/plugin.rb', line 360

def save
  File.open(@cache_file, 'w') do |f|
    each do |repo|
      f.write(repo.uri)
      f.write("\n")
    end
  end
end