Module: Familia::Utils
- Included in:
- Familia
- Defined in:
- lib/familia/utils.rb
Overview
Family-related utility methods
Instance Method Summary collapse
-
#dbkey(*val) ⇒ String
Creates a dbkey from given values.
-
#join(*val) ⇒ String
Joins array elements with Familia delimiter.
-
#now(current_time = Time.now) ⇒ Float
Returns current time in UTC as a float.
-
#pretty_path(filepath) ⇒ Pathname, ...
Converts an absolute file path to a path relative to the current working directory.
-
#pretty_stack(skip: 1, limit: 5) ⇒ String
Formats a stack trace with pretty file paths for improved readability.
-
#qstamp(quantum = 10.minutes, pattern: nil, time: nil) ⇒ Integer, String
A quantized timestamp.
-
#serverid(uri) ⇒ Object
Gets server ID without DB component for pool identification.
-
#split(val) ⇒ Array
Splits a string using Familia delimiter.
Instance Method Details
#dbkey(*val) ⇒ String
Creates a dbkey from given values
26 27 28 |
# File 'lib/familia/utils.rb', line 26 def dbkey(*val) join(*val) end |
#join(*val) ⇒ String
Joins array elements with Familia delimiter
12 13 14 |
# File 'lib/familia/utils.rb', line 12 def join(*val) val.compact.join(Familia.delim) end |
#now(current_time = Time.now) ⇒ Float
Returns current time in UTC as a float
41 42 43 |
# File 'lib/familia/utils.rb', line 41 def now(current_time = Time.now) current_time.utc.to_f end |
#pretty_path(filepath) ⇒ Pathname, ...
Converts an absolute file path to a path relative to the current working directory. This simplifies logging and error reporting by showing only the relevant parts of file paths instead of lengthy absolute paths.
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/familia/utils.rb', line 90 def pretty_path(filepath) return nil if filepath.nil? basepath = Dir.pwd relative_path = Pathname.new(filepath).relative_path_from(basepath) if relative_path.to_s.start_with?('..') File.basename(filepath) else relative_path end end |
#pretty_stack(skip: 1, limit: 5) ⇒ String
Formats a stack trace with pretty file paths for improved readability
110 111 112 |
# File 'lib/familia/utils.rb', line 110 def pretty_stack(skip: 1, limit: 5) caller(skip..(skip + limit + 1)).first(limit).map { |frame| pretty_path(frame) }.join("\n") end |
#qstamp(quantum = 10.minutes, pattern: nil, time: nil) ⇒ Integer, String
A quantized timestamp
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/familia/utils.rb', line 59 def qstamp(quantum = 10.minutes, pattern: nil, time: nil) time ||= Familia.now time = time.to_f if time.is_a?(Time) rounded = time - (time % quantum) if pattern Time.at(rounded).utc.strftime(pattern) else Time.at(rounded).utc.to_i end end |
#serverid(uri) ⇒ Object
Gets server ID without DB component for pool identification
31 32 33 34 35 36 |
# File 'lib/familia/utils.rb', line 31 def serverid(uri) # Create a copy of URI without DB for server identification uri = uri.dup uri.db = nil uri.serverid end |