Class: GitCommitStory

Inherits:
Object
  • Object
show all
Defined in:
lib/git-commit-story.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ GitCommitStory

Returns a new instance of GitCommitStory.



7
8
9
10
11
12
# File 'lib/git-commit-story.rb', line 7

def initialize(options)
  @config_file_name    = ".git-commit-story.yml"
  config_file_options = options_from_config_file
  config_file_options.merge!(options)
  @options = config_file_options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/git-commit-story.rb', line 5

def options
  @options
end

Instance Method Details

#commitObject



78
79
80
81
82
83
# File 'lib/git-commit-story.rb', line 78

def commit
  message = %Q(git commit -m "#{final_commit_message}")
  result = system(message)
  options[:previous_story_id] = story_id
  save_config_file
end

#final_commit_messageObject



58
59
60
61
62
63
64
65
66
# File 'lib/git-commit-story.rb', line 58

def final_commit_message
  message = ""
  if options[:commit_message]
    message = options[:commit_message]
  else
    message = get_message_from_prompt
  end
  "#{message}\n\nStory: #{story_id}"
end

#get_message_from_promptObject



68
69
70
71
72
73
74
75
76
# File 'lib/git-commit-story.rb', line 68

def get_message_from_prompt
  $stdout.print "Enter a commit message: "
  message = $stdin.gets
  if message.length == 0
    puts "No message supplied"
    abort
  end
  message.strip
end

#options_from_config_fileObject



14
15
16
17
18
19
20
# File 'lib/git-commit-story.rb', line 14

def options_from_config_file
  if File.exist?(@config_file_name)
    config_file_options = YAML.load_file(@config_file_name)
  else
    {}
  end
end

#save_config_fileObject



22
23
24
# File 'lib/git-commit-story.rb', line 22

def save_config_file
  File.open(@config_file_name, "w") { |f| f.puts @options.to_yaml }
end

#story_idObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/git-commit-story.rb', line 26

def story_id
  if @options[:story_id]
    strip_leading_pound @options[:story_id]
  else
    prompt = if @options[:previous_story_id]
      "Enter a story [#{@options[:previous_story_id]}]: "
    else
      "Enter a story: "
    end
    $stdout.print prompt
    response = $stdin.gets.strip
    if response == ""
      if @options[:previous_story_id]
        @options[:story_id] = @options[:previous_story_id]
      else
        puts "No story id was supplied"
        abort
      end
    else
      @options[:story_id] = strip_leading_pound response
    end
  end
end

#strip_leading_pound(string) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/git-commit-story.rb', line 50

def strip_leading_pound(string)
  if string[0] == "#"
    string[1..-1]
  else
    string
  end
end