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.



298
299
300
301
# File 'lib/commands/plugin.rb', line 298

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



379
380
381
# File 'lib/commands/plugin.rb', line 379

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

.instanceObject



375
376
377
# File 'lib/commands/plugin.rb', line 375

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

Instance Method Details

#add(uri) ⇒ Object



307
308
309
310
311
# File 'lib/commands/plugin.rb', line 307

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

#allObject



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

def all
  @repositories
end

#defaultsObject



351
352
353
354
355
# File 'lib/commands/plugin.rb', line 351

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

#each(&block) ⇒ Object



303
304
305
# File 'lib/commands/plugin.rb', line 303

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

#exist?(uri) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#find_homeObject



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/commands/plugin.rb', line 357

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



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

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



334
335
336
337
338
339
340
# File 'lib/commands/plugin.rb', line 334

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



313
314
315
# File 'lib/commands/plugin.rb', line 313

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

#saveObject



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

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