Class: Bananajour::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/bananajour/repository.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Repository

Returns a new instance of Repository.



12
13
14
# File 'lib/bananajour/repository.rb', line 12

def initialize(path)
  @path = Pathname(path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/bananajour/repository.rb', line 18

def path
  @path
end

Class Method Details

.for_name(name) ⇒ Object



6
7
8
# File 'lib/bananajour/repository.rb', line 6

def self.for_name(name)
  new(Bananajour.repositories_path.join(name + ".git"))
end

.html_id(name) ⇒ Object



9
10
11
# File 'lib/bananajour/repository.rb', line 9

def self.html_id(name)
  name.gsub(/[^A-Za-z-]+/, '').downcase
end

Instance Method Details

#==(other) ⇒ Object



15
16
17
# File 'lib/bananajour/repository.rb', line 15

def ==(other)
  other.respond_to?(:path) && self.path == other.path
end

#dirnameObject



32
33
34
# File 'lib/bananajour/repository.rb', line 32

def dirname
  path.split.last.to_s
end

#exist?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/bananajour/repository.rb', line 19

def exist?
  path.exist?
end

#grit_repoObject



44
45
46
# File 'lib/bananajour/repository.rb', line 44

def grit_repo
  @grit_repo ||= Grit::Repo.new(path)
end

#html_idObject



29
30
31
# File 'lib/bananajour/repository.rb', line 29

def html_id
  self.class.html_id(name)
end

#init!Object



22
23
24
25
# File 'lib/bananajour/repository.rb', line 22

def init!
  path.mkpath
  Dir.chdir(path) { `git init --bare` }
end

#nameObject



26
27
28
# File 'lib/bananajour/repository.rb', line 26

def name
  dirname.sub(".git",'')
end

#readme_fileObject



50
51
52
# File 'lib/bananajour/repository.rb', line 50

def readme_file
  grit_repo.tree.contents.find {|c| c.name =~ /readme/i}
end

#recent_commitsObject



47
48
49
# File 'lib/bananajour/repository.rb', line 47

def recent_commits
  @commits ||= grit_repo.commits(nil, 10)
end

#remove!Object



65
66
67
# File 'lib/bananajour/repository.rb', line 65

def remove!
  path.rmtree
end

#rendered_readmeObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bananajour/repository.rb', line 53

def rendered_readme
  case File.extname(readme_file.name)
  when /\.md/i, /\.markdown/i
    require 'rdiscount'
    RDiscount.new(readme_file.data).to_html
  when /\.textile/i
    require 'redcloth'
    RedCloth.new(readme_file.data).to_html(:textile)
  end
rescue LoadError
  ""
end

#to_hashObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bananajour/repository.rb', line 68

def to_hash
  heads = grit_repo.heads
  {
    "name" => name,
    "html_friendly_name" => html_id, # TODO: Deprecate in v3. Renamed to html_id since 2.1.4
    "html_id" => html_id,
    "uri" => uri,
    "heads" => heads.map {|h| h.name},
    "recent_commits" => recent_commits.collect do |c|
      c.to_hash.merge(
        "head" => (head = heads.find {|h| h.commit == c}) && head.name,
        "gravatar" => c.author.gravatar_uri
      )
    end,
    "bananajour" => Bananajour.to_hash
  }
end

#to_sObject



35
36
37
# File 'lib/bananajour/repository.rb', line 35

def to_s
  name
end

#uriObject



38
39
40
# File 'lib/bananajour/repository.rb', line 38

def uri
  Bananajour.git_uri + dirname
end

#web_uriObject



41
42
43
# File 'lib/bananajour/repository.rb', line 41

def web_uri
  Bananajour.web_uri + "#" + html_id
end