Class: Pickler::Runner::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pickler/runner.rb', line 9

def initialize(argv)
  @argv = argv
  @tty = $stdout.tty?
  @opts = OptionParser.new
  @opts.version = "0.0"
  @opts.banner = "Usage: pickler #{self.class.command_name} #{self.class.banner_arguments}"
  @opts.base.long["help"] = OptionParser::Switch::NoArgument.new do
    help = @opts.help.chomp.chomp + "\n"
    help += "\n#{self.class.description}" if self.class.description
    puts help
    @exit = 0
  end
  @opts.separator("")
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



7
8
9
# File 'lib/pickler/runner.rb', line 7

def argv
  @argv
end

Class Method Details

.arityObject



69
70
71
# File 'lib/pickler/runner.rb', line 69

def self.arity
  instance_method(:process).arity
end


33
34
35
36
37
38
39
# File 'lib/pickler/runner.rb', line 33

def self.banner_arguments(value = nil)
  if value
    @banner_arguments = value
  else
    @banner_arguments || (arity.zero? ? "" : "...")
  end
end

.command_nameObject



57
58
59
# File 'lib/pickler/runner.rb', line 57

def self.command_name
  name.split('::').last.gsub(/(.)([A-Z])/) {"#$1-#$2"}.downcase
end

.description(value = nil) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/pickler/runner.rb', line 49

def self.description(value = nil)
  if value
    @description = value
  else
    @description || "#@summary."
  end
end

.method_nameObject



61
62
63
# File 'lib/pickler/runner.rb', line 61

def self.method_name
  command_name.gsub('-','_')
end

.on(*args, &block) ⇒ Object



28
29
30
31
# File 'lib/pickler/runner.rb', line 28

def self.on(*args, &block)
  options << args
  define_method("option_#{args.object_id}", &block)
end

.optionsObject



24
25
26
# File 'lib/pickler/runner.rb', line 24

def self.options
  @options ||= []
end

.process(&block) ⇒ Object



65
66
67
# File 'lib/pickler/runner.rb', line 65

def self.process(&block)
  define_method(:process, &block)
end

.summary(value = nil) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/pickler/runner.rb', line 41

def self.summary(value = nil)
  if value
    @summary = value
  else
    @summary
  end
end

Instance Method Details

#abort(message) ⇒ Object

Raises:



81
82
83
# File 'lib/pickler/runner.rb', line 81

def abort(message)
  raise Error, message
end

#arityObject



73
74
75
# File 'lib/pickler/runner.rb', line 73

def arity
  self.class.arity
end

#color?Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
# File 'lib/pickler/runner.rb', line 112

def color?
  case pickler.config["color"]
  when "always" then true
  when "never"  then false
  else
    @tty && RUBY_PLATFORM !~ /mswin|mingw/
  end
end

#colorize(code, string) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/pickler/runner.rb', line 121

def colorize(code, string)
  if color?
    "\e[#{code}m#{string}\e[00m"
  else
    string.to_s
  end
end

#paginated_outputObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/pickler/runner.rb', line 163

def paginated_output
  stdout = $stdout
  if @tty && pager = pickler.config["pager"]
    # Modeled after git
    ENV["LESS"] ||= "FRSX"
    IO.popen(pager,"w") do |io|
      $stdout = io
      yield
    end
  else
    yield
  end
rescue Errno::EPIPE
ensure
  $stdout = stdout
end

#picklerObject



77
78
79
# File 'lib/pickler/runner.rb', line 77

def pickler
  @pickler ||= Pickler.new(Dir.getwd)
end

#process(*argv) ⇒ Object



108
109
110
# File 'lib/pickler/runner.rb', line 108

def process(*argv)
  pickler.send(self.class.method_name,*argv)
end

#puts_full(story) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/pickler/runner.rb', line 139

def puts_full(story)
  puts colorize("01;3#{TYPE_COLORS[story.story_type]}", story.name)
  puts "Type:      #{story.story_type}".rstrip
  if story.story_type == "release"
    puts "Deadline:  #{story.deadline}".rstrip
  else
    puts "Estimate:  #{story.estimate}".rstrip
  end
  puts "State:     #{story.current_state}".rstrip
  puts "Labels:    #{story.labels.join(', ')}".rstrip
  puts "Requester: #{story.requested_by}".rstrip
  puts "Owner:     #{story.owned_by}".rstrip
  puts "URL:       #{story.url}".rstrip
  puts unless story.description =~ /^\s*$/
  story.description_lines.each do |line|
    puts "  #{line}".rstrip
  end
  story.notes.each do |note|
    puts
    puts "  #{colorize('01', note.author)} (#{note.date})"
    puts(*note.lines(72).map {|l| "    #{l}".rstrip})
  end
end

#puts_summary(story) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/pickler/runner.rb', line 129

def puts_summary(story)
  summary = "%6d " % story.id
  type  = story.estimate || TYPE_SYMBOLS[story.story_type]
  state = STATE_SYMBOLS[story.current_state]
  summary << colorize("3#{STATE_COLORS[story.current_state]}", state) << ' '
  summary << colorize("01;3#{TYPE_COLORS[story.story_type]}", type) << ' '
  summary << story.name
  puts summary
end

#runObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pickler/runner.rb', line 89

def run
  self.class.options.each do |arguments|
    @opts.on(*arguments, &method("option_#{arguments.object_id}"))
  end
  begin
    @opts.parse!(@argv)
  rescue OptionParser::InvalidOption
    abort $!.message
  end
  return @exit if @exit
  minimum = arity < 0 ? -1 - arity : arity
  if arity >= 0 && arity < @argv.size
    too_many
  elsif minimum > @argv.size
    abort "not enough arguments"
  end
  process(*@argv)
end

#too_manyObject



85
86
87
# File 'lib/pickler/runner.rb', line 85

def too_many
  abort "too many arguments"
end