Class: Peacock::Ignorer

Inherits:
Object
  • Object
show all
Defined in:
lib/peacock/ignorer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt_hash) ⇒ Ignorer

Returns a new instance of Ignorer.



13
14
15
16
17
# File 'lib/peacock/ignorer.rb', line 13

def initialize(opt_hash)
  @hash = opt_hash
  @git_ignore = File.open('.gitignore', 'a+')
  @repo = Git.open Dir.pwd
end

Class Method Details

.ignore(opt_hash) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/peacock/ignorer.rb', line 5

def self.ignore(opt_hash)
  ignorer = Ignorer.new(opt_hash)
  ignorer.commit_all('peacock: before .gitignore commit')
  ignorer.ignore_files_and_directories
  ignorer.clear_cache
  ignorer.commit_all('peacock: after .gitignore commit')
end

Instance Method Details

#check_and_write(str) ⇒ Object



53
54
55
56
# File 'lib/peacock/ignorer.rb', line 53

def check_and_write(str)
  @git_ignore.write(str + "\n")  unless entry_exists?(str)
  @git_ignore.rewind
end

#clear_cacheObject



19
20
21
22
23
24
# File 'lib/peacock/ignorer.rb', line 19

def clear_cache
  # totally ugly, but ruby-git does not support git rm -r --cached
  Open3.popen3('git rm -r --cached .') do |stdin, stdout, stderr, thread|
    # ignore output
  end
end

#commit_all(message) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/peacock/ignorer.rb', line 26

def commit_all(message)
  begin
    @repo.add(all: true) 
    @repo.commit_all(message)
  rescue
    # do nothing
  end
end

#entry_exists?(entry) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/peacock/ignorer.rb', line 58

def entry_exists?(entry)
  @git_ignore.each do |line|
    return true if line.chomp == entry
  end
  false
end

#ignore_directoriesObject



40
41
42
43
44
45
# File 'lib/peacock/ignorer.rb', line 40

def ignore_directories
  @hash[:dirs].each do |dir|
    dir = dir + '/' unless dir =~ /\/$/  # add backlash to dir name if it does not exist yet
    check_and_write(dir)
  end
end

#ignore_filesObject



47
48
49
50
51
# File 'lib/peacock/ignorer.rb', line 47

def ignore_files
  @hash[:files].each do |file|
    check_and_write(file)
  end
end

#ignore_files_and_directoriesObject



35
36
37
38
# File 'lib/peacock/ignorer.rb', line 35

def ignore_files_and_directories
  ignore_files
  ignore_directories
end