Class: Ginatra::RepoList

Inherits:
Object
  • Object
show all
Includes:
Logger, Singleton
Defined in:
lib/ginatra/repo_list.rb

Overview

A singleton class that lets us make and use a constantly updating list of repositories.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger, logger

Constructor Details

#initializeRepoList

This creates the list, then does the first refresh to populate it.

It returns what refresh returns.



15
16
17
18
# File 'lib/ginatra/repo_list.rb', line 15

def initialize
  self.list = []
  self.refresh
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



9
10
11
# File 'lib/ginatra/repo_list.rb', line 9

def list
  @list
end

Class Method Details

.find(local_param) ⇒ Object

This just brings up the find method to the class scope.

See Also:



95
96
97
# File 'lib/ginatra/repo_list.rb', line 95

def self.find(local_param)
  self.instance.find(local_param)
end

.listArray<Ginatra::Repo>

The preferred way to access the list publicly.

Returns:



23
24
25
26
# File 'lib/ginatra/repo_list.rb', line 23

def self.list
  self.instance.refresh
  self.instance.list
end

.method_missing(sym, *args, &block) ⇒ Object

allows missing methods to cascade to the instance,



100
101
102
# File 'lib/ginatra/repo_list.rb', line 100

def self.method_missing(sym, *args, &block)
  instance.send(sym, *args, &block)
end

.respond_to?(sym) ⇒ Boolean

updated to correspond to the method_missing definition

Returns:

  • (Boolean)


105
106
107
# File 'lib/ginatra/repo_list.rb', line 105

def self.respond_to?(sym)
  instance.respond_to?(sym) || super
end

Instance Method Details

#add(path, param = File.split(path).last) ⇒ Object

adds a Repo corresponding to the path it found a git repo at in the configured globs. Checks to see that it’s not there first

Parameters:

  • path (String)

    the path of the git repo

  • param (String) (defaults to: File.split(path).last)

    the param of the repo if it differs, for looking to see if it’s already on the list



54
55
56
57
58
59
60
61
62
63
# File 'lib/ginatra/repo_list.rb', line 54

def add(path, param=File.split(path).last)
  unless self.has_repo?(param)
    begin
      list << Repo.new(path)
    rescue Rugged::RepositoryError
      logger.warn "SKIPPING '#{path}' - not a git repository"
    end
  end
  list
end

#find(local_param) ⇒ Ginatra::Repo

quick way to look up if there is a repo with a given param in the list. If not, it refreshes the list and tries again.

Parameters:

  • local_param (String)

    the param to lookup

Returns:

  • (Ginatra::Repo)

    the repository corresponding to that param.



81
82
83
84
85
86
87
88
89
90
# File 'lib/ginatra/repo_list.rb', line 81

def find(local_param)
  if repo = list.find { |r| r.param == local_param }
    repo
  else
    refresh
    repo = list.find { |r| r.param == local_param }
    raise Ginatra::RepoNotFound if repo.nil?
    repo
  end
end

#has_repo?(local_param) ⇒ true, false

checks to see if the list contains a repo with a param matching the one passed in.

Parameters:

  • local_param (String)

    param to check.

Returns:

  • (true, false)


71
72
73
# File 'lib/ginatra/repo_list.rb', line 71

def has_repo?(local_param)
  !list.find { |r| r.param == local_param }.nil?
end

#refreshObject

searches through the configured directory globs to find all the repositories and adds them if they’re not already there.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ginatra/repo_list.rb', line 30

def refresh
  list.clear

  Ginatra.load_config["git_dirs"].map do |git_dir|
    if Dir.exist?(git_dir.chop)
      dirs = Dir.glob(git_dir).sort
    else
      dir = File.expand_path("../../../#{git_dir}", __FILE__)
      dirs = Dir.glob(dir).sort
    end

    dirs = dirs.select {|f| File.directory? f }
    dirs.each {|d| add(d) }
  end

  list
end