module DataMetaXtra

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

Constants

LOGGER_DTTM_FMT

Default Logger datetime format

VERSION

Current version

Public Class Methods

appendGlobs(arr, patterns) click to toggle source

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

@param [Array] arr array to append the globs to @param [Array] patterns array of glob patterns

@return [Array] flattenned source array with the filenames by the glob patterns appended to the end

# 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) click to toggle source

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

@param [Hash] opts standard Ruby options hash keyed by a sym, see individual options for details @param [Trollop::Parser] parser optional Trollop parser to call “educate” on.

# 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) click to toggle source

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

# File lib/dataMetaXtra.rb, line 171
def logName(fullPath); "#{fullPath.chomp(File.extname(fullPath))}.log"  end
nilBinding() click to toggle source

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.

# File lib/dataMetaXtra.rb, line 106
def nilBinding; ProcFactory.new.create.binding end
nowMicros() click to toggle source

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

# File lib/dataMetaXtra.rb, line 191
def nowMicros; (Time.now.utc.to_f * 1000000).to_i end
nowMillis() click to toggle source

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

# File lib/dataMetaXtra.rb, line 183
def nowMillis; (Time.now.utc.to_f * 1000).to_i end
nowSeconds() click to toggle source

UTC seconds of now.

# File lib/dataMetaXtra.rb, line 186
def nowSeconds; Time.now.utc.to_i end
nowWdSeconds() click to toggle source

Current-Directory-Basename - dash - seconds.

# File lib/dataMetaXtra.rb, line 194
def nowWdSeconds; "#{File.basename(Dir.getwd)}-#{nowSeconds}" end
toMicros(time) click to toggle source

Turns the given instance to microseconds as integer.

# File lib/dataMetaXtra.rb, line 177
def toMicros(time); (time.to_f * 1000000).to_i end
toMillis(time) click to toggle source

Turns the given instance to milliseconds as integer.

# File lib/dataMetaXtra.rb, line 174
def toMillis(time); (time.to_f * 1000).to_i end
toPathList(arr) click to toggle source

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

# File lib/dataMetaXtra.rb, line 208
def toPathList(arr); arr.join(File::PATH_SEPARATOR) end
winArgHack(file) click to toggle source

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__.

# 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

Private Instance Methods

appendGlobs(arr, patterns) click to toggle source

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

@param [Array] arr array to append the globs to @param [Array] patterns array of glob patterns

@return [Array] flattenned source array with the filenames by the glob patterns appended to the end

# 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) click to toggle source

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

@param [Hash] opts standard Ruby options hash keyed by a sym, see individual options for details @param [Trollop::Parser] parser optional Trollop parser to call “educate” on.

# 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) click to toggle source

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

# File lib/dataMetaXtra.rb, line 171
def logName(fullPath); "#{fullPath.chomp(File.extname(fullPath))}.log"  end
nilBinding() click to toggle source

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.

# File lib/dataMetaXtra.rb, line 106
def nilBinding; ProcFactory.new.create.binding end
nowMicros() click to toggle source

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

# File lib/dataMetaXtra.rb, line 191
def nowMicros; (Time.now.utc.to_f * 1000000).to_i end
nowMillis() click to toggle source

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

# File lib/dataMetaXtra.rb, line 183
def nowMillis; (Time.now.utc.to_f * 1000).to_i end
nowSeconds() click to toggle source

UTC seconds of now.

# File lib/dataMetaXtra.rb, line 186
def nowSeconds; Time.now.utc.to_i end
nowWdSeconds() click to toggle source

Current-Directory-Basename - dash - seconds.

# File lib/dataMetaXtra.rb, line 194
def nowWdSeconds; "#{File.basename(Dir.getwd)}-#{nowSeconds}" end
toMicros(time) click to toggle source

Turns the given instance to microseconds as integer.

# File lib/dataMetaXtra.rb, line 177
def toMicros(time); (time.to_f * 1000000).to_i end
toMillis(time) click to toggle source

Turns the given instance to milliseconds as integer.

# File lib/dataMetaXtra.rb, line 174
def toMillis(time); (time.to_f * 1000).to_i end
toPathList(arr) click to toggle source

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

# File lib/dataMetaXtra.rb, line 208
def toPathList(arr); arr.join(File::PATH_SEPARATOR) end
winArgHack(file) click to toggle source

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__.

# 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