Module: Cuboid::Utilities
- Extended by:
- Utilities
- Included in:
- Application, Processes::Agents, Processes::Instances, Processes::Schedulers, RPC::Server::Agent, RPC::Server::ApplicationWrapper, RPC::Server::Instance, RPC::Server::Scheduler, Report, Support::Mixins::Observable, Utilities
- Defined in:
- lib/cuboid/utilities.rb
Overview
Includes some useful methods for the system.
Class Method Summary collapse
Instance Method Summary collapse
-
#available_port(range = nil) ⇒ Fixnum
Random available port number.
- #bytes_to_kilobytes(bytes) ⇒ Object
- #bytes_to_megabytes(bytes) ⇒ Object
-
#caller_name ⇒ String
Filename (without extension) of the caller.
-
#caller_path(offset = 2) ⇒ String
Filepath of the caller.
-
#exception_jail(raise_exception = true, &block) ⇒ Object
Wraps the
blockin exception handling code and runs it. - #generate_token ⇒ Object
- #hms_to_seconds(time) ⇒ Object
-
#port_available?(port) ⇒ Bool
Checks whether the port number is available.
-
#rand_port(range = nil) ⇒ Integer
Random port within the user specified range.
-
#random_seed ⇒ String
Random HEX (SHA2) string.
- #regexp_array_match(regexps, str) ⇒ Object
- #remove_constants(mod, skip = []) ⇒ Object
-
#seconds_to_hms(seconds) ⇒ String
Time in
00:00:00(hours:minutes:seconds) format.
Class Method Details
.available_port_mutex ⇒ Object
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.
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_name ⇒ String
Returns 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.
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.
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_token ⇒ Object
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.
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.
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_seed ⇒ String
Returns 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.
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 |