Module: SemanticLogger::Utils

Defined in:
lib/semantic_logger/utils.rb

Constant Summary collapse

SELF_PATTERN =
File.join('lib', 'semantic_logger')

Class Method Summary collapse

Class Method Details

.camelize(term) ⇒ Object

Borrow from Rails, when not running Rails



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

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

.cleanse_backtrace(stack = caller) ⇒ Object

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



40
41
42
43
44
45
# File 'lib/semantic_logger/utils.rb', line 40

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

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



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

def self.constantize_symbol(symbol, namespace = 'SemanticLogger::Appender')
  klass = "#{namespace}::#{camelize(symbol.to_s)}"
  begin
    if RUBY_VERSION.to_i >= 2
      Object.const_get(klass)
    else
      klass.split('::').inject(Object) { |o, name| o.const_get(name) }
    end
  rescue NameError
    raise(ArgumentError, "Could not convert symbol: #{symbol.inspect} to a class in: #{namespace}. Looking for: #{klass}")
  end
end

.method_visibility(mod, method_name) ⇒ Object

Returns the visibility for an instance method



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

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