Module: Kwatable::Util
- Defined in:
- lib/kwatable/util.rb
Class Method Summary collapse
-
.encode(str, encoding) ⇒ Object
encode string.
-
.eval_template(template_filename, context, template = nil) ⇒ Object
evaluate template with context object.
-
.find_file(filename, path_list, find_in_currdir = true, test_char = ?f) ⇒ Object
find file from path_list.
-
.parse_argv(argv, single_opts = '', arged_opts = '', optional_opts = '') ⇒ Object
parse command-line options.
-
.untabify(str, width = 8) ⇒ Object
expand tab characters into spaces.
Class Method Details
.encode(str, encoding) ⇒ Object
encode string
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/kwatable/util.rb', line 60 def encode(str, encoding) require 'kconv' out_code = case encoding when /\Aeuc-jp\z/i ; Kconv::EUC when /\As(hift[-_])?jis\z/i ; Kconv::SJIS when /\Autf8\z/i ; Kconv::UTF8 when /\Autf16\z/i ; Kconv::UTF16 when /\Aiso-2022-jp\z/i ; Kconv::JIS else ; nil end return Kconv.kconv(str, out_code) end |
.eval_template(template_filename, context, template = nil) ⇒ Object
evaluate template with context object
49 50 51 52 53 54 |
# File 'lib/kwatable/util.rb', line 49 def eval_template(template_filename, context, template=nil) template ||= File.read(template_filename) trim_mode = 1 erb = ERB.new(template, nil, trim_mode) return context.instance_eval(erb.src, template_filename) end |
.find_file(filename, path_list, find_in_currdir = true, test_char = ?f) ⇒ Object
find file from path_list
36 37 38 39 40 41 42 43 |
# File 'lib/kwatable/util.rb', line 36 def find_file(filename, path_list, find_in_currdir=true, test_char=?f) ch = test_char return filename if find_in_currdir && test(ch, filename) #return nil if filename[0] == ?/ # absolute path fname = nil found = path_list.find { |path| test(ch, fname = "#{path}/#{filename}") } return found ? fname : nil end |
.parse_argv(argv, single_opts = '', arged_opts = '', optional_opts = '') ⇒ Object
parse command-line options
ex.
argv = %w[-hqu user -p passwd --help --name=val file1 file2]
, properties, filenames = Util.parse_argv(argv, "hvq", "upl")
p #=> { ?h=>true, ?q=>true, ?u=>"user", ?p=>"passwd" }
p properties #=> { :help=>true, :name=>"val" }
p filenames #=> [ "file1", "file2" ]
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/kwatable/util.rb', line 84 def parse_argv(argv, single_opts='', arged_opts='', optional_opts='') = {} properties = {} while argv[0] && argv[0][0] == ?- optstr = argv.shift if optstr == '-' break elsif optstr =~ /\A--([-\w]+)(?:=(.*))?/ # properties key, value = $1, $2 case value when nil ; value = true when "true", "yes" ; value = true when "false", "no" ; value = false when "null", "nil" ; value = nil when /\A\d+\z/ ; value = value.to_i when /\A\d+\.\d+\z/ ; value = value.to_f when /\A'.*'\z/ ; value = eval(value) when /\A".*"\z/ ; value = eval(value) end properties[key.gsub(/-/, '_').intern] = value else # options optstr = optstr.slice(1..-1) while optstr && !optstr.empty? optchar = optstr[0] optstr = optstr.slice(1..-1) if single_opts && single_opts.include?(optchar) [optchar] = true elsif arged_opts && arged_opts.include?(optchar) arg = optstr.empty? ? argv.shift : optstr optstr = nil raise "-#{optchar.chr}: argument required." unless arg [optchar] = arg elsif optional_opts && optional_opts.include?(optchar) arg = optstr.empty? ? true : optstr optstr = nil [optchar] = arg else raise "-#{optchar.chr}: unknown option." end end #while end #if end #while filenames = argv return , properties, filenames end |
.untabify(str, width = 8) ⇒ Object
expand tab characters into spaces
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/kwatable/util.rb', line 19 def untabify(str, width=8) list = str.split(/\t/) last = list.pop sb = '' list.each do |s| column = (n = s.rindex(?\n)) ? s.length - n - 1 : s.length n = width - (column % width) sb << s << (" " * n) end sb << last return sb end |