Module: Helper
- Defined in:
- lib/helper.rb,
lib/web/helper4web.rb
Overview
雑多なお助けメソッド群
Defined Under Namespace
Modules: CacheLoader Classes: AsyncCommand, InvalidVariableName, InvalidVariableType, UnknownVariableType
Constant Summary collapse
- HOST_OS =
RbConfig::CONFIG["host_os"]
- ENTITIES =
{ quot: '"', amp: "&", nbsp: " ", lt: "<", gt: ">", copy: "(c)", "#39" => "'" }
- TYPE_OF_VALUE =
{ TrueClass => :boolean, FalseClass => :boolean, Fixnum => :integer, Float => :float, String => :string }
Class Method Summary collapse
-
.ampersand_to_entity(str) ⇒ Object
アンパサンドをエンティティに変換.
-
.convert_to_windows_path(path) ⇒ Object
CYGWINのパスからwindowsのパスへと変換(cygpathを呼び出すだけ).
-
.copy_files(from, dest_dir) ⇒ Object
ファイルを指定したディレクトリにまとめてコピーする 指定したディレクトリが存在しなければ作成する.
-
.date_string_to_time(date) ⇒ Object
日付形式の文字列をTime型に変換する.
- .determine_os ⇒ Object
- .engine_jruby? ⇒ Boolean
-
.extract_illust_chuki(str) ⇒ Object
文章の中から挿絵注記を分離する.
-
.file_latest?(path) ⇒ Boolean
指定のファイルが前回のチェック時より新しいかどうか.
- .getch ⇒ Object
- .open_browser(url) ⇒ Object
- .open_browser_linux(address, error_message) ⇒ Object
- .open_directory(path, confirm_message = nil) ⇒ Object
- .original_print_horizontal_rule ⇒ Object
- .os_cygwin? ⇒ Boolean
- .os_mac? ⇒ Boolean
- .os_windows? ⇒ Boolean
-
.pretreatment_source(src, encoding = Encoding::UTF_8) ⇒ Object
ダウンロードしてきたデータを使いやすいように処理.
- .print_horizontal_rule ⇒ Object
- .replace_filename_special_chars(str, invalid_replace = false) ⇒ Object
-
.restor_entity(str) ⇒ Object
エンティティ復号.
-
.string_cast_to_type(value, type) ⇒ Object
文字列データを指定された型にキャストする.
-
.to_unprintable_words(string, mask = "●") ⇒ Object
伏せ字にする.
-
.type_of_value(value) ⇒ Object
Rubyの変数がなんの型かシンボルで取得.
-
.variable_type_to_description(type) ⇒ Object
与えられた型情報の意味文字列を取得.
Class Method Details
.ampersand_to_entity(str) ⇒ Object
アンパサンドをエンティティに変換
139 140 141 |
# File 'lib/helper.rb', line 139 def ampersand_to_entity(str) str.gsub(/&(?!amp;)/mi, "&") end |
.convert_to_windows_path(path) ⇒ Object
CYGWINのパスからwindowsのパスへと変換(cygpathを呼び出すだけ)
132 133 134 |
# File 'lib/helper.rb', line 132 def convert_to_windows_path(path) `cygpath -aw \"#{path}\"`.strip end |
.copy_files(from, dest_dir) ⇒ Object
ファイルを指定したディレクトリにまとめてコピーする 指定したディレクトリが存在しなければ作成する
from: ファイルパスをまとめた Array dest_dir: コピー先のディレクトリ
253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/helper.rb', line 253 def copy_files(from, dest_dir) from.each do |path| basename = File.basename(path) dirname = File.basename(File.dirname(path)) save_dir = File.join(dest_dir, dirname) unless File.directory?(save_dir) FileUtils.mkdir_p(save_dir) end FileUtils.copy(path, File.join(save_dir, basename)) end end |
.date_string_to_time(date) ⇒ Object
日付形式の文字列をTime型に変換する
268 269 270 |
# File 'lib/helper.rb', line 268 def date_string_to_time(date) date ? Time.parse(date.sub(/[\((].+?[\))]/, "").tr("年月日時分秒@;", "///::: :")) : nil end |
.determine_os ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/helper.rb', line 30 def determine_os case when os_windows? :windows when os_mac? :mac when os_cygwin? :cygwin else :other end end |
.engine_jruby? ⇒ Boolean
43 44 45 |
# File 'lib/helper.rb', line 43 def engine_jruby? @@engine_is_jruby ||= RUBY_ENGINE == "jruby" end |
.extract_illust_chuki(str) ⇒ Object
文章の中から挿絵注記を分離する
146 147 148 149 150 151 152 153 |
# File 'lib/helper.rb', line 146 def extract_illust_chuki(str) illust_chuki_array = [] extracted_str = str.gsub(/[ \t]*?([#挿絵(.+?)入る])\n?/) do illust_chuki_array << $1 "" end [extracted_str, illust_chuki_array] end |
.file_latest?(path) ⇒ Boolean
指定のファイルが前回のチェック時より新しいかどうか
初回チェック時は無条件で新しいと判定
277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/helper.rb', line 277 def file_latest?(path) @@file_mtime_list ||= {} fullpath = File.(path) last_mtime = @@file_mtime_list[fullpath] mtime = File.mtime(fullpath) if mtime == last_mtime result = false else result = true @@file_mtime_list[fullpath] = mtime end result end |
.getch ⇒ Object
49 50 51 |
# File 'lib/helper.rb', line 49 def $stdin.getch WinAPI._getch.chr end |
.open_browser(url) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/helper.rb', line 80 def open_browser(url) case determine_os when :windows escaped_url = url.gsub("%", "%^").gsub("&", "^&") # MEMO: start の引数を "" で囲むと動かない system(%!start #{escaped_url}!) when :cygwin system(%!cygstart #{url}!) when :mac system(%!open "#{url}"!) else open_browser_linux(url, "ブラウザが見つかりませんでした") end end |
.open_browser_linux(address, error_message) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/helper.rb', line 56 def open_browser_linux(address, ) %w(xdg-open firefox w3m).each do |browser| system(%!#{browser} "#{address}"!) return if $?.success? end error end |
.open_directory(path, confirm_message = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/helper.rb', line 64 def open_directory(path, = nil) if return unless Narou::Input.confirm(, false, false) end case determine_os when :windows system(%!explorer "file:///#{path.encode(Encoding::Windows_31J)}"!) when :cygwin system(%!cygstart "#{path}"!) when :mac system(%!open "#{path}"!) else open_browser_linux(path, "フォルダが開けませんでした") end end |
.original_print_horizontal_rule ⇒ Object
12 13 14 |
# File 'lib/web/helper4web.rb', line 12 def print_horizontal_rule puts "―" * 35 end |
.os_cygwin? ⇒ Boolean
26 27 28 |
# File 'lib/helper.rb', line 26 def os_cygwin? @@os_is_cygwin ||= HOST_OS =~ /cygwin/i end |
.os_mac? ⇒ Boolean
22 23 24 |
# File 'lib/helper.rb', line 22 def os_mac? @@os_is_mac ||= HOST_OS =~ /darwin/i end |
.os_windows? ⇒ Boolean
18 19 20 |
# File 'lib/helper.rb', line 18 def os_windows? @@os_is_windows ||= HOST_OS =~ /mswin(?!ce)|mingw|bccwin/i end |
.pretreatment_source(src, encoding = Encoding::UTF_8) ⇒ Object
ダウンロードしてきたデータを使いやすいように処理
112 113 114 115 |
# File 'lib/helper.rb', line 112 def pretreatment_source(src, encoding = Encoding::UTF_8) src.force_encoding(encoding).gsub("\r", "") .encode("UTF-16BE", encoding, :invalid => :replace, :undef => :replace, :replace => "?").encode("UTF-8") end |
.print_horizontal_rule ⇒ Object
95 96 97 |
# File 'lib/helper.rb', line 95 def print_horizontal_rule puts "―" * 35 end |
.replace_filename_special_chars(str, invalid_replace = false) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/helper.rb', line 99 def replace_filename_special_chars(str, invalid_replace = false) result = str.tr("/:*?\"<>|.", "/:*?”〈〉|.").gsub("\\", "¥").gsub("\t", "").gsub("\n", "") if invalid_replace org_encoding = result.encoding result = result.encode(Encoding::Windows_31J, invalid: :replace, undef: :replace, replace: "_") .encode(org_encoding) end result end |
.restor_entity(str) ⇒ Object
エンティティ復号
121 122 123 124 125 126 127 |
# File 'lib/helper.rb', line 121 def restor_entity(str) result = str.dup ENTITIES.each do |key, value| result.gsub!("&#{key};", value) end result end |
.string_cast_to_type(value, type) ⇒ Object
文字列データを指定された型にキャストする
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/helper.rb', line 196 def string_cast_to_type(value, type) result = nil case type when :boolean case value.strip.downcase when "true" result = true when "false" result = false else raise InvalidVariableType, type end when :integer begin result = Integer(value) rescue raise InvalidVariableType, type end when :float begin result = Float(value) rescue raise InvalidVariableType, type end when :directory, :file if File.method("#{type}?").call(value) result = File.(value) else raise InvalidVariableType, type end when :string, :select, :multiple result = value else raise UnknownVariableType, type end result end |
.to_unprintable_words(string, mask = "●") ⇒ Object
伏せ字にする
数字やスペース、句読点、感嘆符はそのままにする
296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/helper.rb', line 296 def to_unprintable_words(string, mask = "●") result = "" string.each_char do |char| result += case char when /[0-90-9 、。!?!?]/ char else mask end end result end |
.type_of_value(value) ⇒ Object
Rubyの変数がなんの型かシンボルで取得
242 243 244 |
# File 'lib/helper.rb', line 242 def type_of_value(value) TYPE_OF_VALUE[value.class] end |
.variable_type_to_description(type) ⇒ Object
与えられた型情報の意味文字列を取得
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/helper.rb', line 172 def variable_type_to_description(type) case type when :boolean "true/false " when :integer "整数 " when :float "小数点数 " when :string, :select "文字列 " when :multiple "文字列(複数)" when :directory "フォルダパス" when :file "ファイルパス" else raise UnknownVariableType, type end end |