Sha256: c3b80cdb7bc93b49bd2e38daa9467a33df874874e8cd4582de5eaf22ee6f9c35

Contents?: true

Size: 855 Bytes

Versions: 4

Compression:

Stored size: 855 Bytes

Contents

require "digest"

module Bdsync
    module Utils
        # Examples:
        #
        # puts Utils.timestamp
        # > 2019-12-11.16-15-57
        #
        # puts Utils.timestamp[0...10].delete("-")
        # > 20191211
        #
        def self.timestamp
            ts = Time.now.to_s[0..18]
            ts[10] = "."
            ts[13] = ts[16] = "-"

            ts
        end

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

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bdsync-2.1.2 lib/bdsync/utils.rb
bdsync-2.1.1 lib/bdsync/utils.rb
bdsync-2.1.0 lib/bdsync/utils.rb
bdsync-2.0.0 lib/bdsync/utils.rb