Class: Saga::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/saga/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



5
6
7
8
# File 'lib/saga/runner.rb', line 5

def initialize(argv)
  @argv = argv
  @options = {}
end

Class Method Details

.authorObject



97
98
99
100
# File 'lib/saga/runner.rb', line 97

def self.author
  name = `osascript -e "long user name of (system info)" &1> /dev/null`.strip
  {:name => name}
end

Instance Method Details

#autofill(filename) ⇒ Object



60
61
62
63
64
# File 'lib/saga/runner.rb', line 60

def autofill(filename)
  document = Saga::Parser.parse(File.read(filename))
  document.autofill_ids
  Saga::Formatter.format(document, :template => 'saga')
end

#convert(filename) ⇒ Object



46
47
48
# File 'lib/saga/runner.rb', line 46

def convert(filename)
  Saga::Formatter.format(Saga::Parser.parse(File.read(filename)))
end

#new_fileObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/saga/runner.rb', line 29

def new_file
  document = Saga::Document.new
  document.title = 'Title'
  document.authors << self.class.author
  document.stories[''] = [{
    :description => 'As a writer I would like to write stories so developers can implement them.',
    :id => 1,
    :status => 'todo'
  }]
  document.definitions[''] = [{
    :title => 'Writer',
    :definition => 'Someone who is responsible for writing down requirements in the form of stories'
  }]
  
  Saga::Formatter.format(document, :template => 'saga')
end

#parserObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/saga/runner.rb', line 10

def parser
  @parser ||= OptionParser.new do |opts|
    opts.banner =  "Usage: saga [command]"
    opts.separator ""
    opts.separator "Commands:"
    opts.separator "    new                 - prints a blank stub"
    opts.separator "    convert <filename>  - convert the stories to HTML"
    opts.separator "    inspect <filename>  - print the internals of the document"
    opts.separator "    autofill <filename> - adds an id to stories without one"
    opts.separator "    planning <filename> - shows the planning of stories in iterations"
    opts.separator ""
    opts.separator "Options:"
    opts.on("-h", "--help", "Show help") do
      puts opts
      exit
    end
  end
end

#planning(filename) ⇒ Object



66
67
68
# File 'lib/saga/runner.rb', line 66

def planning(filename)
  Saga::Planning.new(Saga::Parser.parse(File.read(filename))).to_s
end

#runObject



87
88
89
90
91
92
93
94
95
# File 'lib/saga/runner.rb', line 87

def run
  argv = @argv.dup
  parser.parse!(argv)
  if command = argv.shift
    run_command(command, @options)
  else
    puts parser.to_s
  end
end

#run_command(command, options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/saga/runner.rb', line 70

def run_command(command, options)
  case command
  when 'new'
    puts new_file
  when 'convert'
    puts convert(File.expand_path(@argv[1]))
  when 'inspect'
    write_parsed_document(File.expand_path(@argv[1]))
  when 'autofill'
    puts autofill(File.expand_path(@argv[1]))
  when 'planning'
    puts planning(File.expand_path(@argv[1]))
  else
    puts convert(File.expand_path(command))
  end
end

#write_parsed_document(filename) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/saga/runner.rb', line 50

def write_parsed_document(filename)
  document = Saga::Parser.parse(File.read(filename))
  puts document.title
  document.authors.each { |author| p author }
  puts
  document.stories.each { |header, stories| puts header; stories.each { |story| p story } }
  puts
  document.definitions.each { |header, definitions| puts header; definitions.each { |definition| p definition } }
end