Module: Cmdapp
- Defined in:
- lib/todorb/common/cmdapp.rb
Instance Method Summary collapse
- #_backup(filename = @app_file_path) ⇒ Object
-
#_get_serial_number ⇒ Object
reads serial_number file, returns serialno for this app and increments the serial number and writes back.
-
#_list_args(args) ⇒ Array
separates args to list-like operations +xxx means xxx should match in output -xxx means xxx should not exist in output that should not match.
-
#_set_serial_number(number) ⇒ Object
After doing a redo of the numbering, we need to reset the numbers for that app.
- #add_action(name, descr) ⇒ Object
- #alias_command(name, *args) ⇒ Object
-
#check_aliases(action, args) ⇒ Boolean
external dependencies: @app_default_action - action to run if none specified @app_file_path - data file we are backing up, or reading into array @app_serial_path - serial_number file.
- #die(text) ⇒ Object
-
#filter_rows(rows, incl) ⇒ Object
creates a regexp and for each row returns the row and the regexp you can use the regexp on whatever part of the row you want to match or reject.
-
#help(args) ⇒ Object
not required if using Subcommand.
-
#load_array ⇒ Object
load data into array as item and task.
-
#message(text) ⇒ Object
prints messages to stderr All messages should go to stderr.
- #print_green(text) ⇒ Object
- #print_red(text) ⇒ Object
-
#run ⇒ 0, ERRCODE
runs method after checking if valid or alias.
-
#save_array ⇒ Object
saves the task array to disk Please use load_array to load, and not populate.
-
#verbose(text) ⇒ Object
print to stderr only if verbose set.
-
#version_info ⇒ String?
retrieve version info updated by jeweler.
-
#warning(text) ⇒ Object
print to stderr only if verbose set.
Instance Method Details
#_backup(filename = @app_file_path) ⇒ Object
140 141 142 143 |
# File 'lib/todorb/common/cmdapp.rb', line 140 def _backup filename=@app_file_path require 'fileutils' FileUtils.cp filename, "#{filename}.org" end |
#_get_serial_number ⇒ Object
reads serial_number file, returns serialno for this app and increments the serial number and writes back.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/todorb/common/cmdapp.rb', line 102 def _get_serial_number require 'fileutils' appname = @appname filename = @app_serial_path || "serial_numbers" h = {} # check if serial file existing in curr dir. Else create if File.exists?(filename) File.open(filename).each { |line| #sn = $1 if line.match regex x = line.split ":" h[x[0]] = x[1].chomp } end sn = h[appname] || 1 # update the sn in file nsn = sn.to_i + 1 # this will create if not exists in addition to storing if it does h[appname] = nsn # write back to file File.open(filename, "w") do |f| h.each_pair {|k,v| f.print "#{k}:#{v}\n"} end return sn end |
#_list_args(args) ⇒ Array
separates args to list-like operations +xxx means xxx should match in output -xxx means xxx should not exist in output that should not match.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/todorb/common/cmdapp.rb', line 218 def _list_args args incl = [] excl = [] args.each do |e| if e[0] == '+' incl << e[1..-1] elsif e[0] == '-' excl << e[1..-1] else incl << e end end incl = nil if incl.empty? excl = nil if excl.empty? return incl, excl end |
#_set_serial_number(number) ⇒ Object
After doing a redo of the numbering, we need to reset the numbers for that app
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/todorb/common/cmdapp.rb', line 128 def _set_serial_number number appname = @appname pattern = Regexp.new "^#{appname}:.*$" filename = @app_serial_path || "serial_numbers" # during testing redo this file does not exist, so i get errors if !File.exists? filename _get_serial_number end _backup filename change_row filename, pattern, "#{appname}:#{number}" end |
#add_action(name, descr) ⇒ Object
94 95 96 97 |
# File 'lib/todorb/common/cmdapp.rb', line 94 def add_action name, descr @actions ||= {} @actions[name.to_s] = desc end |
#alias_command(name, *args) ⇒ Object
90 91 92 93 |
# File 'lib/todorb/common/cmdapp.rb', line 90 def alias_command name, *args @aliases ||= {} @aliases[name.to_s] = args end |
#check_aliases(action, args) ⇒ Boolean
external dependencies:
@app_default_action - action to run if none specified
@app_file_path - data file we are backing up, or reading into array
@app_serial_path - serial_number file
check whether this action is mapped to some alias and changes variables@action and @argv if true.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/todorb/common/cmdapp.rb', line 30 def check_aliases action, args return false unless @aliases ret = @aliases[action] if ret a = ret.shift b = [*ret, *args] @action = a @argv = b #puts " #{@action} ; argv: #{@argv} " return true end return false end |
#die(text) ⇒ Object
144 145 146 147 |
# File 'lib/todorb/common/cmdapp.rb', line 144 def die text $stderr.puts text exit ERRCODE end |
#filter_rows(rows, incl) ⇒ Object
creates a regexp and for each row returns the row and the regexp you can use the regexp on whatever part of the row you want to match or reject
237 238 239 240 241 242 243 244 245 |
# File 'lib/todorb/common/cmdapp.rb', line 237 def filter_rows rows, incl if incl incl_str = incl.join "|" r = Regexp.new incl_str #rows = rows.select { |row| row['title'] =~ r } rows = rows.select { |row| yield(row, r) } end rows end |
#help(args) ⇒ Object
not required if using Subcommand
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/todorb/common/cmdapp.rb', line 70 def help args if @actions.nil? if defined? @commands unless @commands.empty? @actions = @commands end end end if @actions puts "Actions are " @actions.each_pair { |name, val| puts "#{name}\t#{val}" } end puts " " if @aliases puts "Aliases are " @aliases.each_pair { |name, val| puts "#{name}:\t#{val.join(' ')}" } end 0 end |
#load_array ⇒ Object
load data into array as item and task
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/todorb/common/cmdapp.rb', line 172 def load_array #return if $valid_array $valid_array = false @data = [] File.open(@app_file_path).each do |line| # FIXME: use @app_delim row = line.chomp.split "\t" @data << row end $valid_array = true end |
#message(text) ⇒ Object
prints messages to stderr All messages should go to stderr. Keep stdout only for output which can be used by other programs
151 152 153 |
# File 'lib/todorb/common/cmdapp.rb', line 151 def text $stderr.puts text end |
#print_green(text) ⇒ Object
165 166 167 |
# File 'lib/todorb/common/cmdapp.rb', line 165 def print_green text "#{GREEN}#{text}#{CLEAR}" end |
#print_red(text) ⇒ Object
162 163 164 |
# File 'lib/todorb/common/cmdapp.rb', line 162 def print_red text "#{RED}#{text}#{CLEAR}" end |
#run ⇒ 0, ERRCODE
runs method after checking if valid or alias. If not found prints help.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/todorb/common/cmdapp.rb', line 47 def run @action = @argv[0] || @app_default_action @action = @action.downcase ret = 0 @argv.shift if respond_to? @action ret = send(@action, @argv) else # check aliases if check_aliases @action, @argv ret = send(@action, @argv) else help @argv ret = ERRCODE end end ret ||= 0 ret = 0 if ret != ERRCODE return ret end |
#save_array ⇒ Object
saves the task array to disk Please use load_array to load, and not populate
186 187 188 189 190 191 192 193 |
# File 'lib/todorb/common/cmdapp.rb', line 186 def save_array raise "Cannot save array! Please use load_array to load" if $valid_array == false File.open(@app_file_path, "w") do |file| # FIXME: use join with @app_delim @data.each { |row| file.puts "#{row[0]}\t#{row[1]}" } end end |
#verbose(text) ⇒ Object
print to stderr only if verbose set
155 156 157 |
# File 'lib/todorb/common/cmdapp.rb', line 155 def verbose text (text) if @options[:verbose] end |
#version_info ⇒ String?
retrieve version info updated by jeweler. Typically used by –version option of any command.
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/todorb/common/cmdapp.rb', line 198 def version_info # thanks to Roger Pack on ruby-forum for how to get to the version # file filename = File.open(File.dirname(__FILE__) + "/../../VERSION") v = nil if File.exists?(filename) v = File.open(filename).read.chomp if File.exists?(filename) #else #$stderr.puts "could not locate file #{filename}. " #puts `pwd` end v end |
#warning(text) ⇒ Object
print to stderr only if verbose set
159 160 161 |
# File 'lib/todorb/common/cmdapp.rb', line 159 def warning text print_red("WARNING: #{text}") end |