Module: Cuboid::Utilities

Overview

Includes some useful methods for the system.

Author:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_port_mutexObject



40
41
42
# File 'lib/cuboid/utilities.rb', line 40

def self.available_port_mutex
    @available_port_mutex ||= Mutex.new
end

Instance Method Details

#available_port(range = nil) ⇒ Fixnum

Returns Random available port number.

Returns:

  • (Fixnum)

    Random available port number.



31
32
33
34
35
36
37
38
# File 'lib/cuboid/utilities.rb', line 31

def available_port( range = nil )
    available_port_mutex.synchronize do
        loop do
            port = self.rand_port( range )
            return port if port_available?( port )
        end
    end
end

#bytes_to_kilobytes(bytes) ⇒ Object



96
97
98
# File 'lib/cuboid/utilities.rb', line 96

def bytes_to_kilobytes( bytes )
    (bytes / 1024.0 ).round( 3 )
end

#bytes_to_megabytes(bytes) ⇒ Object



92
93
94
# File 'lib/cuboid/utilities.rb', line 92

def bytes_to_megabytes( bytes )
    (bytes / 1024.0 / 1024.0).round( 3 )
end

#caller_nameString

Returns Filename (without extension) of the caller.

Returns:

  • (String)

    Filename (without extension) of the caller.



14
15
16
# File 'lib/cuboid/utilities.rb', line 14

def caller_name
    File.basename( caller_path( 3 ), '.rb' )
end

#caller_path(offset = 2) ⇒ String

Returns Filepath of the caller.

Returns:

  • (String)

    Filepath of the caller.



20
21
22
# File 'lib/cuboid/utilities.rb', line 20

def caller_path( offset = 2 )
    ::Kernel.caller[offset].split( /:(\d+):in/ ).first
end

#exception_jail(raise_exception = true, &block) ⇒ Object

Wraps the ‘block` in exception handling code and runs it.

Parameters:

  • raise_exception (Bool) (defaults to: true)

    Re-raise exception?

  • block (Block)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cuboid/utilities.rb', line 105

def exception_jail( raise_exception = true, &block )
    block.call
rescue => e
    if respond_to?( :print_error ) && respond_to?( :print_exception )
        print_exception e
        print_error
        print_error 'Parent:'
        print_error  self.class.to_s
        print_error
        print_error 'Block:'
        print_error block.to_s
        print_error
        print_error 'Caller:'
        ::Kernel.caller.each { |l| print_error l }
        print_error '-' * 80
    end

    raise e if raise_exception

    nil
end

#generate_tokenObject



55
56
57
# File 'lib/cuboid/utilities.rb', line 55

def generate_token
    SecureRandom.hex
end

#hms_to_seconds(time) ⇒ Object



85
86
87
88
89
90
# File 'lib/cuboid/utilities.rb', line 85

def hms_to_seconds( time )
    a = [1, 60, 3600] * 2
    time.split( /[:\.]/ ).map { |t| t.to_i * a.pop }.inject(&:+)
rescue
    0
end

#port_available?(port) ⇒ Bool

Checks whether the port number is available.

Parameters:

  • port (Fixnum)

Returns:

  • (Bool)


64
65
66
67
68
69
70
71
72
73
# File 'lib/cuboid/utilities.rb', line 64

def port_available?( port )
    begin
        socket = ::Socket.new( :INET, :STREAM, 0 )
        socket.bind( ::Socket.sockaddr_in( port, '127.0.0.1' ) )
        socket.close
        true
    rescue Errno::EADDRINUSE, Errno::EACCES
        false
    end
end

#rand_port(range = nil) ⇒ Integer

Returns Random port within the user specified range.

Returns:

  • (Integer)

    Random port within the user specified range.



47
48
49
50
51
52
53
# File 'lib/cuboid/utilities.rb', line 47

def rand_port( range = nil )
    range ||= [1025, 65535]
    first, last = range
    range = (first..last).to_a

    range[ rand( range.last - range.first ) ]
end

#random_seedString

Returns random HEX (SHA2) string.

Returns:

  • (String)

    random HEX (SHA2) string



25
26
27
# File 'lib/cuboid/utilities.rb', line 25

def random_seed
    @@random_seed ||= generate_token
end

#regexp_array_match(regexps, str) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/cuboid/utilities.rb', line 127

def regexp_array_match( regexps, str )
    regexps = [regexps].flatten.compact.
        map { |s| s.is_a?( Regexp ) ? s : Regexp.new( s.to_s ) }
    return true if regexps.empty?

    cnt = 0
    regexps.each { |filter| cnt += 1 if filter.match? str }
    cnt == regexps.size
end

#remove_constants(mod, skip = []) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cuboid/utilities.rb', line 137

def remove_constants( mod, skip = [] )
    return if skip.include?( mod )
    return if !(mod.is_a?( Class ) || mod.is_a?( Module )) ||
        !mod.to_s.start_with?( 'Cuboid' )

    parent = Object
    mod.to_s.split( '::' )[0..-2].each do |ancestor|
        parent = parent.const_get( ancestor.to_sym )
    end

    mod.constants.each { |m| mod.send( :remove_const, m ) }
    nil
end

#seconds_to_hms(seconds) ⇒ String

Returns Time in ‘00:00:00` (`hours:minutes:seconds`) format.

Parameters:

  • seconds (String, Float, Integer)

Returns:

  • (String)

    Time in ‘00:00:00` (`hours:minutes:seconds`) format.



79
80
81
82
83
# File 'lib/cuboid/utilities.rb', line 79

def seconds_to_hms( seconds )
    seconds = seconds.to_i
    [seconds / 3600, seconds / 60 % 60, seconds % 60].
        map { |t| t.to_s.rjust( 2, '0' ) }.join( ':' )
end