Class: Avm::Git::AutoCommitPath

Inherits:
Object
  • Object
show all
Defined in:
lib/avm/git/auto_commit_path.rb

Constant Summary collapse

CLASS_NAME_PATTERNS =
[%r{lib/(.+)\.rb\z}, %r{app/[^/]+/(.+)\.rb\z}].freeze

Instance Method Summary collapse

Instance Method Details



18
19
20
21
22
# File 'lib/avm/git/auto_commit_path.rb', line 18

def banner
  infom "Checking \"#{relative_path}\""
  infov '  * Class name', class_name
  infov '  * Commit message', commit_message
end

#class_nameObject



31
32
33
34
35
36
# File 'lib/avm/git/auto_commit_path.rb', line 31

def class_name
  CLASS_NAME_PATTERNS.each do |pattern|
    pattern.if_match(relative_path.to_path, false) { |m| return m[1].camelize }
  end
  raise "No pattern matched \"#{relative_path}\""
end

#commitObject



24
25
26
27
28
29
# File 'lib/avm/git/auto_commit_path.rb', line 24

def commit
  infom '  * Commiting...'
  git.system!('reset', 'HEAD')
  git.system!('add', '--', relative_path.to_path)
  git.system!('commit', '-m', commit_message, '--', relative_path.to_path)
end

#commit_messageObject



38
39
40
41
42
# File 'lib/avm/git/auto_commit_path.rb', line 38

def commit_message
  r = class_name
  r += ': remove' unless path.file?
  r + '.'
end

#relative_pathObject



44
45
46
# File 'lib/avm/git/auto_commit_path.rb', line 44

def relative_path
  path.relative_path_from(git.root_path)
end

#runObject



13
14
15
16
# File 'lib/avm/git/auto_commit_path.rb', line 13

def run
  banner
  commit
end