Class: Command

Inherits:
Object
  • Object
show all
Defined in:
lib/pivotal-github/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ Command

Returns a new instance of Command.



8
9
10
11
12
# File 'lib/pivotal-github/command.rb', line 8

def initialize(args = [])
  self.args = args
  self.options = OpenStruct.new
  parse
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



6
7
8
# File 'lib/pivotal-github/command.rb', line 6

def args
  @args
end

#cmdObject

Returns the value of attribute cmd.



6
7
8
# File 'lib/pivotal-github/command.rb', line 6

def cmd
  @cmd
end

#known_optionsObject

Returns the value of attribute known_options.



6
7
8
# File 'lib/pivotal-github/command.rb', line 6

def known_options
  @known_options
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/pivotal-github/command.rb', line 6

def options
  @options
end

#unknown_optionsObject

Returns the value of attribute unknown_options.



6
7
8
# File 'lib/pivotal-github/command.rb', line 6

def unknown_options
  @unknown_options
end

Class Method Details

.run!(command_class, args) ⇒ Object

Runs a command. If the argument array contains ‘–debug’, returns the command that would have been run.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pivotal-github/command.rb', line 66

def self.run!(command_class, args)
  debug = args.delete('--debug')
  command = command_class.new(args)
  if debug
    puts command.cmd
    return 1
  else
    command.run!
    return 0
  end
end

Instance Method Details

#messageObject

Returns the message for the story id(s) and action (if any).



44
45
46
47
48
49
50
51
52
53
# File 'lib/pivotal-github/command.rb', line 44

def message
  if finish?
    label = "Finishes #{message_ids}"
  elsif deliver?
    label = "Delivers #{message_ids}"
  else
    label = message_ids
  end
  "[#{label}]"
end

#message_idsObject

Returns the story ids formatted for story commits. For single-id stories, this is just the number preceded by ‘#’, as in ‘#62831853’. For multiple-id stories, each story id is precede by ‘#’, as in ‘#62831853 #31415926’



59
60
61
# File 'lib/pivotal-github/command.rb', line 59

def message_ids
  @message_ids ||= story_ids.map { |id| "##{id}" }.join(' ')
end

#parseObject



14
15
16
17
18
# File 'lib/pivotal-github/command.rb', line 14

def parse
  self.known_options   = Options::known_options(parser, args)
  self.unknown_options = Options::unknown_options(parser, args)
  parser.parse!(known_options)
end

#parserObject



20
21
22
# File 'lib/pivotal-github/command.rb', line 20

def parser
  OptionParser.new
end

#story_branchObject



24
25
26
# File 'lib/pivotal-github/command.rb', line 24

def story_branch
  @story_branch ||= `git rev-parse --abbrev-ref HEAD`.strip
end

#story_idObject

Returns the single story id for the common case of one id.



39
40
41
# File 'lib/pivotal-github/command.rb', line 39

def story_id
  story_ids.first
end

#story_idsObject

Returns the story id (or ids). We extract the story id(s) from the branch name, so that, e.g., the branch ‘add-markdown-support-62831853` gives story_id ’62831853’. New as of version 0.7, we support multiple story ids in a single branch name, so that ‘add-markdown-support-62831853-31415926` can be used to update story 62831853 and story 31415926 simultaneously.



34
35
36
# File 'lib/pivotal-github/command.rb', line 34

def story_ids
  story_branch.scan(/[0-9]{8,}/)
end