Module: Common
- Defined in:
- lib/ektoplayer/common.rb
Class Method Summary collapse
- .extract_zip(zip_file, dest) ⇒ Object
- .mksingleton(cls) ⇒ Object
- .open_image_extern(url) ⇒ Object
- .open_url_extern(url) ⇒ Object
- .to_time(secs) ⇒ Object
- .with_hash_zip(keys, values) {|hash| ... } ⇒ Object
Class Method Details
.extract_zip(zip_file, dest) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ektoplayer/common.rb', line 29 def self.extract_zip(zip_file, dest) absolute_path = File.absolute_path(zip_file) # try 'unzip' out, err, status = Open3.capture3('unzip', absolute_path, chdir: dest) fail err unless status.exitstatus == 0 rescue Errno::ENOENT # try '7zip' out, err, status = Open3.capture3('7z', ?x, absolute_path, chdir: dest) fail err unless status.exitstatus == 0 rescue Errno::ENOENT # try RubyZip gem require 'zip' Zip::File.open(absolute_path) do |zip_obj| zip_obj.each do |f| f.extract(File.join(dest, f.name)) end end rescue LoadError fail 'neither RubzZip gem nor /bin/unzip or /bin/7z found' rescue Ektoplayer::Application.log(self, "error extracting zip", zip_file, dest, $!) end |
.mksingleton(cls) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ektoplayer/common.rb', line 71 def self.mksingleton(cls) unless cls.singleton_methods.include? :_get_instance cls.define_singleton_method(:_get_instance) do unless cls.class_variable_defined? :@@_class_instance cls.class_variable_set :@@_class_instance, cls.new end cls.class_variable_get :@@_class_instance end (cls.instance_methods - Object.instance_methods).each do |method| cls.define_singleton_method(method) do |*args| cls._get_instance.send(method, *args) end end end end |
.open_image_extern(url) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/ektoplayer/common.rb', line 19 def self.open_image_extern(url) fork do exec('feh', url) rescue ( exec('display', url) rescue ( exec('xdg-open', url) ) ) end end |
.open_url_extern(url) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/ektoplayer/common.rb', line 11 def self.open_url_extern(url) if url =~ /\.(jpe?g|png)$/i Common.open_image_extern(url) else fork { exec('xdg-open', url) } end end |
.to_time(secs) ⇒ Object
66 67 68 69 |
# File 'lib/ektoplayer/common.rb', line 66 def self.to_time(secs) return '00:00' unless secs or secs == 0 "%0.2d:%0.2d" % [(secs / 60), (secs % 60)] end |
.with_hash_zip(keys, values) {|hash| ... } ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ektoplayer/common.rb', line 53 def self.with_hash_zip(keys, values) hash = {} keys.size.times do |i| hash[keys[i]] = values[i] end yield hash keys.clear << hash.keys values.clear << hash.values end |