Class: CallerUtils
- Defined in
- lib/kyanite/general/callerutils.rb
CallerUtils
- Kyanite definitions
- Kyanite tests and examples
- Usage
-
require 'kyanite/general/callerutils'
Class Method Summary (collapse)
-
+ (String, Integer) mycaller(options = {})
Examines the call stack, returns the size of the stack, or the last caller.
-
+ (String) mycaller_maindir(mycaller)
Determines the root directory of a caller on heuristic way.
Class Method Details
+ (String, Integer) mycaller(options = {})
Examines the call stack, returns the size of the stack, or the last caller. Example:
CallerUtils.mycaller(:skip => ['ruby\gems', 'ruby/gems', 'test/unit'])
=> "C:/Ruby-Projekte/kyanite/lib/kyanite/general/callerutils.rb:110:in `<main>'"
CallerUtils.mycaller(:skip => ['ruby\gems', 'ruby/gems', 'test/unit'], :mode=> :size)
=> 1
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/kyanite/general/callerutils.rb', line 29 def self.mycaller(={}) skip = [:skip] || [] skip = [skip] if skip.kind_of?(String) mode = [:mode] || :caller result = nil size = caller.size if skip.empty? return caller[0] if mode == :caller return size if mode == :size end caller.each_with_index do | c, i | #puts "caller=#{c}" #puts "skip=#{skip.inspect_pp}" next if c.include?(skip) #puts "ok! \n" if mode == :caller result = c elsif mode == :size result = (size - i) end # if mode break end # each caller # puts "mycaller=#{result}" result end |
+ (String) mycaller_maindir(mycaller)
Determines the root directory of a caller on heuristic way. The name of the main directory is usually the name of the application or library. Example:
my_caller = CallerUtils.mycaller(:skip => ['ruby\gems', 'ruby/gems', 'test/unit'])
CallerUtils.mycaller_maindir(my_caller)
=> "C:/Ruby-Projekte/kyanite"
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/kyanite/general/callerutils.rb', line 65 def self.mycaller_maindir(mycaller) dir_caller =File.dirname(mycaller) array_caller = dir_caller.split('/') array_caller = dir_caller.split("\\") if array_caller.size == 1 #require 'pp' #pp array_caller indicatorfiles = %W{Init Boot History License Manifest Rakefile README ReadMe Readme Setup} check = [] (-array_caller.size).upto(-1) do |lev| indicatorfiles.each do | f | next if array_caller[lev..lev] == '..' check << array_caller[0..lev].join('/') + "/#{f}.rb" check << array_caller[0..lev].join('/') + "/#{f}.txt" check << array_caller[0..lev].join('/') + "/#{f}" check << array_caller[0..lev].join('/') + "/#{f.downcase}.rb" check << array_caller[0..lev].join('/') + "/#{f.downcase}.txt" check << array_caller[0..lev].join('/') + "/#{f.downcase}" end end #return check maindir = '' check.uniq.each do | try | # puts "try #{try}" maindir = try break if File.exist?(try) end # puts "mycaller_maindir=#{File.dirname(maindir) }" return File.dirname(maindir) end |