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.



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

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.for_name(name) ⇒ Object



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

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

.html_id(name) ⇒ Object



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

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

Instance Method Details

#==(other) ⇒ Object



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

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

#dirnameObject



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

def dirname
  path.split.last.to_s
end

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  path.exists?
end

#grit_repoObject



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

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

#html_idObject



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

def html_id
  self.class.html_id(name)
end

#init!Object



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

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

#nameObject



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

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

#readme_fileObject



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

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

#recent_commitsObject



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

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

#remove!Object



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

def remove!
  path.rmtree
end

#rendered_readmeObject



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

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



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

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



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

def to_s
  name
end

#uriObject



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

def uri
  Bananajour.git_uri + dirname
end

#web_uriObject



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

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