Module: Logrithm::Utils::Helpers

Defined in:
lib/logrithm/utils/helpers.rb

Class Method Summary collapse

Class Method Details

.constantize(string, namespace = Kernel, inherit = false) ⇒ Object



5
6
7
8
9
10
# File 'lib/logrithm/utils/helpers.rb', line 5

def constantize(string, namespace = Kernel, inherit = false)
  namespace = Kernel.const_get(namespace) unless namespace.is_a?(Module)
  namespace.const_get string.to_s.gsub(/(?:\A|_)(\w)/) { |m| m[-1].upcase }, inherit
rescue NameError
  nil
end

.dirty_lookup_class_method(file, lineno) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/logrithm/utils/helpers.rb', line 12

def dirty_lookup_class_method(file, lineno)
  content = File.readlines(file)[0..lineno.to_i].reverse
  result = content.each_with_object(method: nil, ns: []) do |line, memo|
    (memo[:method] ||= line[/\A\s*def\s+(?<method>\w+)/, :method]) &&
      (memo[:ns] << line[/\A\s*(?:class|module)\s+(?<ns>\w+)/, :ns])
  end

  [const_get(result[:ns].compact.reverse.join('::')), result[:method]]
end