Module: Bdsync::Utils

Defined in:
lib/bdsync/utils.rb

Class Method Summary collapse

Class Method Details

.caller_info(level) ⇒ Object



29
30
31
32
# File 'lib/bdsync/utils.rb', line 29

def self.caller_info level
    info = caller[level].match(%r{([^/]+):(\d+):in `(.+)'})
    "#{info.captures[0]}:#{info.captures[1]} - #{info.captures[2]}"
end

.file_md5(file_path) ⇒ Object



25
26
27
# File 'lib/bdsync/utils.rb', line 25

def self.file_md5 file_path
    Digest::MD5.file(file_path).hexdigest
end

.md5(s) ⇒ Object



21
22
23
# File 'lib/bdsync/utils.rb', line 21

def self.md5(s)
    Digest::MD5.hexdigest(s)
end

.timestampObject

Examples:

puts Utils.timestamp > 2019-12-11.16-15-57

puts Utils.timestamp.delete(“-”) > 20191211



13
14
15
16
17
18
19
# File 'lib/bdsync/utils.rb', line 13

def self.timestamp
    ts = Time.now.to_s[0..18]
    ts[10] = "."
    ts[13] = ts[16] = "-"

    ts
end

.try_lock(&block) ⇒ Object



34
35
36
37
38
39
# File 'lib/bdsync/utils.rb', line 34

def self.try_lock &block
    File.open(__FILE__, 'r') { |f|
        return if !f.flock(File::LOCK_EX | File::LOCK_NB)
        yield
    }
end