Class: Peacock::Engine::Ignorer

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Engine

#check_and_return_hash, #determine_git_ignore_path, #determine_root_dir, #git_ignore_exists?

Constructor Details

#initialize(opt_hash) ⇒ Ignorer

Returns a new instance of Ignorer.



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

def initialize(opt_hash)
  @hash = check_and_return_hash(opt_hash)
  @logger = Peacock::Logger.new(@hash.silent?)
  path = determine_git_ignore_path
  @git_ignore = File.open(path, 'a+')
end

Class Method Details

.start_engine(opt_hash) ⇒ Object



8
9
10
11
# File 'lib/peacock/engine/ignorer.rb', line 8

def self.start_engine(opt_hash)
  ignorer = Ignorer.new(opt_hash)
  ignorer.workflow
end

Instance Method Details

#check_and_write(str) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/peacock/engine/ignorer.rb', line 42

def check_and_write(str)
  unless entry_exists?(str)
    insert_in_gitignore(str)
  end

  @git_ignore.rewind
end

#entry_exists?(entry) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
# File 'lib/peacock/engine/ignorer.rb', line 50

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

#ignore_directoriesObject



30
31
32
33
34
# File 'lib/peacock/engine/ignorer.rb', line 30

def ignore_directories
  @hash.dirs.each do |dir|
    check_and_write(dir)
  end
end

#ignore_filesObject



36
37
38
39
40
# File 'lib/peacock/engine/ignorer.rb', line 36

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

#ignore_files_and_directoriesObject



25
26
27
28
# File 'lib/peacock/engine/ignorer.rb', line 25

def ignore_files_and_directories
  ignore_files
  ignore_directories
end

#insert_in_gitignore(str) ⇒ Object



57
58
59
60
61
# File 'lib/peacock/engine/ignorer.rb', line 57

def insert_in_gitignore(str)
  @git_ignore.write(str + "\n")
  Git.remove_from_cache(prepare_arg(str))
  @logger.ignore(str)
end

#prepare_arg(str) ⇒ Object

if directory then remove leading slash



64
65
66
67
68
69
70
# File 'lib/peacock/engine/ignorer.rb', line 64

def prepare_arg(str)
  if str.start_with?('/')
    str[1..-1]
  else
    str
  end
end

#workflowObject



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

def workflow
  ignore_files_and_directories
  @git_ignore.close
end