Module: DataMetaXtra

Defined in:
lib/dataMetaXtra.rb,
lib/dataMetaXtra/fileSys.rb

Overview

Various extensions to the Ruby SDK addressing whatever shortcomings we run into.

Defined Under Namespace

Modules: FileSys, Str, Sys Classes: ProcFactory

Constant Summary collapse

VERSION =

Current version

'1.0.0'
LOGGER_DTTM_FMT =

Default Logger datetime format

'%Y-%m-%d %H:%M:%S'

Class Method Summary collapse

Class Method Details

.appendGlobs(arr, patterns) ⇒ Array

Collects several glob patterns to this array. This is so simple that can be inlined.

Parameters:

  • arr (Array)

    array to append the globs to

  • patterns (Array)

    array of glob patterns

Returns:

  • (Array)

    flattenned source array with the filenames by the glob patterns appended to the end



205
# File 'lib/dataMetaXtra.rb', line 205

def appendGlobs(arr, patterns); patterns.each { |p| arr << Dir.glob(p) }; arr.flatten end

.defaultLogger(opts = nil, parser = nil) ⇒ Object

Builds a default logger for the given trollop opts and parser. If the opts and parser passed, builds the logger according to the options, with daily rollover and max size 10 M.

If no opts or no parser passed, returns the logger to STDOUT for the WARN level.

There is no point in creating a more generic method, the bare Logger.new call creates a weekly logger with the 1M size, to STDOUT. Can call Logger.new('file.log')

The default level is DEBUG, therefore you may want change it

Parameters:

  • opts (Hash) (defaults to: nil)

    standard Ruby options hash keyed by a sym, see individual options for details

  • parser (Trollop::Parser) (defaults to: nil)

    optional Trollop parser to call “educate” on.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dataMetaXtra.rb', line 60

def defaultLogger(opts=nil, parser=nil)
    if opts && parser
        #noinspection RubyArgCount
        result = Logger.new(opts[:logFile] || 'dataMetaXtra.log', 'daily', 10*1024*1024)
        result.level = case opts[:level] ? opts[:level].downcase[0] : 'i'
           when 'd'
               Logger::DEBUG
           when 'i'
               Logger::INFO
           when 'w'
               Logger::WARN
           when 'e'
               Logger::ERROR
           else
               parser.educate
               raise "Invalid log level #{opts[:level]}"
        end
        result.datetime_format = '%Y-%m-%d %H:%M:%S'
        result
    else
        result = Logger.new($stdout)
        result.level = Logger::WARN
        result
    end
end

.logName(fullPath) ⇒ Object

Returns a log name for the given filename, replacing the extension with “log”.

Normally would pass __FILE__ to this method.

To get a full path, can call File.extend_path



171
# File 'lib/dataMetaXtra.rb', line 171

def logName(fullPath); "#{fullPath.chomp(File.extname(fullPath))}.log"  end

.nilBindingObject

New empty binding for evaluation to avoid exposing class variables

Require anything that you may need here outside of the block, it bubbles into here.



106
# File 'lib/dataMetaXtra.rb', line 106

def nilBinding; ProcFactory.new.create.binding end

.nowMicrosObject

UTC micros of now. not delegated to #toMicros because method calls are still relatively expensive in Ruby, especially in older versions



191
# File 'lib/dataMetaXtra.rb', line 191

def nowMicros; (Time.now.utc.to_f * 1000000).to_i end

.nowMillisObject

likely to be lacking precision UTC millis of now. not delegated to #toMillis because method calls are still relatively expensive in Ruby, especially in older versions



183
# File 'lib/dataMetaXtra.rb', line 183

def nowMillis; (Time.now.utc.to_f * 1000).to_i end

.nowSecondsObject

UTC seconds of now.



186
# File 'lib/dataMetaXtra.rb', line 186

def nowSeconds; Time.now.utc.to_i end

.nowWdSecondsObject

Current-Directory-Basename - dash - seconds.



194
# File 'lib/dataMetaXtra.rb', line 194

def nowWdSeconds; "#{File.basename(Dir.getwd)}-#{nowSeconds}" end

.toMicros(time) ⇒ Object

Turns the given instance to microseconds as integer.



177
# File 'lib/dataMetaXtra.rb', line 177

def toMicros(time); (time.to_f * 1000000).to_i end

.toMillis(time) ⇒ Object

Turns the given instance to milliseconds as integer.



174
# File 'lib/dataMetaXtra.rb', line 174

def toMillis(time); (time.to_f * 1000).to_i end

.toPathList(arr) ⇒ Object

Turns this array to a PATH list according to File::PATH_SEPARATOR.



208
# File 'lib/dataMetaXtra.rb', line 208

def toPathList(arr); arr.join(File::PATH_SEPARATOR) end

.winArgHack(file) ⇒ Object

Hack for Windows that often the name of the executable into the first argument: Discard the first argument if any if it has the same base file name as the current runnable Pass the caller’s __FILE__.



91
92
93
# File 'lib/dataMetaXtra.rb', line 91

def winArgHack(file)
    ARGV.shift if !ARGV.empty? && ARGV[0] && File.exist?(ARGV[0]) && File.basename(ARGV[0]) == File.basename(file)
end