Module: SemanticLogger::Utils

Defined in:
lib/semantic_logger/utils.rb

Overview

Internal-use only utility functions for Semantic Logger. Not intended for public use.

Constant Summary collapse

SELF_PATTERN =
File.join('lib', 'semantic_logger')
GEM_ROOT =
File.expand_path('../../..', __dir__) + '/'

Class Method Summary collapse

Class Method Details

.camelize(term) ⇒ Object

Borrow from Rails, when not running Rails



15
16
17
18
19
20
21
# File 'lib/semantic_logger/utils.rb', line 15

def self.camelize(term)
  string = term.to_s
  string = string.sub(/^[a-z\d]*/, &:capitalize)
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }
  string.gsub!('/'.freeze, '::'.freeze)
  string
end

.constantize_symbol(symbol, namespace = 'SemanticLogger::Appender') ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/semantic_logger/utils.rb', line 5

def self.constantize_symbol(symbol, namespace = 'SemanticLogger::Appender')
  klass = "#{namespace}::#{camelize(symbol.to_s)}"
  begin
    Object.const_get(klass)
  rescue NameError
    raise(ArgumentError, "Could not convert symbol: #{symbol.inspect} to a class in: #{namespace}. Looking for: #{klass}")
  end
end

.extract_backtrace(stack = caller) ⇒ Object

Extract the backtrace leaving out the last few Semantic Logger lines.



38
39
40
41
42
43
# File 'lib/semantic_logger/utils.rb', line 38

def self.extract_backtrace(stack = caller)
  while (first = stack.first) && first.include?(SELF_PATTERN)
    stack.shift
  end
  stack
end

.method_visibility(mod, method_name) ⇒ Object

Returns the visibility for an instance method



24
25
26
27
28
29
30
31
32
33
# File 'lib/semantic_logger/utils.rb', line 24

def self.method_visibility(mod, method_name)
  method_name = method_name.to_sym
  if mod.instance_methods.include?(method_name)
    :public
  elsif mod.private_instance_methods.include?(method_name)
    :private
  elsif mod.protected_instance_methods.include?(method_name)
    :protected
  end
end

.strip_backtrace(stack = caller) ⇒ Object

Strips off all gems and built-in ruby code paths from the top of the stack until application code is found.



46
47
48
49
50
51
# File 'lib/semantic_logger/utils.rb', line 46

def self.strip_backtrace(stack = caller)
  while (first = stack.first) && system_path?(first)
    stack.shift
  end
  stack
end

.system_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/semantic_logger/utils.rb', line 55

def self.system_path?(path)
  path.start_with?(GEM_ROOT) ||
    path.start_with?(RbConfig::CONFIG['rubylibdir'])
end