class Command

Attributes

args[RW]
cmd[RW]
known_options[RW]
options[RW]
unknown_options[RW]

Public Class Methods

new(args = []) click to toggle source
# File lib/pivotal-github/command.rb, line 8
def initialize(args = [])
  self.args = args
  self.options = OpenStruct.new
  parse
end
run!(command_class, args) click to toggle source

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

# 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

Public Instance Methods

message() click to toggle source

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

# 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_ids() click to toggle source

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’

# File lib/pivotal-github/command.rb, line 59
def message_ids
  @message_ids ||= story_ids.map { |id| "##{id}" }.join(' ')
end
parse() click to toggle source
# 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
parser() click to toggle source
# File lib/pivotal-github/command.rb, line 20
def parser
  OptionParser.new
end
story_branch() click to toggle source
# File lib/pivotal-github/command.rb, line 24
def story_branch
  @story_branch ||= `git rev-parse --abbrev-ref HEAD`.strip
end
story_id() click to toggle source

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

# File lib/pivotal-github/command.rb, line 39
def story_id
  story_ids.first
end
story_ids() click to toggle source

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.

# File lib/pivotal-github/command.rb, line 34
def story_ids
  story_branch.scan(/[0-9]{8,}/)
end

Private Instance Methods

argument_string(args) click to toggle source

Returns an argument string based on given arguments. The main trick is to add in quotes for option arguments when necessary. For example, [‘-a’, ‘-m’, ‘foo bar’] becomes ‘-a -m “foo bar”’

# File lib/pivotal-github/command.rb, line 85
def argument_string(args)
  args.inject([]) do |opts, opt|
    opts << (opt =~ /^-/ ? opt : opt.inspect)
  end.join(' ')
end
deliver?() click to toggle source
# File lib/pivotal-github/command.rb, line 95
def deliver?
  options.deliver
end
finish?() click to toggle source
# File lib/pivotal-github/command.rb, line 91
def finish?
  options.finish
end